14. Longest Common Prefix Total Accepted: 141524 Total Submissions: 464593 Difficulty: Easy Contributors: Admin
Write a function to find the longest common prefix string amongst an array of strings.
這道題知道Common Prefix的意思是共同子串就可以了…………就是很多個string擁有的最長的共同首碼。(就是最長前面幾個一樣……)
-----------------------------------------------最簡單無腦的迴圈兩次就很快了---------------------------------------------------
class Solution {public:string longestCommonPrefix(vector<string>& strs) {//題目的意思是返回共同首碼,很多個string裡最長的首碼…string result = "";if (strs.size() == 0)return result;for (int i = 0; i < strs[0].size(); i++) {bool goon = true;for (int j = 0; j < strs.size(); j++) {if (strs[j][i] != strs[0][i])goon = false;}if (!goon)break;else result += strs[0][i];}return result;}};
15. 3Sum Add to List Question Editorial Solution My Submissions Total Accepted: 170831 Total Submissions: 821476 Difficulty: Medium Contributors: Admin
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note: The solution set must not contain duplicate triplets.
For example, given array S = [-1, 0, 1, 2, -1, -4],A solution set is:[ [-1, 0, 1], [-1, -1, 2]]
---------------------------------------O(N^2)的解決方案------------------------------------------------------------
//感覺自己寫的好冗雜,但好歹通過檢測了,花了好幾個小時…
class Solution {public:vector<vector<int>> threeSum(vector<int>& nums) {//先試試O(n^2)vector<vector<int>> result;if (nums.size() == 0) { return result; }if(nums[0]==0&&nums[1]==0&&nums[2]==0&&nums.size()>2){ vector<int>temp; temp.push_back(0); temp.push_back(0); temp.push_back(0);result.push_back(temp); // return result;}map<int, int>mapping;//insert是直接插入到後面for (int i = 0; i < nums.size(); i++) {if (mapping.find(nums[i]) == mapping.end())mapping[nums[i]] = 1;else mapping[nums[i]]++;}//自動排過序了map <int, int>::iterator end = mapping.end();end--;map <int, int>::iterator begin = mapping.begin();//begin--;for (map <int, int>::iterator i = mapping.begin(); (i != mapping.end() && i->first <= 0); i++) {for (map <int, int>::iterator j = end; (j!=begin&& j->first >= 0); j--) {vector<int>temp;if (-j->first - i->first < i->first || -j->first - i->first >j->first){}else if (-j->first - i->first == i->first || -j->first - i->first == j->first) {if (i->first == 0 ) { if(mapping[i->first] > 2){temp.push_back(0); temp.push_back(0); temp.push_back(0);result.push_back(temp); }}else {if (mapping[-j->first - i->first] > 1) {temp.push_back(i->first);temp.push_back(-j->first - i->first);temp.push_back(j->first);result.push_back(temp);}}}else {if (mapping.find(-j->first - i->first) != mapping.end()) {temp.push_back(i->first);temp.push_back(-j->first - i->first);temp.push_back(j->first);result.push_back(temp);}}}}return result;}};
運行結果:
Top Solution的50ms的解決方案://簡潔了不少
vector<vector<int> > threeSum(vector<int> &num) { vector<vector<int> > res; std::sort(num.begin(), num.end()); for (int i = 0; i < num.size(); i++) { int target = -num[i]; int front = i + 1; int back = num.size() - 1; while (front < back) { int sum = num[front] + num[back]; // Finding answer which start from number num[i] if (sum < target) front++; else if (sum > target) back--; else { vector<int> triplet(3, 0); triplet[0] = num[i]; triplet[1] = num[front]; triplet[2] = num[back]; res.push_back(triplet); // Processing duplicates of Number 2 // Rolling the front pointer to the next different number forwards while (front < back && num[front] == triplet[1]) front++; // Processing duplicates of Number 3 // Rolling the back pointer to the next different number backwards while (front < back && num[back] == triplet[2]) rear--; } } // Processing duplicates of Number 1 while (i + 1 < num.size() && num[i + 1] == num[i]) i++; } return res; }
祝刷題愉快~