leetcode 350 easy
Posted 热之雪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode 350 easy相关的知识,希望对你有一定的参考价值。
350. Intersection of Two Arrays II
class Solution { public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { unordered_map<int, int> dict; vector<int> res; for(int i = 0; i < (int)nums1.size(); i++) dict[nums1[i]]++; for(int i = 0; i < (int)nums2.size(); i++) if(dict.find(nums2[i]) != dict.end() && --dict[nums2[i]] >= 0) res.push_back(nums2[i]); return res; } };
345. Reverse Vowels of a String
class Solution { public: string reverseVowels(string s) { int i = 0, j = s.size() - 1; while (i < j) { i = s.find_first_of("aeiouAEIOU", i); j = s.find_last_of("aeiouAEIOU", j); if (i < j) { swap(s[i++], s[j--]); } } return s; } };
387. First Unique Character in a String
Brute force solution, traverse string s 2 times. First time, store counts of every character into the hash table, second time, find the first character that appears only once. //遍历两次string class Solution { public: int firstUniqChar(string s) { unordered_map<char, int> m; for (auto &c : s) { m[c]++; } for (int i = 0; i < s.size(); i++) { if (m[s[i]] == 1) return i; } return -1; } }; if the string is extremely long, we wouldn‘t want to traverse it twice, so instead only storing just counts of a char, we also store the index, and then traverse the hash table. //遍历一次string和一次map class Solution { public: int firstUniqChar(string s) { unordered_map<char, pair<int, int>> m; int idx = s.size(); for (int i = 0; i < s.size(); i++) { m[s[i]].first++; m[s[i]].second = i; } for (auto &p : m) { if (p.second.first == 1) idx = min(idx, p.second.second); } return idx == s.size() ? -1 : idx; } };
409. Longest Palindrome
Python: def longestPalindrome(self, s): odds = sum(v & 1 for v in collections.Counter(s).values()) return len(s) - odds + bool(odds)
C++: int longestPalindrome(string s) { int odds = 0; for (char c=‘A‘; c<=‘z‘; c++) odds += count(s.begin(), s.end(), c) & 1; //如果是奇数则加1,偶数不加 return s.size() - odds + (odds > 0); }
412. Fizz Buzz
class Solution { public: vector<string> fizzBuzz(int n) { vector<string> ret_vec(n); for(int i=1; i<=n; ++i) { if(0 == i%3) { ret_vec[i-1] += "Fizz"; } if(0 == i%5) { ret_vec[i-1] += "Buzz"; } if(ret_vec[i-1].empty()) { ret_vec[i-1] += to_string(i); } } return ret_vec; } };
414. Third Maximum Number
class Solution { public: int thirdMax(vector<int>& nums) { set<int> top3; for (int num : nums) { top3.insert(num); if (top3.size() > 3) top3.erase(top3.begin()); } return top3.size() == 3 ? *top3.begin() : *top3.rbegin(); } };
415. Add Strings
class Solution { public: string addStrings(string num1, string num2) { int i = num1.size() - 1; int j = num2.size() - 1; int carry = 0; string res = ""; while(i>=0 || j>=0 || carry){ long sum = 0; if(i >= 0){sum += (num1[i] - ‘0‘);i--;} if(j >= 0){sum += (num2[j] - ‘0‘);j--;} sum += carry; carry = sum / 10; sum = sum % 10; res = res + to_string(sum); } reverse(res.begin(), res.end()); return res; } };
437. Path Sum III
class Solution { public: int pathSum(TreeNode* root, int sum) { if(!root) return 0; return dfs(root,0,sum)+pathSum(root->left,sum)+pathSum(root->right,sum); } int dfs(TreeNode* root,int pre,int sum) { if(!root) return 0; int cur=pre+root->val; return (cur==sum)+dfs(root->left,cur,sum)+dfs(root->right,cur,sum); } };
438. Find All Anagrams in a String
class Solution { public: vector<int> findAnagrams(string s, string p) { vector<int> pv(256,0), sv(256,0), res; if(s.size() < p.size()) return res; for(int i = 0; i < p.size(); ++i) { ++pv[p[i]]; ++sv[s[i]]; } if(pv == sv) res.push_back(0); for(int i = p.size(); i < s.size(); ++i) { ++sv[s[i]]; --sv[s[i-p.size()]]; if(pv == sv) res.push_back(i-p.size()+1); } return res; } };
以上是关于leetcode 350 easy的主要内容,如果未能解决你的问题,请参考以下文章
Leetcode Binary Search Conclusion
白菜刷LeetCode记-350. Intersection of Two Arrays II
leetcode350. Intersection of Two Arrays II
[TIA PORTAL][CONVERT] Convert Char Array to DInt...DInt to Char Array..Useful and easy function(代码片段