leetcode two-sum(Java)

leetcode题目

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

思路

 * 思路1:(需要两次遍历数组)
 * 1、遍历数组,把每个值作为key, 索引+1 作为value
 * 2、再次遍历数组,查看map中是否有target-curNum作为key的项,有的话则找到目标值
 * 思路2:(一次遍历数组即可)
 * 1、遍历数组,如果map包含key为curNum的值,则找到目标值。否则把target-curNum作为key, 
 *    索引作为value放入map

代码

package com.my.test.leetcode.array;

import java.util.HashMap;
import java.util.Map;

/**
 * 题目:
 * 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
 * 
 */
public class TwoSum
{
    /**
     * 思路:(需要两次遍历数组)
     * 1、遍历数组,把每个值作为key, 索引+1 作为value
     * 2、再次遍历数组,查看map中是否有target-curNum作为key的项,有的话则找到目标值
     */
    public int[] twoSum(int[] numbers, int target) {
        if (numbers == null || numbers.length <= 1) {
            return null;
        }
        
        // 缓存所有值到hash表中,取值时间复杂度o(1)
        Map<Integer, Integer> cache = new HashMap<>();
        int len = numbers.length;
        for (int i=0; i<len; i++) {
            cache.put(numbers[i], i);
        }
        
        int[] ret = new int[2];
        // 再次遍历数组,找出目标值
        for (int i=0; i<len; i++) {
            int val = numbers[i];
            int otherVal = target - val;
            Integer otherValIndex = cache.get(otherVal);
            // 另外一个值在数组中, 并且不是当前遍历的数字
            if (otherValIndex != null && i != otherValIndex) {
                // 索引值+1
                ret[0] = i + 1;
                ret[1] = otherValIndex + 1;
                return ret;
            }
        }
        
        return ret;
    }

    /**
     * 思路:(一次遍历数组即可)
     * 1、遍历数组,如果map包含key为curNum的值,则找到目标值。否则把target-curNum作为key, 索引 作为value放入map
     */
    public int[] twoSum1(int[] numbers, int target) {
        if (numbers == null || numbers.length <= 1) {
            return null;
        }
        
        // 缓存所有值到hash表中,取值时间复杂度o(1)
        Map<Integer, Integer> cache = new HashMap<>();
        int[] ret = new int[2];
        
        int len = numbers.length;
        for (int i=0; i<len; i++) {
            int val = numbers[i];
            if (cache.containsKey(val)) {
                // target-val的索引值
                ret[0] = cache.get(val) + 1;
                // 当前值的索引值
                ret[1] = i + 1;
                return ret;
            }
            // 放入cache, target-val作为key, i为value
            cache.put(target - val, i);
        }
        
        return ret;
    }
    
    public static void main(String[] args)
    {
        int[] numbers = {2, 7, 11, 15};
        int[] ret = new TwoSum().twoSum(numbers, 9);
        System.out.println(ret[0] + ", " + ret[1]);
        int[] ret1 = new TwoSum().twoSum1(numbers, 9);
        System.out.println(ret1[0] + ", " + ret1[1]);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值