【LeetCode014-015演算法/編程練習C++】最長共同首碼,3Sum(和為0) //用到了map的自動排序__Python

來源:互聯網
上載者:User



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;    }


祝刷題愉快~







聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.