leetcode - 1、15、18、167

126 篇文章 0 订阅

1. Two Sum

167. Two Sum II - Input array is sorted

其实我没太看出来这两道题到底有什么区别,似乎按照出题人的考虑第一道题用hashmap来做比较好,后面一道题用两点法来做比较好。这里放在一起讲

  • hashmap的方法
    首先hashmap的方法是比较容易想到的,从一组数组中找到和为target的两个数,只要用target减数组中的每个数,用这个差值作为key,用索引i作为value。然后从0到length-1遍历即可。整个的复杂度为O(n)。很简单的思路,java代码如下:
public class Solution {
    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
        for(int i = 0 ; i < nums.length ; i++){
            if(map.keySet().contains(nums[i])){
                int[] r = new int[2];
                r[0] = map.get(nums[i]);
                r[1] = i;
                return r;
            }
            int ret = target - nums[i];
            map.put(ret,i);
        }
        return null;
    }
}
  • 两点法:
    两点法稍微复杂一些,但是有更强的拓展性,可以用于任意找任意k个数的和。
    首先,对这个数组排序,
    然后,如果k为2,那么指定left点为索引0和right点为索引length-1,这样如果两个数的和小于target则左节点右移,如果和大于target则右节点左移,如果相等则两节点同时移动。遍历就行。

    复杂度的话,O(nlgn)+O(n) = O(nlgn)。

public class Solution {
    public int[] twoSum(int[] num, int target) {
        int[] indice = new int[2];
        if (num == null || num.length < 2) return indice;
        int left = 0, right = num.length - 1;
        while (left < right) {
            int v = num[left] + num[right];
            if (v == target) {
                indice[0] = left + 1;
                indice[1] = right + 1;
                break;
            } else if (v > target) {
                right --;
            } else {
                left ++;
            }
        }
        return indice;
    }
}

15. 3Sum

18. 4Sum

对应3Sum的情况,其实就是2sum的拓展了,固定1个点然后退化为2Sum的情况,对于Ksum则可以依次退化为k-1Sum。下面直接贴代码了。3Sum是针对3Sum的实现,4Sum是针对KSum的实现。不过就具体实现来说还是有很多需要注意的部分。实现也是写了很长一段时间。

复杂度的话是O(n^(k-1))

public class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        // List<List<Integer>> ret = new ArrayList<List<Integer>>();
        // List<List<Integer>> ret = new ArrayList<>();
        // List<List<Integer>> ret = new ArrayList<ArrayList<Integer>>(); wrong
        List<List<Integer>> ret = new ArrayList();
        if(nums.length < 3) return ret;

        Arrays.sort(nums);
        int left = 0;
        while(left < nums.length - 2){
            if(nums[left] > 0) break;
            int middle = left + 1;
            int right = nums.length - 1;
            while(middle < right){
                int sum = nums[left] + nums[middle] + nums[right];
                if(sum==0) ret.add(Arrays.asList(nums[left],nums[middle],nums[right]));
                if(sum<=0) while(nums[middle++] ==nums[middle] && middle < right);
                if(sum>=0) while(nums[right--] == nums[right] && middle < right);
            }
            while(nums[left++]==nums[left] && left < nums.length - 2);
        }
        return ret;
    }
}
public class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> ret = new ArrayList();
        if(nums.length < 4) return ret;
        Arrays.sort(nums);
        ret = helper(nums,4,target,0);
        return ret;
    }

    private List<List<Integer>> helper(int[] nums, int k,int target,int pos){
        List<List<Integer>> ret = new ArrayList();
        if (target < nums[pos]*k || target > nums[nums.length - 1]*k){
            return ret;
        }
        if(k > 2){
            int i = pos;
            while(i <= (nums.length - k)){
                List<List<Integer>>tmp = helper(nums,k-1,target-nums[i],i+1);
                for(List<Integer> sub : tmp){
                    List<Integer> node = new LinkedList<Integer>();
                    node.add(nums[i]);
                    node.addAll(sub);
                    ret.add(node);
                }
                while(nums[i++]==nums[i] && i <= nums.length - k);
            }
        }else{//when k ==2
            int left = pos;
            int right = nums.length - 1;
            while(left < right){
                int sum = nums[left] + nums[right];
                if(sum == target){
                    List<Integer> tmp = Arrays.asList(nums[left],nums[right]);
                    ret.add(tmp);
                    while(nums[left++] == nums[left] && left < right);
                    while(nums[right--] == nums[right] && left < right);
                }else if(sum < target){
                    while(nums[left++] == nums[left] && left < right);
                }else{//sum > target
                    while(nums[right--] == nums[right] && left < right);
                }
            }
        }
        return ret;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LeetCode-Editor是一种在线编码工具,它提供了一个用户友好的界面编写和运行代码。在使用LeetCode-Editor时,有时候会出现乱码的问题。 乱码的原因可能是由于编码格式不兼容或者编码错误导致的。在这种情况下,我们可以尝试以下几种解决方法: 1. 检查文件编码格式:首先,我们可以检查所编辑的文件的编码格式。通常来说,常用的编码格式有UTF-8和ASCII等。我们可以将编码格式更改为正确的格式。在LeetCode-Editor中,可以通过界面设置或编辑器设置来更改编码格式。 2. 使用正确的字符集:如果乱码是由于使用了不同的字符集导致的,我们可以尝试更改使用正确的字符集。常见的字符集如Unicode或者UTF-8等。在LeetCode-Editor中,可以在编辑器中选择正确的字符集。 3. 使用合适的编辑器:有时候,乱码问题可能与LeetCode-Editor自身相关。我们可以尝试使用其他编码工具,如Text Editor、Sublime Text或者IDE,看是否能够解决乱码问题。 4. 查找特殊字符:如果乱码问题只出现在某些特殊字符上,我们可以尝试找到并替换这些字符。通过仔细检查代码,我们可以找到导致乱码的特定字符,并进行修正或替换。 总之,解决LeetCode-Editor乱码问题的方法有很多。根据具体情况,我们可以尝试更改文件编码格式、使用正确的字符集、更换编辑器或者查找并替换特殊字符等方法来解决这个问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值