【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<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

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


祝刷题愉快~







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朱铭德

五毛也是爱٩(●´৺`●)૭

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值