力扣(LeetCode)刷题,简单题(第10期)

举报
不脱发的程序猿 发表于 2021/01/02 00:22:03 2021/01/02
【摘要】 目录 第1题:有序数组的平方 第2题:增减字符串匹配 第3题:数字的补数 第4题:Nim游戏 第5题:删除字符串中的所有相邻重复项 第6题:除数博弈 第7题:转换成小写字母 第8题:生成每种字符都是奇数个的字符串 第9题:按奇偶排序数组 第10题:转置矩阵 力扣(LeetCode)定期刷题,每期10道题,业务繁重的同志可以看看我分享的思路,不是最高效解...

目录

第1题:有序数组的平方

第2题:增减字符串匹配

第3题:数字的补数

第4题:Nim游戏

第5题:删除字符串中的所有相邻重复项

第6题:除数博弈

第7题:转换成小写字母

第8题:生成每种字符都是奇数个的字符串

第9题:按奇偶排序数组

第10题:转置矩阵


力扣(LeetCode)定期刷题,每期10道题,业务繁重的同志可以看看我分享的思路,不是最高效解决方案,只求互相提升。

第1题:有序数组的平方

试题要求如下:

回答(C语言):


  
  1. /**
  2. * Note: The returned array must be malloced, assume caller calls free().
  3. */
  4. int cmp (const void * a, const void * b)
  5. {
  6. return ( *(int*)a - *(int*)b );
  7. }
  8. int* sortedSquares(int* A, int ASize, int* returnSize){
  9. *returnSize=ASize;
  10. for(int i=0;i<ASize;i++){
  11. A[i]=A[i]*A[i];
  12. }
  13. qsort(A, ASize, sizeof(int), cmp);
  14. return A;
  15. }

运行效率如下所示:


第2题:增减字符串匹配

试题要求如下:

回答(C语言):


  
  1. /**
  2. * Note: The returned array must be malloced, assume caller calls free().
  3. */
  4. int* diStringMatch(char * S, int* returnSize)
  5. {
  6. int N = strlen(S);
  7. int a=N;
  8. int b=0;
  9. int *ret = (int *)malloc(sizeof(int)*(N+1));
  10. *returnSize = N + 1;
  11. for (int i=0;i<N;i++){
  12. if (S[i] == 'I'){
  13. ret[i] = b;
  14. b++;
  15. }
  16. else{
  17. ret[i] = a;
  18. a--;
  19. }
  20. }
  21. ret[N] = b;
  22. return ret;
  23. }

运行效率如下所示:


第3题:数字的补数

试题要求如下:

回答(C语言):


  
  1. int findComplement(int num){
  2. long temp = 1;
  3. while (num >= temp){
  4. temp <<= 1;
  5. }
  6. return (temp - 1 - num);
  7. }

运行效率如下所示:


第4题:Nim游戏

试题要求如下:

回答(C语言):


  
  1. bool canWinNim(int n){
  2. return (n % 4 != 0);
  3. }

运行效率如下所示:


第5题:删除字符串中的所有相邻重复项

试题要求如下:

回答(C语言):


  
  1. char * removeDuplicates(char * S){
  2. int i, cnt, len = strlen(S);
  3. for(i = 1, cnt = 0;i <= len;++i)
  4. {
  5. if(cnt == -1 || S[i] != S[cnt])
  6. S[++cnt] = S[i];
  7. else
  8. --cnt;
  9. }
  10. return S;
  11. }

运行效率如下所示:


第6题:除数博弈

试题要求如下:

回答(C语言):


  
  1. bool divisorGame(int N){
  2. // 如果爱丽丝拿到奇数,则必输,
  3. // 因为奇数的因数必为奇数,则N-x必为偶数,
  4. // 鲍勃拿到偶数之后,只需要-1交还给爱丽丝,则爱丽丝再次拿到奇数,
  5. // 这样鲍勃永远拿到偶数,直到拿到2赢下比赛
  6. // 反过来,如果爱丽丝拿到偶数,只需要-1给鲍勃,那么鲍勃就必输。
  7. return N%2==0;
  8. }

运行效率如下所示:


第7题:转换成小写字母

试题要求如下:

回答(C语言):


  
  1. char * toLowerCase(char * str){
  2. for(int i=0;i<strlen(str);i++){
  3. if(str[i]>='A'&&str[i]<='Z')
  4. str[i]=str[i]+32;
  5. }
  6. return str;
  7. }

运行效率如下所示:


第8题:生成每种字符都是奇数个的字符串

试题要求如下:

回答(C语言):


  
  1. char * generateTheString(int n)
  2. {
  3. char *ret=(char *)malloc(sizeof(char)*(n+1));
  4. ret[n]='\0';
  5. memset(ret,'a',n);
  6. ret[n-1]='a'+(n%2==0);
  7. return ret;
  8. }

运行效率如下所示:


第9题:按奇偶排序数组

试题要求如下:

回答(C语言):


  
  1. /**
  2. * Note: The returned array must be malloced, assume caller calls free().
  3. */
  4. int* sortArrayByParity(int* A, int ASize, int* returnSize){
  5. int i=0,j=ASize-1;
  6. int temp=0;
  7. while(i<j){
  8. if(A[i]%2!=0 && A[j]%2==0){
  9. temp=A[j];
  10. A[j]=A[i];
  11. A[i]=temp;
  12. }
  13. if(A[i]%2==0)
  14. i++;
  15. if(A[j]%2!=0)
  16. j--;
  17. }
  18. *returnSize=ASize;
  19. return A;
  20. }

运行效率如下所示:


第10题:转置矩阵

试题要求如下:

回答(C语言):


  
  1. /**
  2. * Return an array of arrays of size *returnSize.
  3. * The sizes of the arrays are returned as *returnColumnSizes array.
  4. * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
  5. */
  6. int** transpose(int** A, int ASize, int* AColSize, int* returnSize, int** returnColumnSizes){
  7. int** num=(int**)malloc(sizeof(int*)*(*AColSize));
  8. *returnColumnSizes=(int*)malloc(sizeof(int)*(*AColSize));
  9. *returnSize=*AColSize;
  10. //分配内存
  11. for(int i=0;i<*AColSize;i++){
  12. num[i]=(int*)malloc(sizeof(int)*ASize);
  13. returnColumnSizes[0][i] = ASize;
  14. }
  15. //矩阵转置
  16. for(int i=0;i<ASize;i++){
  17. for(int j=0;j<*AColSize;j++){
  18. num[j][i]=A[i][j];
  19. }
  20. }
  21. return num;
  22. }

运行效率如下所示:

文章来源: handsome-man.blog.csdn.net,作者:不脱发的程序猿,版权归原作者所有,如需转载,请联系作者。

原文链接:handsome-man.blog.csdn.net/article/details/105279448

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。