EASY:
242. Valid Anagram
https://leetcode.com/problems/valid-anagram/
这题算是一个技巧吧,一定要记住这种方法的,后面的题经常会用到这种方法。我知道很多人觉得先将两个字符串sort之后再比较会简单一点,但毕竟时间复杂度会高很多,知道时间复杂度低的方法是必要的。
class Solution { public: bool isAnagram(string s, string t) { if(s.length()!=t.length()) return false; map<char,int> hash_s,hash_t; int cnt=0; for(int i=0;i<s.length();i++){ char a = s[i], b = t[i]; if(hash_t[a]>0){ hash_t[a]--; cnt--; } else{ hash_s[a]++; cnt++; } if(hash_s[b]>0){ hash_s[b]--; cnt--; } else{ hash_t[b]++; cnt++; } } return cnt==0; } };204. Count Primes
https://leetcode.com/problems/count-primes/
这题我之前试过用普通的方法,从2开始遍历到n,逐个判断是否是素数。时间上超时的。下面的方法其实不难理解,但是很有必要记住。
class Solution { public: int countPrimes(int n) { int cnt=0; vector<int>tag(n,0); for(int i=2;i<n;i++){ if(tag[i]==0){ cnt++; int t=2; while(t*i<n){ tag[t*i]=1; t++; } } } return cnt; } };202. Happy Number
https://leetcode.com/problems/happy-number/
很简单的一道题,不知道为什么难度排在前面两题之上。
class Solution { public: bool isHappy(int n) { map<int,int>hash; while(hash.find(n)==hash.end()){ hash[n]=1; n = next(n); if(n==1) return true; } return false; } int next(int n){ int re=0; while(n){ int t = n%10; re+=t*t; n/=10; } return re; } };
看了我的方法一定觉得很眼熟。
class Solution { public: string getHint(string secret, string guess) { map<char,int>hash; int len = secret.length(); int bull=0, cow=0; for(int i=0;i<len;i++){ char a = secret[i], b = guess[i]; if(a==b) bull++; else{ if(hash[a]<0) cow++; hash[a]++; if(hash[b]>0) cow++; hash[b]--; } } string s; s+= to_string(bull)+'A'+to_string(cow)+'B'; return s; } };
36. Valid Sudoku
https://leetcode.com/problems/valid-sudoku/
看似复杂,其实还是很简单的,只要用数组来标识就ok了。
<span style="font-size:14px;">class Solution { public: bool isValidSudoku(vector<vector<char>>& board) { int tag[9]; for(int i=0;i<9;i++){ memset(tag,0,sizeof(tag)); for(int j=0;j<9;j++){ if(board[i][j]=='.')continue; else if(tag[board[i][j]-'1']==0) tag[board[i][j]-'1']=1; else return false; } } for(int i=0;i<9;i++){ memset(tag,0,sizeof(tag)); for(int j=0;j<9;j++){ if(board[j][i]=='.')continue; else if(tag[board[j][i]-'1']==0) tag[board[j][i]-'1']=1; else return false; } } for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ memset(tag,0,sizeof(tag)); for(int m=0;m<3;m++) for(int n=0;n<3;n++){ if(board[i*3+m][j*3+n]=='.')continue; else if(tag[board[i*3+m][j*3+n]-'1']==0) tag[board[i*3+m][j*3+n]-'1']=1; else return false; } } } return true; } };</span>205. Isomorphic Strings
https://leetcode.com/problems/isomorphic-strings/
这题,其实最初的时候,我的想法是建立char到char的map,但是后来发现用char到int的map要更简单。
<span style="font-size:14px;">class Solution { public: bool isIsomorphic(string s, string t) { map<char,int>s2i,t2i; int len = s.length(),i; for(i=0; i<t.length(); i++){ if(i>=len || s2i[s[i]]!=t2i[t[i]])return false; s2i[s[i]] = t2i[t[i]] = i+1; } return i==len; } };</span>217. Contains Duplicate
https://leetcode.com/problems/contains-duplicate/
此题在array中已经提到过了。
<span style="font-size:14px;">class Solution { public: bool containsDuplicate(vector<int>& nums) { map<int,int>hash; int len = nums.size(); for(int i=0;i<len;i++){ if(hash.find(nums[i])==hash.end()) hash[nums[i]]=1; else return true; } return false; } };</span>219. Contains Duplicate II
https://leetcode.com/problems/contains-duplicate-ii/
<span style="font-size:14px;">class Solution { public: bool containsNearbyDuplicate(vector<int>& nums, int k) { map<int,int>hash; int len = nums.size(); for(int i=0;i<len;i++){ if(hash.find(nums[i])==hash.end()){ hash[nums[i]]=i; } else{ if(i-hash[nums[i]]<=k) return true; else hash[nums[i]]=i; } } return false; } };</span>290. Word Pattern
https://leetcode.com/problems/word-pattern/
class Solution { public: bool wordPattern(string pattern, string str) { int len = (int)pattern.size(), i = 0; map<char,int>p2i; map<string, int>s2i; istringstream st(str); for (string s; st>>s; i++) { if(i>=len)return false; if(p2i.find(pattern[i])==p2i.end() && s2i.find(s)==s2i.end()) p2i[pattern[i]]=s2i[s]=i+1; else if(p2i[pattern[i]]!=s2i[s]) return false; } return i==len; } };1. Two Sum
https://leetcode.com/problems/two-sum/
<span style="font-size:14px;">class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { int len = nums.size(); vector<int>re; map<int,vector<int>> hash; for(int i=0;i<len;i++){ hash[nums[i]].push_back(i); } sort(nums.begin(),nums.end()); int i=0,j=len-1; while(i<j){ if(nums[i]+nums[j]<target) i++; else if(nums[i]+nums[j]>target)j--; else{ re.push_back(hash[nums[i]][0]); hash[nums[i]].erase(hash[nums[i]].begin()); re.push_back(hash[nums[j]][0]); return re; } } return re; } };</span>