【leetcode】格雷编码 背下来即可
class Solution { public: vector<int> grayCode(int n) { vector<int> ans; for(int i=0;i<1<<n;i++) { ans.push_back(i^(i>>1)); } return ans; } };
【复习数组和字符串】
寻找中间的索引
class Solution { public: int pivotIndex(vector<int>& nums) { if(nums.size()==0)return -1; int sum = 0; for(int i=0;i<nums.size();i++) sum += nums[i]; int leftsum = 0; for(int i=0;i<nums.size();i++) { if(leftsum*2+nums[i] == sum) return i; leftsum += nums[i]; } return -1; } };
二分寻找插入的位置
class Solution { public: int searchInsert(vector<int>& nums, int target) { int left = 0; int right = nums.size(); while(left<right) { int mid = (left+right)/2 ; if(nums[mid]<target) { left = mid+1; } else if(nums[mid]>target) { right = mid; } else if(nums[mid] == target) { return mid; } } return left; } };
合并区间
class Solution { public: static bool cmp(vector<int>a,vector<int>b) { return (a[0]<b[0]); } vector<vector<int>> merge(vector<vector<int>>& intervals) { vector<vector<int>>ans; sort(intervals.begin(),intervals.end(),cmp); /* for(int i=0;i<intervals.size();i++) { for(int j=0;j<intervals[i].size();j++) { printf("%d ",intervals[i][j]); } printf("\n"); } */ if(intervals.size()==0) return ans; int left = intervals[0][0]; int right = intervals[0][1]; for(int i=0;i<intervals.size();i++) { if(intervals[i][0]<=right) { if(right<intervals[i][1]) right = intervals[i][1]; } else if(intervals[i][0]>right ) { vector<int> tmp = {left,right}; ans.push_back(tmp); left = intervals[i][0]; right = intervals[i][1]; } if(i == intervals.size()-1) { vector<int> tmp = {left,right}; ans.push_back(tmp); } } return ans; } };
零矩阵
class Solution { public: void setZeroes(vector<vector<int>>& matrix) { if(matrix.size()==0)return; int rows[matrix.size()]; int cols[matrix[0].size()]; memset(rows,0,sizeof(rows)); memset(cols,0,sizeof(cols)); for(int i=0;i<matrix.size();i++) { for(int j=0;j<matrix[i].size();j++) { if(matrix[i][j] == 0) { rows[i] = 1; cols[j] = 1; } } } int m = matrix.size(); int n = matrix[0].size(); for(int i=0;i<matrix.size();i++) { if(rows[i] == 1) for(int j=0;j<matrix[i].size();j++) { matrix[i][j] = 0; } } for(int j=0;j<n;j++) { if(cols[j] == 1) { for(int i=0;i<m;i++) { matrix[i][j] = 0; } } } } };
对角线遍历
要注意判断边界条件的优先级
class Solution { public: vector<int> findDiagonalOrder(vector<vector<int>>& matrix) { vector<int> ans; int m = matrix.size(); if(m==0)return ans; int n = matrix[0].size(); int ccount = m*n; int x = 0; int y = 0; while(ccount--) { ans.push_back(matrix[x][y]); printf("%d %d\n",x,y); if(x == m-1&&((x+y)%2==1)) { y++; } else if((y ==0&&(x+y)%2==1)) { x++; } else if(y == n-1&&(x+y)%2==0) { x++; } else if(x == 0&&((x+y)%2==0)) { y++; } else if((x+y)%2==0) { x--; y++; } else if((x+y)%2==1) { x++; y--; } } return ans; } };
字符串最长公共前缀
class Solution { public: string longestCommonPrefix(vector<string>& strs) { string ans; if(strs.size() == 0) return ""; for(int j=0;j<strs[0].length();j++) { char tmp = strs[0][j]; int is = 1; for(int i=0;i<strs.size();i++) { if(strs[i][j]!=tmp) { is = 0; break; } } if(is) ans.push_back(tmp); else break; } return ans; } };
最长回文子串
动态规划
class Solution { public: string longestPalindrome(string s) { int n = s.length(); if(n<=1)return s; string ans; int dp[n][n]; int maxlen = -1; for(int i=0;i<n;i++) { for(int j=0;j<=i;j++) { if(s[j] == s[i]) { if(i-j>=3) { dp[j][i] = dp[j+1][i-1]; } else dp[j][i] = 1; } else { dp[j][i] = 0; } if(dp[j][i] == 1&&i-j+1>maxlen) { maxlen = i-j+1; ans = s.substr(j,maxlen); printf("%d %d\n",j,i); } } } return ans; } };
翻转句子里的单词
要求使用空间为o(1)
使用stl的reverse函数
class Solution { public: string reverseWords(string s) { reverse(s.begin(),s.end()); int left,right; for(int i=0;i<s.length();i++) { if(s[i] == ' ') continue; if(isalpha(s[i])||isdigit(s[i])) { left = i; i++; while(isalpha(s[i])||isdigit(s[i])) { i++; } right = i; reverse(&s[left],&s[right]); } } int pos = 0; int word = 0; for(int i=0;i<s.length();i++) { if((s[i] == ' '&&pos == 0)||(s[i] == ' '&&word == 0)) { continue; } else if(isalpha(s[i])||isdigit(s[i])) { word = 1; s[pos++] = s[i]; continue; } else if(s[i] == ' '&&word ==1) { s[pos++] = s[i]; word = 0; } } if(s[pos-1]==' ') pos--; s = s.substr(0,pos); return s; } };