• 2 Sum ,3 Sum, 3 Sum close


    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.

    Example:

    Given nums = [2, 7, 11, 15], target = 9,
    
    Because nums[0] + nums[1] = 2 + 7 = 9,
    return [0, 1].
    public class Solution {
        public int[] twoSum(int[] nums, int target) {
            int[] res = new int[2];
            if(nums == null || nums.length == 0) return res;
            HashMap<Integer, Integer> map = new HashMap<>();
            for(int i = 0; i < nums.length ; i++){
                if(map.containsKey(target-nums[i])){
                    res[0] = map.get(target-nums[i]);
                    res[1] = i;
                }
                map.put(nums[i], i);
            }
            return res;
        }
    }

    3 Sum

    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]
    ]
    public class Solution {
        public List<List<Integer>> threeSum(int[] nums) {
            List<List<Integer>> res = new ArrayList<>();
            Arrays.sort(nums);
            for(int i = 0 ; i < nums.length - 2; i++){
                List<Integer> list = new ArrayList<>();
                int left = i + 1;
                int right = nums.length -1;
                int sum =  0 - nums[i];
                while(left < right){
                    if(nums[left] + nums[right] == sum){
                        list.add(nums[i]);
                        list.add(nums[left]);
                        list.add(nums[right]);
                        if(!res.contains(list))
                            res.add(new ArrayList<>(list));
                        list.clear();
                        while(left < right && nums[left] == nums[left+1]) left++;
                        while(left < right && nums[right] == nums[right-1]) right--;
                        left++;
                        right--;
                    }
                    else if(nums[left] + nums[right] < sum){
                        left ++;
                    }
                    else
                        right--;
                }
            }
            return res;
        }
    }

    16. 3Sum Closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

        For example, given array S = {-1 2 1 -4}, and target = 1.
    
        The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
    public class Solution {
        public int threeSumClosest(int[] nums, int target) {
          Arrays.sort(nums);
          int min = Integer.MAX_VALUE;
          for(int i = 0 ;  i < nums.length -2; i++){
              int left = i + 1;
              int right = nums.length - 1;
              int value = target - nums[i];
                while(left < right){
                    if(nums[left] + nums[right] == value)
                        return target;
                   
                    if(min == Integer.MAX_VALUE || Math.abs(nums[i] + nums[left] + nums[right] - target) < Math.abs(min - target))
                        min = nums[i] + nums[left] + nums[right];
           
                    if(nums[left] + nums[right] > value)                    
                        right --;
                    
                    else
                        left++ ;
                    }
                }
              
          return min;
        }
    }
  • 相关阅读:
    docker logs-查看docker容器日志
    Linux Python3 的一些坑
    系统安装-007 CentOS7yum源添加、删除及其yum优化
    老司机使用 docker-pan 一键搭建可离线磁力种子的私有云盘,可在线播放预览文件
    syncthing安卓客户端怎么使用
    syncthing搭建私人网盘分享
    黑群晖6.1.x虚拟化安装方法
    Swoole跟thinkphp5结合开发WebSocket在线聊天通讯系统教程
    phpmailer 生产环境发送邮件发送失败Failed to connect to server的解决办法
    aliyun RDS不支持MYIAM
  • 原文地址:https://www.cnblogs.com/joannacode/p/6124331.html
Copyright © 2020-2023  润新知