Two-Sum

原题
  Given an array of integers, find two numbers such that they add up to a specific target number.
  The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
  You may assume that each input would have exactly one solution.
  Input: numbers={2, 7, 11, 15}, target=9
  Output: index1=1, index2=2

题目大意
要求:这个函数twoSum必须要返回能够相加等于目标数字的两个数的索引,且index1必须要小于index2。请注意一点,你返回的结果(包括index1和index2)都不是基于0开始的。

解题思路
类似于归并排序的思想 左右两个指针移动
,一开始用了辅助类来解决,其实还可以map来解决更快

解法一

public class SimpleTest {

    public int[] twoSum(int []datas,int target){
        int []result = {0,0};
        //记录value的位置
        Node []temp = new Node[datas.length];
        for (int i=0;i<datas.length;i++){
            temp[i] = new Node(datas[i], i);
        }
        //记录左边索引
        int left = 0;
        //记录右边索引
        int right = datas.length-1;
        //进行升序
        Arrays.sort(temp);
        //当左边与右边在一起结束循环
        while (left<right){
            Node leftNode = temp[left];
            Node rightNode = temp[right];
            //找到目标值还需要比较索引
            if (leftNode.value+rightNode.value==target){
                if (leftNode.index>rightNode.index){
                    result[0] = leftNode.index+1;
                    result[1] = rightNode.index+1;
                }
                else {
                    result[0] = rightNode.index+1;
                    result[1] = leftNode.index=1;
                }
                break;
            }
            //sum太大需要right--
            else if(leftNode.value+rightNode.value>target){
                    right--;
            }
            //sum太小需要eft++
            else if(leftNode.value+rightNode.value<target){
                left++;
            }
        }
        return result;
    }
    public static class Node implements Comparable<Node>{
        public int value;
        public int index;
        public Node(){ }
        public Node(int value,int index){
            this.index = index;
            this.value = value;
        }

        @Override
        public int compareTo(Node o) {
            return this.value-o.value;
        }
    }
    @Test
    public void testTwoSum(){
        int []datas = {1,3,100,98,20};
        int target = 120;
        // 0 0 代表没有满足条件的
        for (int data:twoSum(datas,target)){
            System.out.println(data);
        }
    }

}

解法二

public class TwoSum {
    public int[] twoSum(int[] nums, int target){
    Map<Integer, Integer>map=new HashMap<>();
    for (int i = 0; i < nums.length; i++) {
        map.put(nums[i], i);
    }
    for (int i = 0; i < nums.length; i++) {
        int temp=target-nums[i];
        Integer index=map.get(temp);
         if (index!=null && i!=index) {//注意这里,特殊地方
             if (index>i)
                 return new int[]{i+1,index+1};
             else
                 return new int[]{index+1,i+1};
         }
     }

     return null;

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值