/**
* 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;
}
/**
* 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;
}
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);
}
}