万人千题第一阶段报告【待继续总结】
學(xué)習(xí)內(nèi)容概況
目的:找編程和做題的手感
具體訓(xùn)練內(nèi)容:萬人千題第一階段題庫(思維導(dǎo)圖),同時(shí)還有一些之前做過的題
練習(xí)后總結(jié)
具體細(xì)節(jié)之后補(bǔ)充為文字版,概況思維導(dǎo)圖如下:
編程細(xì)節(jié)
- 位運(yùn)算使用技巧
- data<<1 = data*2、data>>1 = data / 2左移右移等于乘除2
- data & 1可以判斷是否為奇數(shù)
- data & (1<<i)可以判斷第i位是否為1
庫函數(shù)適當(dāng)使用
qsort
排序內(nèi)容必須是數(shù)組
原型:
void qsort (void* base, size_t num, size_t size,int (*compar)(const void*,const void*));代碼案例:
- 一維數(shù)組:
- /* qsort example */ #include <stdio.h> /* printf */ #include <stdlib.h> /* qsort */int values[] = { 40, 10, 100, 90, 20, 25 };int compare (const void * a, const void * b) {return ( *(int*)a - *(int*)b ); }int main () {int n;qsort (values, 6, sizeof(int), compare);for (n=0; n<6; n++)printf ("%d ",values[n]);return 0; }
- 二維數(shù)組:
- #include <stdio.h> /* printf */ #include <stdlib.h> /* qsort */int cmp(const void *a,const void *b) {int *ap = *(int **)a; int *bp = *(int **)b;if(ap[0] == bp[0])return ap[1] - bp[1];elsereturn ap[0] - bp[0]; } int cmp(const void *a, const void *b) {return ((int *)a)[0] - ((int *)b)[0]; }int main(){int *b,**a;int n = 10;a = (int**)malloc(n*sizeof(int*));。for(i=0;i<n;i++){b = malloc(2*sizeof(int));a[i] = b;}int c[10][2] = {0};//賦值cqsort(a, n, sizeof(a[0]), cmp);qsort(c, n, sizeof(c[0]), cmp);for(i=0;i<n;i++) { free(a[i]);}free(a); }
- 結(jié)構(gòu)體排序:
- /* qsort example */ #include <stdio.h> /* printf */ #include <stdlib.h> /* qsort */typedef struct node{int x; int y; int z; }Node;//-> is prioer than (type) int cmp(const void *a, const void *b) { // return (Node *)a->x - (Node *)b->x; //wrong! // return ((Node *)a)->x - ((Node *)b)->x; //wrong!return (*(Node *)a).x - (*(Node *)b).x; //right }int main () {// get many Node things.qsort(a, n, sizeof(a[0]), cmp); }
解題模板
盡量通過總結(jié)和理解,將題目抽象為一類題目,并構(gòu)建基礎(chǔ)編程模板
看了題解的問題
-
26. 刪除有序數(shù)組中的重復(fù)項(xiàng)
- 快慢指針的經(jīng)典使用
- 快指針先行探路,確定怎樣的是符合規(guī)范的
- 慢指針是新的標(biāo)準(zhǔn),用于接收符合條件的數(shù)據(jù)進(jìn)行存儲(chǔ)
- 符合條件的判斷準(zhǔn)則
- 快慢指針的經(jīng)典使用
-
33. 搜索旋轉(zhuǎn)排序數(shù)組
- 分情況討論分析
- 判斷條件的先后可能會(huì)影響復(fù)雜度
- 簡(jiǎn)化你的模型,不要陷入過度復(fù)雜的細(xì)節(jié)里,更多的簡(jiǎn)單重復(fù)的事情應(yīng)該交給計(jì)算機(jī)做
- 有序一定要嘗試二分法,即使無法實(shí)現(xiàn)嚴(yán)格的二分
- 分情況討論分析
-
41. 缺失的第一個(gè)正數(shù)
- 運(yùn)用數(shù)學(xué)方法去分析數(shù)據(jù)和題目,分析數(shù)據(jù)的特征,并尋找規(guī)律
- 數(shù)學(xué)原理:n個(gè)正整數(shù)序列中的尋找沒有出現(xiàn)過的最小的整數(shù)值,看數(shù)軸分析最多覆蓋[1,n],所以要么在[1,n]之間,要么是n + 1
- 基于上述原理,可以使用+ - 號(hào)和abs將原數(shù)組作為hash表判斷數(shù)據(jù)是否出現(xiàn)
-
121. 買賣股票的最佳時(shí)機(jī)
- 確定適用的貪心策略,在遍歷中迭代
- 記錄迭代所需要的數(shù)據(jù)
-
152. 乘積最大子數(shù)組
* -
154. 尋找旋轉(zhuǎn)排序數(shù)組中的最小值 II
-
201. 數(shù)字范圍按位與
-
414. 第三大的數(shù)
-
451. 根據(jù)字符出現(xiàn)頻率排序
-
509. 斐波那契數(shù)
-
628. 三個(gè)數(shù)的最大乘積
-
659. 分割數(shù)組為連續(xù)子序列
-
812. 最大三角形面積
-
1108. IP 地址無效化
-
1302. 層數(shù)最深葉子節(jié)點(diǎn)的和
-
1365. 有多少小于當(dāng)前數(shù)字的數(shù)字
-
1662. 檢查兩個(gè)字符串?dāng)?shù)組是否相等
-
1863. 找出所有子集的異或總和再求和
-
1925. 統(tǒng)計(jì)平方和三元組的數(shù)目
-
LCP 44. 開幕式焰火
-
劍指 Offer 55 - I. 二叉樹的深度
-
劍指 Offer 57 - II. 和為s的連續(xù)正數(shù)序列
-
劍指 Offer 64. 求1+2+…+n
-
面試題 02.03. 刪除中間節(jié)點(diǎn)
解答匯總
編寫的代碼遵循以下規(guī)范:
基礎(chǔ)語法&數(shù)據(jù)處理&數(shù)據(jù)結(jié)構(gòu)
位運(yùn)算
136. 只出現(xiàn)一次的數(shù)字
int singleNumber(int* nums, int numsSize){int res = 0;while (numsSize--) {printf("%d ",numsSize);res ^= nums[numsSize];}return res; }面試題 16.01. 交換數(shù)字
/*** Note: The returned array must be malloced, assume caller calls free().*/ int* swapNumbers(int* numbers, int numbersSize, int* returnSize){*returnSize = numbersSize;int *res = (int *)malloc(sizeof(int)*2);res[0] = numbers[0] ^ numbers[1];res[1] = numbers[1] ^ res[0];res[0] = res[1] ^ res[0];return res; }191. 位1的個(gè)數(shù)
int hammingWeight(uint32_t n) {int res =0;while (n) {res += n & 1;n >>= 1;}return res; }int hammingWeight(uint32_t n) {int res =0;while (n) {n &= n - 1;printf("%d ",n);res++;}return res; }1486. 數(shù)組異或操作
int xorOperation(int n, int start){int res = start;while (n--) {int num = start + 2 * n;res ^= num;} return res ^ start; }1720. 解碼異或后的數(shù)組
201. 數(shù)字范圍按位與
int rangeBitwiseAnd(int left, int right){int t = 0;while (left != right) {left >>= 1;right >>= 1;t++;}return left << t; }劍指 Offer 64. 求1+2+…+n
int sumNums(int n){int A = n;int B = n + 1;int ans = 0;//當(dāng)b為偶數(shù),a*b=2a * b/2;當(dāng)n為奇數(shù),a*b=(b-1)/2 * 2a+a(B & 1) && (ans += A);A <<= 1;B >>= 1;(B & 1) && (ans += A);A <<= 1;B >>= 1;(B & 1) && (ans += A);A <<= 1;B >>= 1;(B & 1) && (ans += A);A <<= 1;B >>= 1;(B & 1) && (ans += A);A <<= 1;B >>= 1;(B & 1) && (ans += A);A <<= 1;B >>= 1;(B & 1) && (ans += A);A <<= 1;B >>= 1;(B & 1) && (ans += A);A <<= 1;B >>= 1;(B & 1) && (ans += A);A <<= 1;B >>= 1;(B & 1) && (ans += A);A <<= 1;B >>= 1;(B & 1) && (ans += A);A <<= 1;B >>= 1;(B & 1) && (ans += A);A <<= 1;B >>= 1;(B & 1) && (ans += A);A <<= 1;B >>= 1;(B & 1) && (ans += A);A <<= 1;B >>= 1;return ans >> 1;}理解整數(shù)型
1281. 整數(shù)的各位積和之差
int subtractProductAndSum(int n){int mul = 1;int sum = 0;int num = 0;while (n) {num = n%10;mul *= num;sum += num;n /=10;}return mul - sum; }1290. 二進(jìn)制鏈表轉(zhuǎn)整數(shù)
/*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/int getDecimalValue(struct ListNode* head){struct ListNode *pNode = head;int sum = 0;while (pNode) {sum = sum * 2 + pNode->val;pNode = pNode->next;}return sum; }1342. 將數(shù)字變成 0 的操作次數(shù)
int numberOfSteps(int num){int res = 0;while (num) {if (num & 1) {res++;num--;continue;}res++;num >>= 1;}return res; }1480. 一維數(shù)組的動(dòng)態(tài)和
/*** Note: The returned array must be malloced, assume caller calls free().*/ int* runningSum(int* nums, int numsSize, int* returnSize){*returnSize = numsSize;int *res = (int *) malloc(sizeof(int) * numsSize);int i = 1;res[0] = nums[0];while (i < numsSize) {res[i] = res[i - 1] + nums[i];i++;}return res; }628. 三個(gè)數(shù)的最大乘積
int cmp(int *a, int *b) {return *a - *b; }int maximumProduct(int* nums, int numsSize){qsort(nums, numsSize, sizeof(int), cmp);int res = (nums[numsSize-1] * nums[numsSize - 2] * nums[numsSize - 3]) > (nums[0] * nums[1] *nums[numsSize - 1]) ?(nums[numsSize-1] * nums[numsSize - 2] * nums[numsSize - 3]) :(nums[0] * nums[1] *nums[numsSize - 1]);return res; }1588. 所有奇數(shù)長(zhǎng)度子數(shù)組的和
int sumOddLengthSubarrays(int* arr, int arrSize){int res = 0;int left = 0;int right = 0;int sum = 0;while (left < arrSize){sum += arr[right++];if (((right - left) & 1)) {res += sum;}if (right >= arrSize) {left++;right = left;sum = 0;} }return res; }485. 最大連續(xù) 1 的個(gè)數(shù)
int findMaxConsecutiveOnes(int* nums, int numsSize){int cnt = 0;int res = 0;for (int i = 0; i < numsSize ; i++) {res = cnt > res ? cnt : res;cnt = cnt * nums[i] + nums[i]; }return cnt > res ? cnt : res; }1822. 數(shù)組元素積的符號(hào)
int signFunc(int x) {return x > 0 ? 1 : -1; } int arraySign(int* nums, int numsSize){int mul = 1;while (numsSize--) {if (nums[numsSize] == 0)return 0;if (nums[numsSize] < 0)mul *= (-1);}return mul; }1295. 統(tǒng)計(jì)位數(shù)為偶數(shù)的數(shù)字
int findNumbers(int* nums, int numsSize){int res = 0;while (numsSize--) {int cnt = 1;int num = nums[numsSize];while (num/=10) {cnt++;}if (cnt & 1) {continue;}res++;}return res; }劍指 Offer 17. 打印從1到最大的n位數(shù)
/*** Note: The returned array must be malloced, assume caller calls free().*/ int* printNumbers(int n, int* returnSize){int size = 0;while (n) {size = size * 10 + 9;n--;}*returnSize = size;int *res = (int *) malloc(sizeof(int) * size);while (size--) {res[size] = size + 1;}return res; }劍指 Offer II 003. 前 n 個(gè)數(shù)字二進(jìn)制中 1 的個(gè)數(shù)
/*** Note: The returned array must be malloced, assume caller calls free().*/ int* countBits(int n, int* returnSize){int *res = (int *)malloc(sizeof(int) * (n + 1));*returnSize = n + 1;res[0] = 0;for (int i = 0; i <= n; i++) {res[i] = res[i >> 1] + (i & 1);}return res; }字符串?
344. 反轉(zhuǎn)字符串
void reverseString(char* s, int sSize){for (int i = 0; i < sSize/2; i++) {s[i] ^= s[sSize - 1 - i];s[sSize - 1 - i] ^= s[i];s[i] ^= s[sSize - 1 - i];} }1662. 檢查兩個(gè)字符串?dāng)?shù)組是否相等
bool arrayStringsAreEqual(char ** word1, int word1Size, char ** word2, int word2Size){int w1 = 0;int w2 = 0;int c1 = 0;int c2 = 0;while (w1 < word1Size && w2 < word2Size) {int flag = 0;if (word1[w1][c1] == '\0') {w1++;c1 = 0;flag = 1;} if (word2[w2][c2] == '\0') {w2++;c2 = 0;flag = 1;}if (flag) {continue;}char ch1 = word1[w1][c1++];char ch2 = word2[w2][c2++];if (ch1 != ch2) {return false;}}if ((w1 != word1Size) || (w2 != word2Size)) {return false;}return true; }1832. 判斷句子是否為全字母句
bool checkIfPangram(char * sentence){int hash[26] = {0};int len = strlen(sentence);while (len--) {hash[sentence[len] - 'a']++;}for (int i = 0; i < 26; i++) {if (hash[i] == 0) {return false;}}return true; }1684. 統(tǒng)計(jì)一致字符串的數(shù)目
int countConsistentStrings(char * allowed, char ** words, int wordsSize){int res = 0;int hash[26] = {0};int len = strlen(allowed);while (len--) {hash[allowed[len] - 'a']++;}while (wordsSize--) {char *tmp = words[wordsSize];len = strlen(tmp);while(len--) {if (!hash[tmp[len] - 'a']) {break;}}if (len == -1) {res++;}}return res; }771. 寶石與石頭
int getIndx(char x) {return x - 'A' > 26 ? x - 'a' + 26 : x - 'A'; }int numJewelsInStones(char * jewels, char * stones){int numj = strlen(jewels);int nums = strlen(stones);int hash[54] = {0};int res = 0;while (nums--) {int idx = getIndx(stones[nums]);hash[idx]++;}while (numj--) {int idx = getIndx(jewels[numj]);res += hash[idx];hash[idx] = 0;}return res; }2011. 執(zhí)行操作后的變量值
int finalValueAfterOperations(char ** operations, int operationsSize){int res = 0;while (operationsSize--) {operations[operationsSize][1] == '-' ?res-- : res++;}return res; }劍指 Offer 58 - II. 左旋轉(zhuǎn)字符串
char* reverseLeftWords(char* s, int n){int len = strlen(s);char *res = (char *)malloc(sizeof(char) * (len + 1));res[len] = '\0';for (int i = 0; i < len - n; i++) {res[i] = s[n + i];}for (int i = 0; i < n; i++) {res[len - n + i] = s[i];}return res; }面試題 01.01. 判定字符是否唯一
面試題 01.02. 判定是否互為字符重排
383. 贖金信
537. 復(fù)數(shù)乘法
void getVal (char *num, int *real1, int *virt1) {int flag = 0;int real = 0;int virt = 0;int minus = 1;int len = strlen(num);for (int i = 0; i < len; i++) {if (num[i] == '+') {flag = 1;continue;}if (i > 0 && num[i] == '-') {flag = -1;continue;} else if (num[i] == '-'){minus = -1;continue;}if (num[i] == 'i') {break;}if (!flag) {real = real * 10 + (num[i] - '0');} else {virt = virt * 10 + (num[i] - '0');}}virt *=flag;real *=minus;printf("%d,%d", real, virt);*real1 = real;*virt1 = virt; }char * complexNumberMultiply(char * num1, char * num2){int real1 = 0;int real2 = 0;int real = 0;int virt1 = 0;int virt2 = 0;int virt = 0; getVal (num1, &real1, &virt1);getVal (num2,&real2, &virt2);real = real1 * real2 - virt1 * virt2;virt = real1 * virt2 + real2 * virt1;char *res = (char *)malloc(sizeof(char) * 20);sprintf(res, "%d+%di", real, virt);return res; }1108. IP 地址無效化
char * defangIPaddr(char * address){int len = strlen (address);int newLen = len + 6;char *res = (char *)malloc(sizeof(char) * newLen + 1);res[newLen] = '\0';while (len--) {if (address[len] == '.') {res[newLen-1] = ']';res[newLen-2] = '.';res[newLen-3] = '[';newLen -= 3;continue;}res[newLen - 1] = address[len];newLen--;}return res; }基礎(chǔ)數(shù)學(xué)
1925. 統(tǒng)計(jì)平方和三元組的數(shù)目
int countTriples(int n){int a = 2;int b = 3;int res = 0;for (a = 1; a < n; a++) {for (b = 1; b < n; b++) {int c = (int)sqrt(a * a + b * b + 1);if (c <= n && (c * c == a * a + b * b)) {res++;}}}return res; }2006. 差的絕對(duì)值為 K 的數(shù)對(duì)數(shù)目
int countKDifference(int* nums, int numsSize, int k){int res = 0;for (int i = 0; i < numsSize; i++) {for (int j = i + 1; j < numsSize; j++) {int judge = abs(nums[i] - nums[j]);if (judge == k) {res++;}}}return res; }1979. 找出數(shù)組的最大公約數(shù)
int findGCD(int* nums, int numsSize){int max = nums[0];int min = nums[0];int res = 1;for (int i = 1; i < numsSize; i++) {max = max > nums[i] ? max : nums[i];min = min < nums[i] ? min : nums[i];}return gcd (max, min); }1492. n 的第 k 個(gè)因子
int kthFactor(int n, int k){int *elem = (int*) malloc(sizeof(int)*k);int idx = 0;for(int i = 1; i <= n && idx < k; i++) {if (n % i == 0) {elem[idx++] = i;}}return idx < k ? -1 : elem[k-1]; }劍指 Offer 57 - II. 和為s的連續(xù)正數(shù)序列
int** findContinuousSequence(int target, int* returnSize, int** returnColumnSizes) {if (target < 2) {return NULL;}int maxSize = (target + 1) / 2;int** res = (int**)malloc(sizeof(int*) * maxSize);*returnColumnSizes = (int*)malloc(sizeof(int) * maxSize);int sizes = *returnColumnSizes;int cnt = 0;int i = 1;int j = i;int size = 1;int sum = 0;while (i < maxSize) {if (sum < target) {sum += j++;size++;}if (sum > target) {sum -= i++;size--;}if (sum == target) {(*returnColumnSizes)[cnt] = size - 1;res[cnt] = (int*)malloc(sizeof(int) * (size - 1));for (int z = 0; z < size - 1; z++) {res[cnt][z] = i + z;}cnt++;sum -= i++;size--;}}*returnSize = cnt;return res; }面試題 08.05. 遞歸乘法
void swap(int *A, int *B) {int t = *A;if (*A < *B) {*A = *B;*B = t;} }int multiply(int A, int B){int sum = 0;swap(&A, &B);while(B) {if (B & 1) {sum += A;}B >>= 1;if (B) A <<= 1;}return sum; }509. 斐波那契數(shù)
int fib(int n){int f0 = 0;int f1 = 1;int fn = f0 + f1;while (n > 1) {fn = f0 + f1;f0 = f1;f1 = fn;n--;}if (n < 1) return f0;return fn; }812. 最大三角形面積
double area(int* P, int* Q, int* R) {return 0.5 * abs(P[0] * Q[1] + Q[0] * R[1] + R[0] * P[1]-P[1] * Q[0] - Q[1] * R[0] - R[1] * P[0]); }double largestTriangleArea(int** points, int pointsSize, int* pointsColSize){double res = 0;for (int i= 0; i < pointsSize - 2; i++) {for (int j = i + 1; j < pointsSize - 1 ; j++) {for (int z = j + 1; z < pointsSize; z++) {double size = area (points[i], points[j], points[z]);res = res > size? res : size;}}}return res; }進(jìn)制理解與運(yùn)算
504. 七進(jìn)制數(shù)
char * convertToBase7(int num){int resDec = 0;int ifMinus = num >= 0 ? 0 : 1;num = abs(num);int cnt = 0;int cal = num;if (num == 0) {cnt = 1;}while (cal) {cal /= 7;cnt++;}cnt = cnt + ifMinus + 1;char *res = (char *) malloc(sizeof(char) * (cnt--));if (ifMinus) {res[0] = '-';}res[cnt--] = '\0';while (cnt >= ifMinus) {res[cnt--] = '0' + num % 7;printf("%d", cnt + 1);num /= 7;}return res; }405. 數(shù)字轉(zhuǎn)換為十六進(jìn)制數(shù)
char getHexChar(int num) {switch (num) {case 0:return '0';case 1:return '1';case 2:return '2';case 3:return '3';case 4:return '4';case 5:return '5';case 6:return '6';case 7:return '7';case 8:return '8';case 9:return '9';case 10:return 'a';case 11:return 'b';case 12:return 'c';case 13:return 'd';case 14:return 'e';case 15:return 'f';}return '0'; }char * toHex(int num){char *res = (char*)malloc(sizeof(char) * 9);unsigned int cal = (unsigned int) num;if (num == 0) {res[0] = '0';res[1] = '\0';return res;}int idx = 0;while (cal) {idx++;cal /= 16;}cal = (unsigned int) num;res[idx] = '\0';while (idx--) {res[idx] = getHexChar(cal % 16);cal /= 16;}return res; }1837. K 進(jìn)制表示下的各位數(shù)字總和
int sumBase(int n, int k){int res = 0;while (n) {res += n%k;n /= k;}return res; }190. 顛倒二進(jìn)制位
uint32_t reverseBits(uint32_t n) {uint32_t res = 0;uint32_t t = (uint32_t)(-1);while (t) {res = (res << 1) + (n & 1);n >>= 1;t >>= 1;}return res; }258. 各位相加
int addDigits(int num){int res = 0;while (num) {res += num % 10;num = num / 10;if (!num && res / 10) {num = res;res = 0;}}return res; }理解數(shù)組
860. 檸檬水找零
1512. 好數(shù)對(duì)的數(shù)目
int numIdenticalPairs(int* nums, int numsSize){int res = 0;for (int i = 0; i < numsSize; i++) {for (int j = i + 1; j < numsSize; j++) {if (nums[i] == nums[j]) {res++;}}}return res; }217. 存在重復(fù)元素
int cmp(int *a, int *b){return *a - *b; }bool containsDuplicate(int* nums, int numsSize){qsort(nums, numsSize, sizeof(int), cmp);for(int i = 0; i < numsSize - 1; i++) {if (nums[i] == nums[i + 1]) {return true;}}return false; }27. 移除元素
int removeElement(int* nums, int numsSize, int val){int newIdx = 0;int idx = 0;for(; idx < numsSize; idx++) {if (nums[idx] == val) {continue;}if (nums[idx] != val) {nums[newIdx++] = nums[idx];}}return newIdx; }26. 刪除有序數(shù)組中的重復(fù)項(xiàng)
int removeDuplicates(int* nums, int numsSize){int fast = 1;int slow = 0;if(numsSize <= 1) {return numsSize;}for (; fast < numsSize; fast++) {if (nums[fast] != nums[fast -1]) {nums[slow + 1] = nums[fast];slow++;}}return slow + 1; }1863. 找出所有子集的異或總和再求和
int subsetXORSum(int* nums, int numsSize){int res = 0;int xor = 0;for (int i = 0; i < numsSize; i++){res += nums[i];xor ^= nums[i];}res += xor;for (int i = 0; i < numsSize; i++) {res += xor ^ nums[i];}return res; }1365. 有多少小于當(dāng)前數(shù)字的數(shù)字
/*** Note: The returned array must be malloced, assume caller calls free().*/ int* smallerNumbersThanCurrent(int* nums, int numsSize, int* returnSize){int *res = (int *)malloc(sizeof(int) * numsSize);memset(res, 0, sizeof(int) * numsSize);*returnSize = numsSize;int hash[101] = {0};for (int i = 0; i < numsSize; i++) {hash[nums[i]]++;}for (int i = 0; i < numsSize; i++) {int val = nums[i];while (val--) {res[i] += hash[val];}}return res; }1929. 數(shù)組串聯(lián)
/*** Note: The returned array must be malloced, assume caller calls free().*/ int* getConcatenation(int* nums, int numsSize, int* returnSize){int len = numsSize;*returnSize = 2 * len;int *res = (int *) malloc(sizeof(int) * 2 * len);while(len--) {res[len] = nums[len];res[len + numsSize] = nums[len];}return res; }1464. 數(shù)組中兩元素的最大乘積
int maxProduct(int* nums, int numsSize){int biggest;int second;int i;if (numsSize < 2) {return 0;}biggest = nums[0] > nums[1] ? nums[0] : nums[1];second = nums[0] > nums[1] ? nums[1] : nums[0];for (i = 2; i < numsSize ; i++) {if (nums[i] > biggest) {second = biggest;biggest = nums[i];} else if (nums[i] > second) {second = nums[i];}}return (biggest-1) * (second-1); }414. 第三大的數(shù)
//堆排序 void swap(int *nums,int i,int j){int t=nums[i];nums[i]=nums[j];nums[j]=t; } void MaxHeapify(int *nums,int index,int numsSize){ //維護(hù)大根堆int lchild=2*index,rchild=2*index+1;int max=index; if(lchild<numsSize&&nums[lchild]>nums[max])max=lchild;if(rchild<numsSize&&nums[rchild]>nums[max])max=rchild;if(index!=max){swap(nums,index,max);MaxHeapify(nums,max,numsSize);} } void MaxHeap(int *nums,int numsSize){ //建堆for(int i=numsSize/2;i>=0;i--){MaxHeapify(nums,i,numsSize);} } int thirdMax(int* nums, int numsSize){MaxHeap(nums,numsSize); //建堆int count=1,ans=nums[0],size=numsSize;int max=ans;while(count<3){swap(nums,0,size-1);--size;if(size<1)return max; //不夠3次MaxHeapify(nums,0,size); //維護(hù)大根堆if(ans!=nums[0]){++count;ans=nums[0];}else{continue;}}return nums[0]; }鏈表の本質(zhì)
面試題 02.03. 刪除中間節(jié)點(diǎn)
/*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/ void deleteNode(struct ListNode* node) {struct ListNode* del = node->next;node->val = node->next->val;node->next = node->next->next;free(del); }劍指 Offer 18. 刪除鏈表的節(jié)點(diǎn)
/*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/struct ListNode* deleteNode(struct ListNode* head, int val){struct ListNode *dumHead = (struct ListNode *)malloc(sizeof(struct ListNode));dumHead->next = head;struct ListNode *pre = head;struct ListNode *later = dumHead;while (pre) {if (pre->val == val) {later->next = pre->next;break;}pre = pre->next;later = later->next;}free(pre);return dumHead->next; }樹の遞歸理解
144. 二叉樹的前序遍歷
94. 二叉樹的中序遍歷
589. N 叉樹的前序遍歷
LCP 44. 開幕式焰火
/*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*/ int dfs (int res, int * hash, struct TreeNode* root) {if (!root) {return res;}if (hash[root->val] == 0) {//printf("%d ", hash[root->val]);hash[root->val] = 1;++res;}res = dfs(res, hash, root -> left);res = dfs(res, hash, root -> right);return res; }int numColor(struct TreeNode* root){ int res = 0;int hash[1001] = {0};res = dfs(res, hash, root);return res; }劍指 Offer 27. 二叉樹的鏡像
劍指 Offer 55 - I. 二叉樹的深度
/*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*/int maxDepth(struct TreeNode* root){if (!root) {return 0;}return fmax(maxDepth(root -> left), maxDepth(root -> right)) + 1; }劍指 Offer 55 - II. 平衡二叉樹
基礎(chǔ)算法&思維
查找
劍指 Offer 04. 二維數(shù)組中的查找
33. 搜索旋轉(zhuǎn)排序數(shù)組
int search(int* nums, int numsSize, int target){int res;int left = 0;int right = numsSize - 1;int mid = (left + right) / 2;while (left <= right) {printf ("%d, %d, %d\n", left, mid, right);if (nums[mid] == target) {return mid;}if (nums[left] == target) {return left;}if (nums[right] == target) {return right;}// left is a sorted arrayif (nums[left] < nums[mid]) {if (nums[mid] < target) {left = mid + 1;right -=1;} else {left += 1;right -=1;}} else {// right is sortedif (nums[mid] > target) {left += 1;right = mid - 1;} else {left += 1;right -=1;}}mid = (left + right) / 2;}return -1; }153. 尋找旋轉(zhuǎn)排序數(shù)組中的最小值
int findMin(int* nums, int numsSize){int left = 0;int right = numsSize - 1;int mid = (left + right) / 2;do {///< when the graph is like ^Vif (nums[left] > nums[right]) {///< narrow the border, follow the trendleft = nums[mid] > nums[left] ? mid : left;right = nums[mid] < nums[right] ? mid : right;mid = (left + right) / 2;} else { ///< is like Areturn nums[left];} } while (1 != (right - left)) ;///< is like V return nums[right]; } int search(int* nums, int numsSize, int target){int left = 0;int right = numsSize - 1;int mid = (left + right) / 2;if (numsSize == 1) {return (nums[0] == target) ? 0 : -1;}while (left <= right) {if (nums[mid] == target) {return mid;}if (nums[left] == target) {return left;}if (nums[right] == target) {return right;}if (nums[left] < nums[mid]) {if (nums[left] < target && nums[mid] > target) {left = left + 1;right = mid - 1;} else {left = mid + 1;right = right - 1;}} else {if (nums[mid + 1] <= target && nums[right] > target) {left = mid + 1;right = right - 1;} else {left = left + 1;right = mid - 1;}}mid = (left + right) / 2;}return -1; }1302. 層數(shù)最深葉子節(jié)點(diǎn)的和
/*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*/ //int sum; //int depth; void dfs(struct TreeNode* root, int cur_depth, int max, int *sum) {if (root == NULL){return;}if ((root->left == NULL) && (root->right == NULL) && (cur_depth == max)) {*sum += root->val;}dfs(root->left, cur_depth + 1, max, sum);dfs(root->right, cur_depth + 1, max, sum); } int maxDepth(struct TreeNode* root) {if (!root){return 0; }return 1 + fmax(maxDepth(root->left), maxDepth(root->right)); }int deepestLeavesSum(struct TreeNode* root){int sum = 0;int max = maxDepth(root);dfs(root, 1, max, &sum);return sum; }劍指 Offer 11. 旋轉(zhuǎn)數(shù)組的最小數(shù)字
int minArray(int* numbers, int numbersSize){int low = 0;int high = numbersSize - 1;while (low < high) {int pivot = low + (high - low) / 2;if (numbers[pivot] < numbers[high]) {high = pivot;} else if (numbers[pivot] > numbers[high]) {low = pivot + 1;} else {high -= 1;}}return numbers[low]; }41. 缺失的第一個(gè)正數(shù)
int firstMissingPositive(int* nums, int numsSize){for (int i = 0; i < numsSize; i++) {if (nums[i] <= 0) {nums[i] = numsSize + 1;}}for (int i = 0; i < numsSize; i++) {if (abs(nums[i]) <= numsSize) {if (nums[abs(nums[i]) - 1] > 0)nums[abs(nums[i]) - 1] *= -1;}}for (int i = 0; i < numsSize; i++) {if(nums[i] > 0) {return i + 1;} }return numsSize + 1; }154. 尋找旋轉(zhuǎn)排序數(shù)組中的最小值 II
int findMin(int* nums, int numsSize){int left = 0;int right = numsSize - 1;while (right - left > 1) {int mid = (left + right) / 2;if (nums[left] > nums[right]) {left = nums[mid] >= nums[left] ? mid : left;right = nums[mid] <= nums[right] ? mid : right;} else if (nums[left] == nums[right]) {right--; ///< only deselect one} else {return nums[left];}}return nums[left] < nums[right] ? nums[left] : nums[right]; }排序
451. 根據(jù)字符出現(xiàn)頻率排序
int getIdx(char x) {if (x - 'A' >= 0){return x - 'A' > 26 ? x - 'a' + 26 : x - 'A';} else {return x - '0' + 52;}} char idx2char(int idx) {return idx < 26 ? (char)(idx + 'A') : idx < 52 ? (char)(idx - 26 + 'a') : (char)(idx - 52 + '0'); } int cmp(const void *a, const void *b) {if (((int *)b)[1] != ((int *)a)[1]) {return ((int *)b)[1] - ((int *)a)[1];} else {return ((int *)a)[0] - ((int *)a)[0];} }char * frequencySort(char * s){int len = strlen(s);char *res = (char *)malloc(sizeof(char) * (len + 1));int hash[62] = {0};while (len--) {hash[getIdx(s[len])]++;}int tmp[62][2] = {0};for (int i = 0; i < 62; i++) {int t = hash[i];tmp[i][0] = i;tmp[i][1] = t;//printf("%d ", t);}qsort(tmp, 62, sizeof(tmp[0]), cmp);len = 0;for (int i = 0; i < 62; i++) {int t = tmp[i][1];if (t == 0) {break;}while (t--) {res[len++] = idx2char(tmp[i][0]);}}res[len] = '\0';return res; }912. 排序數(shù)組
/*** Note: The returned array must be malloced, assume caller calls free().*/int cp(int *a, int *b) {return *a - *b; } int* sortArray(int* nums, int numsSize, int* returnSize){int *res = (int*) malloc (sizeof(int) * numsSize);*returnSize = numsSize;memcpy(res,nums,sizeof(int)*numsSize);qsort(res,numsSize,sizeof(int),cp);return res; }貪心
121. 買賣股票的最佳時(shí)機(jī)
int maxProfit(int* prices, int pricesSize){int minPrice = 1e9;int maxPro = 0;for (int i = 0; i < pricesSize; i++){maxPro = maxPro > (prices[i] - minPrice) ? maxPro : (prices[i] - minPrice);minPrice = prices[i] < minPrice ? prices[i] : minPrice;}return maxPro; }還差得遠(yuǎn)呢!
關(guān)于之前刷題沒有及時(shí)復(fù)習(xí)導(dǎo)致重新再看沒有什么思路,以及群里一些同學(xué)發(fā)的稍微復(fù)雜一點(diǎn)的問題根本毫無思路的事。。。慢慢重刷ing
之前的題
659. 分割數(shù)組為連續(xù)子序列
322. 零錢兌換
204. 計(jì)數(shù)質(zhì)數(shù)
169. 多數(shù)元素
98. 驗(yàn)證二叉搜索樹
2. 兩數(shù)相加
3. 無重復(fù)字符的最長(zhǎng)子串
7. 整數(shù)反轉(zhuǎn)
11. 盛最多水的容器
二分法
區(qū)間位移-藍(lán)橋杯
數(shù)軸上有n個(gè)閉區(qū)間D1,…,Dn。其中區(qū)間Di用一對(duì)整數(shù)[ai, bi]來描述,滿足ai < bi。
已知這些區(qū)間的長(zhǎng)度之和至少有10000。所以,通過適當(dāng)?shù)囊苿?dòng)這些區(qū)間,你總可以使得他們的“并”覆蓋[0, 10000]——也就是說[0, 10000]這個(gè)區(qū)間內(nèi)的每一個(gè)點(diǎn)都落于至少一個(gè)區(qū)間內(nèi)。
你希望找一個(gè)移動(dòng)方法,使得位移差最大的那個(gè)區(qū)間的位移量最小。
具體來說,假設(shè)你將Di移動(dòng)到[ai+ci, bi+ci]這個(gè)位置。你希望使得max |ci| 最小。
題解
枚舉
n選m個(gè)的全排列
從n個(gè)當(dāng)中選m個(gè),有多少種排列呢? 請(qǐng)全輸出
輸入格式
輸入n,m(1≤m≤n≤5)
輸出格式
所有可能的排列,字典序
輸入輸出樣例
輸入
輸出
12 13 21 23 31 32題解
數(shù)據(jù)的有效數(shù)字
eor&(~eor+1)到lowbit到樹狀數(shù)組
總結(jié)
以上是生活随笔為你收集整理的万人千题第一阶段报告【待继续总结】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一文详解 OpenGL ES 纹理颜色混
- 下一篇: 漫漫秋招路