LeetCode 1,11,26

换阵地了,不搞uva直接来LeetCode了,一天两道easy一道medium,一周一道hard,希望可以坚持吧。

0 1. Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:
Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

给一个数组和一个目标数字,找俩数相加起来的和刚好是目标数字的,返回两个数字的位置(还要按先后顺序)。
C++:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> res;
        if(nums.size() < 2){
            return res;
        }
        map<int, int> m;
        for(int i = 0; i < nums.size(); i++){
            m[nums[i]] = i;
        }
        map<int, int>::iterator it;
        for(int i = 0; i < nums.size(); i++){
            it = m.find(target - nums[i]);
            if(it != m.end()){
                if (i == it->second) continue;
                res.push_back(i);
                res.push_back(it->second);
                return res;
            }
        }
        return res;
    }
};

java:

public class Solution {
    public int[] twoSum(int[] numbers, int target) {
    int[] result = new int[2];
    Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    for (int i = 0; i < numbers.length; i++) {
        if (map.containsKey(target - numbers[i])) {
            result[1] = i;
            result[0] = map.get(target - numbers[i]);
            return result;
        }
        map.put(numbers[i], i);
    }
    return result;
}
}

1 1. Container With Most Water

Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container and n is at least 2.
坐标轴上很多条线,找出两条线能使与x轴围成的面积最大(返回的是最大面积)。
关于这个题,只要思路想通了刷刷几下就写完了。。
从两端开始往中间移,左边一条线l,右边一条线r,比较lr的高低,哪条线短就抛弃哪条线,l短的话就l++这样。但是要记录每次的最大面积值,新的面积更大了就替换掉之前的最大面积。这个思路是,面积的大小是以两条线的距离和较短那条线的高度决定的,所以当l比r短时,如果你不动l去动r,那之后每次获得的面积肯定不会比现在更大,因为l一直是那么长。相反要是移动l的话,可能中间会有比l长的线,使得min(l,r)比现在大。

class Solution {
public:
    int maxArea(vector<int>& height) {
        int l = 0, r = height.size()-1;
        int maxv = 0;
        while(l < r){
            maxv = max(maxv, (r - l) * min(height[l], height[r]));
            if(height[l] < height[r]) l++;
            else r--;
        }
        return maxv;
    }
};

2 6. Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

For example,
Given input array nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn’t matter what you leave beyond the new length.
值得注意的是题目给的就是已经排好序了的数组,就很好做了。

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int temp = 0;//记录有多少重复了的数字个数
        for(int i = 1; i < nums.size(); i++){
            if(nums[i] == nums[i-1]) temp++;
            else nums[i-temp] = nums[i];
        }
        return nums.size()-temp;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值