【LeetCode】[TOP-1] 【两数之和】

  • 题目描述
  • 思路分析
  • Java代码
  • 代码链接

题目描述

  给定一个整数数组 nums?和一个目标值 target,请你在该数组中找出和为目标值的那?两个整数,并返回他们的数组下标。你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
示例
给定 nums = [2, 7, 11, 15], target = 9 ,因为 nums[0] + nums[1] = 2 + 7 = 9 , 所以返回 [0, 1]

思路分析

  1. 可以暴力解
  2. 可以利用了java集合中map 哈希表的特性,遍历数组,将元素存入哈希表中,并同时判断(目标值-当前元素)得到的结果是否在哈希表中,如果在哈希表中则返回这两个下标。
  3. 自己实现哈希表的方式,直接用数组。

Java代码

public class TOP001 {
    public int[] twoSum(int[] nums, int target) {
        return Solution3(nums, target);
    }

    /**
     * 解法一:暴力解法
     * @param nums
     * @param target
     * @return
     */
    public int[] Solution1(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    return new int[] { i, j };
                }
            }
        }
        throw new IllegalArgumentException("No two sum solution");
    }

    /**
     * 解法二: 这里利用了java集合中map 哈希表的特性,遍历数组,将元素存入哈希表中, 并同时判断(目标值-当前元素)得到的结果是否在哈希表中,
     * 如果在哈希表中则返回这两个下标,
     *
     * @param nums
     * @param target
     * @return
     */
    public int[] Solution2(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; i++) {
            int targetResult = target - nums[i];
            if (map.get(nums[i]) != null && nums[i] * 2 == target) {
                //此处针对于 2 +2 =4  4+4=8 这种 存在重复的情况,且重复的加起来正好是target
                return new int[] { map.get(targetResult), i };
            }
            if (map.containsKey(targetResult)) {
                return new int[] { map.get(targetResult), i };
            }
            map.put(nums[i], i);
        }
        throw new IllegalArgumentException("No two sum solution");
    }

    /**
     * 解法三:
     * 利用手动哈希的方法,和解法二思路相同
     * @param nums
     * @param target
     * @return
     */
    public static int[] Solution3(int[] nums, int target) {
        int indexArrayMax = 2047;
        int[] indexArrays = new int[indexArrayMax + 1];
        for (int i = 0; i < nums.length; i++) {
            int diff = target - nums[i];
            int index = diff & indexArrayMax;
            if (indexArrays[index] != 0) {
                return new int[] { indexArrays[index] - 1, i };
            }
            indexArrays[nums[i] & indexArrayMax] = i + 1;
        }
        throw new IllegalArgumentException("No two sum value");
    }
}

代码链接

剑指Offer代码-Java

原文地址:https://www.cnblogs.com/haoworld/p/leetcodetop1-liang-shu-zhi-he.html

时间: 2024-08-27 12:05:48

【LeetCode】[TOP-1] 【两数之和】的相关文章

leetcode刷题--两数之和(简单)

一.序言 第一次刷leetcode的题,之前从来没有刷题然后去面试的概念,直到临近秋招,或许是秋招结束的时候才有这个意识,原来面试是需要刷题的,面试问的问题都是千篇一律的,只要刷够了题就差不多了,当然你的基础也要扎实,毕竟在技术面的时候很容易露馅的. 所以奉劝各位还未毕业,在大三或大二的师弟师妹早点刷题,心里也有底气进入求职大军,毕竟大四开始刷题的话时间上有些太紧了,推荐刷题的话就是牛客和leetcode. 回归正题,这次记录的是leetcode刷的第一题--两数之和. 二.审题 审题真的很重要

LeetCode | No.1 两数之和

题目描述: 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. 给定一个整数数组nums和一个目标值target,请你在该数

leetcode——Two Sum 两数之和(AC)

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

LeetCode算法题——两数之和(python)

两数之和: 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数.你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用.例如:给定 nums = [2, 7, 11, 15], target = 9.由nums[0] + nums[1] = 2 + 7 = 9,所以返回 [0, 1] 原文地址:http://blog.51cto.com/13921683/2318829

LeetCode【167. 两数之和 II - 输入有序数组】

这道题最开始想到的就是,两个for,target是两数之和,就可以target - numbers[i],比较是否有与后面数相等. class Solution { public int[] twoSum(int[] numbers, int target) { int i,j,s; int c = numbers.length; int[] t = new int[2]; for(i = 0;i <= c-1;i++) { s = target - numbers[i]; t[0] = i+1

leetcode刷题两数之和

给定一个整数数组 nums?和一个目标值 target,请你在该数组中找出和为目标值的那?两个?整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/two-sum 两

Leetcode篇:两数之和

@author: ZZQ @software: PyCharm @file: addTwoNumbers.py @time: 2018/9/18 10:35 要求:给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. e.g.: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465

leetcode中的两数之和(第一题:简单)

描述:给定一个整数数组和一个目标值,找出数组中和为目标值的 两个 数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用.示例:给定 nums = [2, 7, 11, 15], target = 9因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 题意:给一个整形的数,这个数在会在给定数组的两个位置数相加的和!这个和是指值的相加,不是数组的索引相加:如果在数组中有这样的两个数,要求返回两个数的索引(也就是数组的下标): 解法一:暴力方法:应用

LeetCode Two Sum 两数之和

题意:在一个整数序列中找到两个元素,他们之和为target,并用vector返回这两个元素的位置(升序),位置从1开始算起. 思路: 方法(1):两个指针法.也就是排序,然后一个从头扫,一个从尾扫,前提是先排序,但是给的数组是无序的,一旦排序就失去了他们的具体位置.如果是ACM的题还可以弄个结构体把他们打包起来,再根据值大小来排序,这样找到了也就能找到他们的位置.可是这里是在一个类里面的,用不上结构体,肯定还有其他方法 ,比如 pair,但是我还不会实现.于是,用multimap来代替,将元素值

python刷LeetCode:1.两数之和

难度等级:简单 题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9所以返回 [0, 1] 来源:力扣(LeetCode)链接:https://leetcode-cn.com/proble