【例题1】1108. IP 地址无效化 - 力扣(LeetCode)
char * defangIPaddr(char * address){
int len = strlen(address);
char* p = (char*)malloc(sizeof(char)*len + 6 + 1);
int i,j;
for(i=0,j=0;i<len;i++){
if(address[i]=='.'){
j +=sprintf(p+j,"%s","[.]");
//返回值:打印字符的个数
// p[j++] = '[';
// p[j++] = '.';
// p[j++] = ']';
}else{
p[j++] = address[i];
}
}
p[len+6] = '\0';
return p;
}
【例题2】LCR 122. 路径加密 - 力扣(LeetCode)
char* pathEncryption(char* path) {
int len = strlen(path);
char* p = (char*)malloc(sizeof(char)*(len+1));
for(int i=0;i<len+1;i++){
if(path[i] == '.') p[i] = ' ';
else p[i] = path[i];
}
p[len] = '\0';
return p;
}
【例题3】1913. 两个数对之间的最大乘积差 - 力扣(LeetCode)
int cmp (const void* p1,const void* p2){
return *(int*)p1 - *(int*)p2;
}
int maxProductDifference(int* nums, int numsSize){
qsort(nums,numsSize,sizeof(int),cmp);
return nums[numsSize-1]*nums[numsSize-2] - nums[0]*nums[1];
}
【例题4】976. 三角形的最大周长 - 力扣(LeetCode)
int cmp(const int* p1,const int* p2){
return *p2-*p1;
}
int largestPerimeter(int* nums, int numsSize){
qsort(nums,numsSize,sizeof(int),cmp);
for(int i=0;i<numsSize-2;i++){
if(nums[i]<nums[i+1]+nums[i+2]) return nums[i]+nums[i+1]+nums[i+2];
}
return 0;
}
【例题5】561. 数组拆分 - 力扣(LeetCode)
int cmp(const int* p1,const int* p2) {
return *p1-*p2;
}
int arrayPairSum(int* nums, int numsSize){
qsort(nums,numsSize,sizeof(int),cmp);
int sum = 0;
for(int i = 0;i<numsSize;i+=2){
sum+=nums[i];
}
return sum;
}