leetcode-第五周

42. Trapping Rain Water

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.

For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.

/**
 * 思路:扫一遍取最大值把数组分两半,并分别从左右两边向中间夹
 */
class Solution {
public:
    int trap(vector<int>& height) {
        if (height.empty()) return 0;
        int mx_idx = 0, ret = 0;
        for (int i = 0; i < height.size(); i++) if (height[mx_idx] < height[i]) mx_idx = i;
        for (int cur_mx = height[0], i = 1; i < mx_idx; i++) {
            ret += max(0, cur_mx - height[i]);
            cur_mx = max(cur_mx, height[i]);
        }
        for (int cur_mx = height.back(), i = height.size() - 1; i > mx_idx; i--) {
            ret += max(0, cur_mx - height[i]);
            cur_mx = max(cur_mx, height[i]);
        }
        return ret;
    }
};

15. 3Sum

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

/**
 * 思路:a+b+c=0,排序,枚举a,左右夹逼b和c
 * 注意要跳过重复的数
 */
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int> > ret;
        sort(nums.begin(), nums.end());
        for (int i = 0; i < nums.size(); i++) {
            if (i > 0 && nums[i] == nums[i - 1]) continue;
            for (int l = i + 1, r = nums.size() - 1; l < r; l++) {
                if (l > i + 1 && nums[l - 1] == nums[l]) continue;
                while (l < r && nums[l] + nums[r] + nums[i] > 0) r--;
                if (l < r && nums[l] + nums[r] + nums[i] == 0)
                    ret.push_back(vector<int>{nums[i], nums[l], nums[r]});
            }
        }
        return ret;
    }
};

128. Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

For example,
Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.

Your algorithm should run in O(n) complexity.

/**
 * 思路:并查集,用哈希表来进行离散化
 */
class Solution {
private:
    int find(unordered_map<int, int> &C, int x) {
        return C[x] == x ? C[x]: find(C, C[x]);
    }
    void merge(unordered_map<int, int> &C, unordered_map<int, int> &S, int x, int y) {
        int px = find(C, x), py = find(C, y);
        if (px == py) return;
        else {
            C[py] = px;
            S[px] += S[py];
        }
    }
public:
    int longestConsecutive(vector<int>& nums) {
        unordered_map<int, int> C, S;
        for (int i = 0; i < nums.size(); i++) {
            int num = nums[i];
            if (C.find(num) == C.end()) C[num] = num, S[num] = 1;
            if (C.find(num + 1) != C.end()) merge(C, S, num, num + 1);
            if (C.find(num - 1) != C.end()) merge(C, S, num, num - 1);
        }
        int ret = 0;
        for (auto e: S) ret = max(e.second, ret);
        return ret;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值