【LeetCode014-015算法/编程练习C++】最长共同前缀,3Sum(和为0) //用到了map的自动排序



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& 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;
	}
};
【LeetCode014-015算法/编程练习C++】最长共同前缀,3Sum(和为0) //用到了map的自动排序_第1张图片






15. 3Sum

 
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 abc 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> threeSum(vector& nums) {
		//先试试O(n^2)
	vector> result;
	if (nums.size() == 0) { return result; }
	if(nums[0]==0&&nums[1]==0&&nums[2]==0&&nums.size()>2){
	    vectortemp;
	    temp.push_back(0); temp.push_back(0); temp.push_back(0);
		result.push_back(temp);
	  //  return result;
	}
	mapmapping;//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 ::iterator end = mapping.end();
	end--;
	map ::iterator begin = mapping.begin();
//	begin--;
	for (map ::iterator i = mapping.begin(); (i != mapping.end() && i->first <= 0); i++) {
		for (map ::iterator j = end; (j!=begin&& j->first >= 0); j--) {
			vectortemp;
			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;
	}
};

运行结果:

【LeetCode014-015算法/编程练习C++】最长共同前缀,3Sum(和为0) //用到了map的自动排序_第2张图片



Top Solution的50ms的解决方法://简洁了不少


vector > threeSum(vector &num) {
    
    vector > 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 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;
    
}


祝刷题愉快~


【LeetCode014-015算法/编程练习C++】最长共同前缀,3Sum(和为0) //用到了map的自动排序_第3张图片





你可能感兴趣的:(LeetCode,-----数据结构-----)