这道题就是给一个数num,然后去将其的每一位都相加起来,然后再将其每一位相加,直到它变成了一个1位数位置。
int addDigits(int num)
{
while(num >= 10)
{
int tmp = num,sum = 0;
//求每一位的总和
while(tmp != 0)
{
sum += (tmp % 10);
tmp /= 10;
}
//将num更新成当前每一位的总和
num = sum;
}
return num;
}
bool judgeCircle(char* moves)
{
int len = strlen(moves);
if(len % 2 != 0)
{
return false;
}
int i, x = 0, y = 0;
for (i = 0; i < len; i++)
{
if(moves[i] == 'U')
{
y++;
}
else if(moves[i] == 'R')
{
x++;
}
else if(moves[i] == 'D')
{
y--;
}
else
{
x--;
}
}
return x == 0 && y == 0;
}
有了上面的这三个条件,代码如下:
int* distributeCandies(int candies, int num_people, int* returnSize)
{
int* ans = (int*)calloc(num_people,sizeof(int));
*returnSize = num_people;
int i = 0,tmp = 1;
while(candies > 0)
{
//总糖果个数大于当前应该发的糖果个数
if(candies < tmp)
{
//将当前所剩的糖果分给当前孩子
ans[i] += candies;
break;
}
else
{
ans[i++] += tmp;
}
//分配完一组回去接着分配
if(i >= num_people)
{
i = 0;
}
candies -= tmp++;
}
return ans;
}
//给x y 所对应的行和列全部增1
void PlusOne(int x, int y,int** matrix, int row, int col)
{
int i;
//行
for (i = 0; i < col; i++)
{
matrix[x][i]++;
}
//列
for (i = 0; i < row; i++)
{
matrix[i][y]++;
}
}
int oddCells(int m, int n, int** indices, int indicesSize, int* indicesColSize)
{
int i,j,count = 0;
//创建矩阵
int** matrix = (int**)malloc(sizeof(int*) * m);
for (i = 0 ; i < m; i++)
{
matrix[i] = (int*)calloc(n,sizeof(int));
}
//处理矩阵自增
for (i = 0; i < indicesSize; i++)
{
PlusOne(indices[i][0],indices[i][1],matrix,m,n);
}
//统计矩阵中的奇数
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if(matrix[i][j] % 2 != 0)
{
count++;
}
}
}
return count;
}
int* createTargetArray(int* nums, int numsSize, int* index, int indexSize, int* returnSize)
{
int* ans = (int*)malloc(sizeof(int) * numsSize);
*returnSize = numsSize;
int i,size = 0;
for (i = 0; i < numsSize; i++)
{
int in = index[i],val = nums[i];
for (int j = size; j > in; j--)
{
ans[j] = ans[j-1];
}
ans[in] = val;
size++;
}
return ans;
}
int numWaterBottles(int numBottles, int numExchange)
{
int count = numBottles,emptyBottles = numBottles; //count 喝掉的 empty 空的
while(emptyBottles - numExchange >= 0)
{
//当前的空水瓶,可以兑换一瓶
count++;
emptyBottles -= (numExchange-1);
}
return count;
}
int* mostVisited(int n, int* rounds, int roundsSize, int* returnSize)
{
int* ans = (int*)malloc(sizeof(int) * n);
int i,index = 0;
int begin = rounds[0],end = rounds[roundsSize-1];
if(begin <= end)
{
for (i = begin; i <= end; i++)
{
ans[index++] = i;
}
}
else
{
//起点比终点大
for (i = 1; i <= n; i++)
{
if(i <= end || i >= begin)
{
ans[index++] = i;
}
}
}
*returnSize = index;
return ans;
}
typedef struct
{
int* map;
} ParkingSystem;
ParkingSystem* parkingSystemCreate(int big, int medium, int small)
{
ParkingSystem* obj = (ParkingSystem*)malloc(sizeof(ParkingSystem));
obj -> map = (int*)calloc(4,sizeof(int));
obj -> map[1] = big;
obj -> map[2] = medium;
obj -> map[3] = small;
return obj;
}
bool parkingSystemAddCar(ParkingSystem* obj, int carType)
{
if(obj -> map[carType] == 0)
{
return false;
}
obj -> map[carType]--;
return true;
}
void parkingSystemFree(ParkingSystem* obj)
{
free(obj -> map);
free(obj);
}
上图中,如果i为偶数的话,i%2永远是0,所以不用考虑后半段的值
int getMaximumGenerated(int n)
{
if(n == 0)
{
return 0;
}
int* nums = (int*)malloc(sizeof(int) * (n+1));
nums[1] = 1;
int max = nums[1],i;
for (i = 2; i <= n; i++)
{
nums[i] = nums[i/2] + (i % 2) * nums[i/2+1];
if(max < nums[i])
{
max = nums[i];
}
}
return max;
}
int numberOfMatches(int n)
{
int ans = 0;
while(n != 1)
{
int tmp = n / 2;
ans += tmp;
//判断奇偶
if(n % 2 != 0)
{
n = tmp + 1;
}
else
{
n = tmp;
}
}
return ans;
}
int* buildArray(int* nums, int numsSize, int* returnSize)
{
int* ans = (int*)malloc(sizeof(int) * numsSize);
*returnSize = numsSize;
int i;
for (i = 0; i < numsSize; i++)
{
ans[i] = nums[nums[i]];
}
return ans;
}
int* getConcatenation(int* nums, int numsSize, int* returnSize)
{
nums = (int*)realloc(nums,sizeof(int) * 2 * numsSize);
*returnSize = 2 * numsSize;
for (int i = 0; i < numsSize; i++)
{
nums[i + numsSize] = nums[i];
}
return nums;
}
int getLucky(char* s, int k)
{
int len = strlen(s);
char* ans = (char*)malloc(sizeof(char) * (2 * len + 1));
int i,pos = 0;
//将字母转化为数字
for (i = 0; i < len; i++)
{
pos += sprintf(ans + pos,"%d",s[i] - 'a' + 1);
}
//数字的长度
int lenAns = pos;
//循环k次
for (i = 0; i < k; i++)
{
//求出每次的值
int sum = 0;
for (int j = 0; j < lenAns; j++)
{
sum += ans[j] - '0';
}
//重新写入ans中并且更新长度
lenAns = sprintf(ans,"%d",sum);
}
return atoi(ans);
}
int finalValueAfterOperations(char** operations, int operationsSize)
{
int i,ans = 0;
for (i = 0; i < operationsSize; i++)
{
if(operations[i][1] == '+')
{
ans++;
}
else
{
ans--;
}
}
return ans;
}
char** divideString(char* s, int k, char fill, int* returnSize)
{
int len = strlen(s);
char** ans = (char**)malloc(sizeof(char*) * ((len/k) + 1));
int i,size = 0; //size ~ ans 的大小
//每一段的字符串
char* tmp = (char*)malloc(sizeof(char) * (k+1));
int index = 0;
//
for (i = 0; i < len; i++)
{
tmp[index++] = s[i];
if(index == k)
{
tmp[index] = '\0';
ans[size] = (char*)malloc(sizeof(char) * (k + 1));
strcpy(ans[size++],tmp);
index = 0;
count = 0;
}
}
//如果是正好全部分配完成的话,index == 0
if(index != 0)
{
while(index < k)
{
tmp[index++] = fill;
}
tmp[index] = '\0';
ans[size] = (char*)malloc(sizeof(char) * (k + 1));
strcpy(ans[size++],tmp);
}
*returnSize = size;
return ans;
}
int findFinalValue(int* nums, int numsSize, int original)
{
int i;
for (i = 0; i < numsSize; i++)
{
if(nums[i] == original)
{
original *= 2;
i = -1;
}
}
return original;
}
接着上面的,因为不知道顺序,所以从头开始找,那么进行排序即可解决这个问题。
int cmp_int(const void* x, const void* y)
{
return *(int*)x - *(int*)y;
}
int findFinalValue(int* nums, int numsSize, int original)
{
int i;
qsort(nums,numsSize,sizeof(nums[0]),cmp_int);
for (i = 0; i < numsSize; i++)
{
if(nums[i] == original)
{
original *= 2;
}
}
return original;
}
恩。。。。。。。。。
int countOperations(int num1, int num2)
{
int count = 0;
while(num1 != 0 && num2 != 0)
{
if(num1 >= num2)
{
num1 -= num2;
}
else
{
num2 -= num1;
}
count++;
}
return count;
}
int DigitSum(int num)
{
int sum = 0;
while(num != 0)
{
sum += (num % 10);
num /= 10;
}
return sum;
}
int countEven(int num)
{
int count = 0;
for (int i = 1; i <= num; i++)
{
int tmp = DigitSum(i);
if(tmp % 2 == 0)
{
count++;
}
}
return count;
}
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define MIN(x,y) ((x) > (y) ? (y) : (x))
int minMaxGame(int* nums, int numsSize)
{
while(numsSize != 1)
{
numsSize /= 2; //折半
for (int i = 0; i < numsSize; i++)
{
if(i % 2 == 0)
{
//偶数下标
nums[i] = MIN(nums[2 * i],nums[2 * i + 1]);
}
else
{
//奇数下标
nums[i] = MAX(nums[2 * i],nums[2 * i + 1]);
}
}
}
return nums[0];
}
char* digitSum(char* s, int k)
{
int len = strlen(s);
//k
while(len > k)
{
int i, pos = 0;
//存储新构成的字符串
char* tmp = (char*)malloc(sizeof(char) * (len + 1));
for (i = 0; i < len; i += k)
{
//size 不能越界,发现比i + k 大 的话,选择len就好了
int sum = 0,size = (i + k) < len ? i + k : len;
for (int j = i; j < size; j++)
{
sum += s[j] - '0';
}
pos += sprintf(tmp + pos, "%d",sum);
}
s = tmp;
len = strlen(s);
}
return s;
}
int cmp_int(const void* x, const void* y)
{
return *(int*)x - *(int*)y;
}
int minimumOperations(int* nums, int numsSize)
{
int count = 0,i;
qsort(nums,numsSize,sizeof(int),cmp_int);
//升序排序后
for (i = 0; i < numsSize; i++)
{
//第一个不为0的位置就是
if(nums[i] != 0)
{
//依次向后减去
int tmp = nums[i];
for (int j = i; j < numsSize; j++)
{
nums[j] -= tmp;
}
count++;
}
}
return count;
}
int* applyOperations(int* nums, int numsSize, int* returnSize)
{
int i,j = 0;
for (i = 0; i < numsSize; i++)
{
if( i + 1 < numsSize && nums[i] == nums[i + 1])
{
nums[i] *= 2;
nums[i + 1] = 0;
}
if(nums[i] != 0)
{
int tmp = nums[i];
nums[i] = nums[j];
nums[j++] = tmp;
}
}
*returnSize = numsSize;
return nums;
}
int* separateDigits(int* nums, int numsSize, int* returnSize)
{
int* ans = (int*)malloc(sizeof(int) * 1000000);
int i, size = 0;
for (i = 0; i < numsSize; i++)
{
int x = nums[i];
int tmp[6] = {0},index = 0; // 6 ~ (10^5)位数
while(x != 0)
{
tmp[index++] = x % 10;
x /= 10;
}
for (int j = index - 1; j >= 0; j--)
{
ans[size++] = tmp[j];
}
}
*returnSize = size;
return ans;
}
int cmp_int(const void* x, const void* y)
{
return *(int*)x - *(int*)y;
}
long long pickGifts(int* gifts, int giftsSize, int k)
{
long long ans = 0;
while(k > 0)
{
qsort(gifts,giftsSize,sizeof(int),cmp_int);
gifts[giftsSize-1] = sqrt(gifts[giftsSize-1]);
k--;
}
for (int i = 0; i < giftsSize; i++)
{
ans += gifts[i];
}
return ans;
}
//25 64 9 4 100
//25 64 9 4 10
//25 8 9 4 10
//5 8 9 4 10
//5 8 9 4 3
// 5+8+9+4+3=29
long long findTheArrayConcVal(int* nums, int numsSize)
{
long long ans = 0;
int i = 0,j = numsSize -1;
while(i < j)
{
char* tmp = (char*)malloc(sizeof(int) * (5 + 1)); //10*10*10*10=10000
int pos = 0;
pos += sprintf(tmp + pos,"%d",nums[i++]);
pos += sprintf(tmp + pos,"%d",nums[j--]);
ans += atoi(tmp);
}
if(i == j)
{
ans += nums[i];
}
return ans;
}
int passThePillow(int n, int time)
{
if((time / (n-1)) % 2 == 0)
{
//偶数回到起点
return (time % (n-1)) + 1;
}
else
{
//奇数,从终点往回退
return n - (time % (n-1));
}
}
int isWinner(int* player1, int player1Size, int* player2, int player2Size)
{
int sum1 = 0, sum2 = 0;
int i;
for (i = 0; i < player1Size; i++)
{
if((i - 1 >= 0 && player1[i-1] == 10) || ((i - 2 >= 0) && player1[i-2] == 10))
{
sum1 += player1[i] * 2;
}
else
{
sum1 += player1[i];
}
if((i - 1 >= 0 && player2[i-1] == 10) || ((i - 2 >= 0) && player2[i-2] == 10))
{
sum2 += player2[i] * 2;
}
else
{
sum2 += player2[i];
}
}
if(sum1 == sum2)
{
return 0;
}
return sum1 > sum2 ? 1 : 2;
}
int semiOrderedPermutation(int* nums, int numsSize)
{
int maxIndex,minIndex,i,count = 0;
for (i = 0; i <numsSize; i++)
{
if(nums[i] == 1)
{
minIndex = i;
}
if(nums[i] == numsSize)
{
maxIndex = i;
}
}
int swap = 0;
if(minIndex > maxIndex)
{
//公式
swap = 2 * (minIndex - maxIndex) - 1;
int tmp = minIndex;
minIndex = maxIndex;
maxIndex = tmp;
}
//小的移动到0 大的移动到numsSize - 1
count = swap + (minIndex - 0) + (numsSize - 1 - maxIndex);
return count;
}
int distanceTraveled(int mainTank, int additionalTank)
{
int count = 0; //记录使用了多少升,每5升可从备用中换取1升
while(mainTank != 0)
{
mainTank--;
count++;
//保证用了5升,并且备用油箱有油
if(count % 5 == 0 && additionalTank != 0)
{
mainTank++;
additionalTank--;
}
}
return count * 10;
}
int maximumNumberOfStringPairs(char ** words, int wordsSize)
{
int i, ans = 0;
for (i = 0; i < wordsSize; i++)
{
for (int j = i + 1; j < wordsSize; j++)
{
if(words[i][0] == words[j][1] && words[i][1] == words[j][0])
{
ans++;
break;
}
}
}
return ans;
}
如果范围不是2的话,反转一下字符串再去比较就好了