刷题时用到的技巧
1、使用memset进行初始化 memset(a,0,sizeof(a)); memset(a,-1,sizeof(a));
2、合理使用sort函数,sort(a,a+n),sort(v.begin(),v.end());
3、 取中值需要小心越界,int mid = l + ((r - l) >> 1);
4、vector删除最一个pop_back
5、vector<pair<int,int>>;
6、vector声明一个二位数值 vector<vecotr<int>> vc(n,vector<int>(m));
7、reverse 反转列表
8、字符转字符串 string(1,char) ,使用string构造
9、substr(pos,count);
0034
在排序数组中查找元素的第一个和最后一个位置
需要结束的位置的时候,使用二分法需要 iMid = (iEnd + iHiegh + 1) / 2; 不加1会死循环
class Solution { public: vector<int> searchRange(vector<int>& nums, int target) { int iLow = 0; int iHiegh = nums.size()-1; int iK = -1; int iStart = -1,iEnd = -1; while(iLow <= iHiegh) { int iMid = (iLow + iHiegh) / 2; if(nums[iMid] > target) { iHiegh = iMid - 1; } else if(nums[iMid] < target) { iLow = iMid + 1; } else { iStart = iMid; iEnd = iMid; while(iLow < iStart) { iMid = (iLow + iStart) / 2; if(nums[iMid] != nums[iStart]) { iLow = iMid + 1; } else { iStart = iMid; } } while(iEnd < iHiegh) { iMid = (iEnd + iHiegh + 1) / 2; //这个地方不加1,会死循环 if(nums[iMid] != nums[iEnd]) { iHiegh = iMid - 1; } else { iEnd = iMid; } } break; } } return vector<int>{iStart,iEnd}; } };
0038 外观数列
不知道为啥 itoa 不能用,只能用 to_string()
class Solution { private: string GetNextString(const string& str) { string strRes = ""; int iLen = str.length(); for(int i=0;i<iLen;) { char c = str[i]; int j = 1; while(i+j < iLen) { if(str[i+j] != c) { break; } ++j; } i += j; strRes += to_string(j); //itoa,居然会报错 strRes += c; } return strRes; } public: string countAndSay(int n) { string str = "1"; for(int i=1;i<n;++i) { str = GetNextString(str); } return str; } };
0040 组合总和
class Solution { vector<int> vcTemp; vector<vector<int>> vcRes; void dfs(vector<pair<int,int>>& vcpair,int target,int cur,const int& n) { if(target==0) { vcRes.push_back(vcTemp); return; } if(cur >= n || target < 0) { return; } dfs(vcpair,target,cur+1,n); int max = min(target / vcpair[cur].first,vcpair[cur].second); for(int i=1;i<=max;++i) { vcTemp.push_back(vcpair[cur].first); dfs(vcpair,target-vcpair[cur].first * i,cur+1,n); } for(int i=1;i<=max;++i) { vcTemp.pop_back(); } } public: vector<vector<int>> combinationSum2(vector<int>& candidates, int target) { sort(candidates.begin(),candidates.end()); vector<pair<int,int>> vcpair{(31,1)}; for(int i=candidates.size()-1;i>=0;--i) { if(vcpair.back().first != candidates[i]) { vcpair.push_back(pair<int,int>(candidates[i],1)); } else { ++vcpair.back().second; } } int n = vcpair.size(); dfs(vcpair,target,1,n); return vcRes; } };
0046 全排列
C++中有函数 next_permutation
class Solution { public: vector<vector<int>> permute(vector<int>& nums) { vector<vector<int>> vcRes; sort(nums.begin(),nums.end()); do { vcRes.push_back(nums); }while(next_permutation(nums.begin(),nums.end())); return vcRes; } };
或者是DFS
class Solution { private: void dfs(vector<int>& nums,int iCur,vector<int>& res) { if(iCur == m_iLen) { m_vcRes.push_back(res); } for(int i=0;i<m_iLen;++i) { if(!Hash[i]) { res.push_back(nums[i]); Hash[i] = 1; dfs(nums,iCur+1,res); res.pop_back(); Hash[i] = 0; } } } public: vector<vector<int>> permute(vector<int>& nums) { memset(Hash,0,sizeof(Hash)); m_iLen = nums.size(); vector<int> temp; dfs(nums,0,temp); return m_vcRes; } private: vector<vector<int>> m_vcRes; int Hash[7]; int m_iLen; };
0047 全排列2
直接使用C++函数 next_permutation
class Solution { public: vector<vector<int>> permuteUnique(vector<int>& nums) { sort(nums.begin(),nums.end()); vector<vector<int>> vcRes; do { vcRes.push_back(nums); }while(next_permutation(nums.begin(),nums.end())); return vcRes; } };
0050
Pow(x, n)
可以考虑n的二进制表现形式
注意点:考虑n为负数,然后变成正数的时候越界情况。
class Solution { public: double myPow(double x, int n) { int64_t N = n; x = N > 0 ? x : 1.0 / x; N = N > 0 ? N : -N; double res = 1; while(N) { if(N & 1) { res *= x; } x *= x; N >>= 1; } return res; } };
0056 合并区间
注意用力 [[1,4],[2,3]]
class Solution { public: vector<vector<int>> merge(vector<vector<int>>& intervals) { auto comp = [](vector<int>& a,vector<int>& b) -> bool { if(a[0] == b[0]) { if(a[1] == b[1]) return false; return a[1] < b[1]; } return a[0] < b[0]; }; sort(intervals.begin(),intervals.end(),comp); vector<vector<int>> res; //加入哨兵 intervals.push_back(vector<int>{10001,10001}); int len = intervals.size(); vector<int> temp = intervals[0]; for(int i=1;i<len;++i) { if(temp[1] < intervals[i][0]) { res.push_back(temp); temp = intervals[i]; } else { temp[1] = max(temp[1],intervals[i][1]); } } return res; } };