【例题1】885. 螺旋矩阵 III - 力扣(LeetCode)

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
 //           B
 //    A  7 8 9
 //       6 1 2
 //       5 4 3 C
 //       D             
#define NULLVAL -1094795586
int** spiralMatrixIII(int rows, int cols, int rStart, int cStart, int* returnSize, int** returnColumnSizes){
    //初始值
    int len = rows*cols;
    int cur = 0;
    int** ans =(int**)malloc(sizeof(int*)*len);
    for(int i=0;i<len;i++){
        ans[i] = (int*)malloc(sizeof(int)*2);
    }
    //规定返回值
    *returnSize = len;
    *returnColumnSizes = (int*)malloc(sizeof(int)*len);
    for(int i=0;i<len;i++){
        (*returnColumnSizes)[i] = 2;
    }
    //主代码逻辑
    int offset = 1;
    int* temp = (int*)malloc(sizeof(int)*2);
    temp[0] = rStart;
    temp[1] = cStart;
    ans[cur++] = temp;
    //4个角有一个为初始值 -- 循环一直进行
    while(cur <= len-1){
        int A = rStart - offset;
        int B = cStart + offset;
        int C = rStart + offset;
        int D = cStart - offset;
        //下
        for(int i=A+1;i<=C;i++){
            if(i<0||B<0||i>=rows||B>=cols) continue;
            int* arr = (int*)malloc(sizeof(int)*2);
            arr[0] = i;
            arr[1] = B;
            ans[cur++] = arr;
        }
        //左
        for(int i=B-1;i>=D;i--){
            if(i<0||C<0||C>=rows||i>=cols) continue;
            int* arr = (int*)malloc(sizeof(int)*2);
            arr[0] = C;
            arr[1] = i;
            ans[cur++] = arr;
        }
        //上
        for(int i=C-1;i>=A;i--){
            if(i<0||D<0||i>=rows||D>=cols) continue;
            int* arr = (int*)malloc(sizeof(int)*2);
            arr[0] = i;
            arr[1] = D;
            ans[cur++] = arr;
        }
        //右
        for(int i=D+1;i<=B;i++){
            if(i<0||A<0||A>=rows||i>=cols) continue;
            int* arr = (int*)malloc(sizeof(int)*2);
            arr[0] = A;
            arr[1] = i;
            ans[cur++] = arr;
        }
        offset++;
    }
    return ans;

}

【例题2】950. 按递增顺序显示卡牌 - 力扣(LeetCode)

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
// 找规律
//17 [17]->[17]
//13 [17, 13]->[13, 17]
//11 [13, 17, 11]->[17, 11, 13]
//7  [17, 11, 13, 7]->[11, 13, 7, 17]
//5  [11, 13, 7, 17, 5]->[13, 7, 17, 5, 11]
//3  [13, 7, 17, 5, 11, 3]->[7, 17, 5, 11, 3, 13]
//2  [7, 17, 5, 11, 3, 13, 2] ->结果
int cmp(const int* p1,const int* p2){
    return *p2 - *p1;
}
int* deckRevealedIncreasing(int* deck, int deckSize, int* returnSize){
    if(deckSize == 1){
        *returnSize = 1;
        return deck;
    }
    int* ans = (int*)malloc(sizeof(int)*deckSize);
    *returnSize = deckSize;
    qsort(deck,deckSize,sizeof(int),cmp);
    //加入最大值到队列尾,然后循环向左移动一位
    for(int i=0;i<deckSize;i++){
        ans[i] = deck[i];
        int temp = ans[0];
        for(int j=0;j<i;j++){
            ans[j] = ans[j+1];
        }
        ans[i] = temp;
    }
    for(int i = 0;i <= deckSize-1-i-1&& i<deckSize-1;i++){
        int temp = ans[i];
        ans[i] = ans[deckSize-1-i-1];
        ans[deckSize-1-i-1] = temp;
    }
    return ans; 
}

【例题3】1688. 比赛中的配对次数 - 力扣(LeetCode)

// 如果当前队伍数是 偶数 ,那么每支队伍都会与另一支队伍配对。
// 总共进行 n / 2 场比赛,且产生 n / 2 支队伍进入下一轮。
// 如果当前队伍数为 奇数 ,那么将会随机轮空并晋级一支队伍,其余的队伍配对。
// 总共进行 (n - 1) / 2 场比赛,且产生 (n - 1) / 2 + 1 支队伍进入下一轮。
int numberOfMatches(int n){
    int count=0;
    if(n == 1 || n == 0){
        return 0;
    }
    if(n%2==0){
        count+=(n/2) + numberOfMatches(n/2);
    }else{
        count+=((n-1)/2)+ numberOfMatches((n-1)/2 + 1);
    }
    return count;
}

【例题4】2169. 得到 0 的操作数 - 力扣(LeetCode)

int countOperations(int num1, int num2){
    if(num1==0){
        return 0;
    }
    if(num2==0){
        return 0;
    }
    if(num1 >= num2){
        return 1+countOperations(num1-num2,num2);
    }else{
        return 1+countOperations(num1,num2-num1);
    }
}

【例题5】258. 各位相加 - 力扣(LeetCode)

int addDigits(int num){
    if(num < 10) return num;
    return addDigits(num % 10 + addDigits(num / 10));
}