This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 7.移动零 | |
void moveZeroes(int* nums, int numsSize) { | |
if (numsSize <= 0) { | |
return; | |
} | |
int j = 0 ; | |
for (int i = 0; i < numsSize; i++) { | |
if (nums[i]) { | |
nums[j++] = nums[i]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 反转字符串 | |
char* reverseString(char* s) { | |
int len=strlen(s); | |
int mid=len/2; | |
for(int i=0,j=len-1;i<mid,i<j;i++,j--){ | |
char tmp; | |
tmp=s[i]; | |
s[i]=s[j]; | |
s[j]=tmp; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 反转字符串 | |
char* reverseString(char* s) { | |
int len=strlen(s); | |
int mid=len/2; | |
for(int i=0,j=len-1;i<mid,i<j;i++,j--){ | |
char tmp; | |
tmp=s[i]; | |
s[i]=s[j]; | |
s[j]=tmp; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
如何判断是否溢出? | |
其实看我上面的代码也可以看得出了,只要把这个式子反着推过来,再来看是否相等就行了。 | |
加法溢出判断:若c=a+b; c-a!=b则溢出;或者a, b>0, c<0溢出;或者a, b<0, c>0溢出; | |
减法溢出判断:若c=a-b; c+b!=a则溢出; | |
除法溢出判断:若b!=0 && a/b=c; b*c!=a则溢出 | |
乘法溢出判断:若c=a*b; a!=0 && c/a!=b则溢出 | |
*/ | |
// 颠倒整数 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
如何判断是否溢出? | |
其实看我上面的代码也可以看得出了,只要把这个式子反着推过来,再来看是否相等就行了。 | |
加法溢出判断:若c=a+b; c-a!=b则溢出;或者a, b>0, c<0溢出;或者a, b<0, c>0溢出; | |
减法溢出判断:若c=a-b; c+b!=a则溢出; | |
除法溢出判断:若b!=0 && a/b=c; b*c!=a则溢出 | |
乘法溢出判断:若c=a*b; a!=0 && c/a!=b则溢出 | |
*/ | |
// 颠倒整数 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ListNode { | |
int val; | |
struct ListNode *next; | |
}; | |
// 删除连表中的节点 | |
void deleteNode(struct ListNode* node) { | |
node->val = node->next->val; | |
node->next = node->next->next; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct TreeNode { | |
int val; | |
struct TreeNode *left; | |
struct TreeNode *right; | |
}; | |
// 二叉树的最大深度 | |
int maxDepth(struct TreeNode* root) { | |
if (root) { | |
int left = 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 帕斯卡三角形 杨辉三角 | |
int** generate(int numRows, int** columnSizes) { | |
if(numRows<=0) return NULL; | |
int **ret=(int **)malloc(numRows*sizeof(int *)); | |
int row,column; | |
*columnSizes = (int *)malloc(numRows*sizeof(int)); | |
//printf("成功初始化数组与行数矩阵\n"); | |
for(row=0;row<numRows;row++) | |
{(*columnSizes)[row]=row+1; | |
ret[row]=(int*)malloc((row+1)*sizeof(int)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 位1的个数, | |
int hammingWeight(uint32_t n) { | |
int count = 0; | |
while (n) { | |
++count; | |
n = (n-1)&n; | |
} | |
return count; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 颠倒二进制位 | |
uint32_t reverseBits(uint32_t n) { | |
uint32_t m=0; | |
for(int i=0;i<32;i++){ | |
m<<=1; | |
m=m|(n&1); | |
//m<<=1; | |
n>>=1;//必须是这样的顺序,m左移32位,如果在下面,左移了33位; | |
} | |
return m; |