美文网首页
2020-03-19 刷题1(三数之和)

2020-03-19 刷题1(三数之和)

作者: nowherespyfly | 来源:发表于2020-03-20 11:52 被阅读0次

15 三数之和

这种解法真的是非常机智啊。因为这个算法的时间复杂度不会小于O(n^2)了,首先对数组进行排序,只会带来O(nlogn)的代价,可以接受。
这个问题最难的地方在于,如何保证三元组不重复,而采用双指针的做法,就可以保证我们添加到ret数组中的三元组的三个元素是单调增的,并且添加顺序也是按照前两个元素严格递增顺序添加的,不会出现(-1, 0, 1)和(-1,1, 0)这种情况。
如果采用哈希表,设置两层循环,去重会变得很困难,还是双指针比较简单明了,学习了。

这个是我参考的一个题解,讲得非常好。


class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> ret;
        if(nums.size() < 3) return ret;
        sort(nums.begin(), nums.end());
        for(int i = 0; i < nums.size() - 2; i++){
            if(nums[i] > 0) break;
            if(i > 0 && nums[i] == nums[i - 1])
                continue;
            int l = i + 1, r = nums.size() - 1;
            int two_sum = 0 - nums[i];
            while(l < r){
                // skip repeat element
                while(l > i + 1 && l < r && nums[l] == nums[l - 1])
                    l++;
                while(r < nums.size() - 1 && l < r && nums[r] == nums[r + 1])
                    r--;
                while(l < r && nums[l] + nums[r] < two_sum)
                    l++;
                while(l < r && nums[l] + nums[r] > two_sum)
                    r--;
                if(l < r && nums[l] + nums[r] == two_sum){
                    ret.push_back({nums[i], nums[l], nums[r]});
                    l++;
                    r--;
                }
            }
        }
        return ret;
    }   
};

409 最长回文串

很简单的哈希表+字符串题

class Solution {
public:
    int longestPalindrome(string s) {
        map<char, int> cmap;
        for(int i = 0;  i < s.size(); i++){
            cmap[s[i]]++;
        }
        map<char, int>::iterator iter;
        bool single_flat = false;
        int len = 0;
        for(iter = cmap.begin(); iter != cmap.end(); iter++){
            len += iter->second / 2 * 2;
            if(!single_flat && iter->second % 2){
                len++;
                single_flat = true;
            }
        }
        return len;
    }
};

相关文章

  • 2020-03-19 刷题1(三数之和)

    15 三数之和 这种解法真的是非常机智啊。因为这个算法的时间复杂度不会小于O(n^2)了,首先对数组进行排序,只会...

  • Python小白 Leetcode刷题历程 No.1-No

    Python小白 Leetcode刷题历程 No.1-No.5 两数之和、两数相加、无重复字符的最长子...

  • Python小白 Leetcode刷题历程 No.16-N

    Python小白 Leetcode刷题历程 No.16-No.20 最接近的三数之和、Phone Nu...

  • LeetCode刷题 1 两数之和

    LeetCode刷题 1 两数之和 题目 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。你可以假设每...

  • leetcode No.16 3Sum Closest(最接近的

    题干: 效果: 思路: 这道题跟No.15很像,就是要找三数之和,但15是找三数之和为0,这题是找离target最...

  • leecode刷题(8)-- 两数之和

    leecode刷题(8)-- 两数之和 两数之和 描述: 给定一个整数数组 nums 和一个目标值 target,...

  • leetcode 刷题 题1.两数之和

    给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 的那 两个 整数,...

  • LeetCode-15 三数之和

    题目:15. 三数之和 难度:中等 分类:数组 解决方案:双指针 今天我们学习第15题三数之和,这是一道中等题。像...

  • leetcode刷题-1. 两数之和

    给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的...

  • LeetCode刷题--两数之和

    题目描述 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。你可以假设每个输入只对应一种答案,且同样的元...

网友评论

      本文标题:2020-03-19 刷题1(三数之和)

      本文链接:https://www.haomeiwen.com/subject/rbmkyhtx.html