LeetCode(1) -Two Sum

  题目要求很简单,给你一个数组(例如,nums = [2,7,11,15])和一个target(target = 9),找到数组里两个数相加后能得到target的这两个数的index。在本例中,返回的应该是[0,1]。

  碰到这样的题目,首先应该想到的是运用HashMap,记录数组里的每个元素和对应的index,把O(n^2)的问题转变为O(n)的问题,然后再第二次遍历数组,当current_number = nums[i]时,寻找HashMap里面是否存在target - nums[i],若存在,则输出的index为[i, map.get(terget - nums[i])]。

  代码如下:

 

 1 public class Solution {
 2     public int[] twoSum(int[] nums, int target) {
 3         HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
 4         for (int i = 0; i < nums.length; i++) {
 5             map.put(nums[i],i);
 6         }
 7         int[] res = new int[2];
 8         for (int i = 0; i < nums.length; i++) {
 9             int remain = target - nums[i];
10             if (map.containsKey(remain) && map.get(remain) != i) {
11                 res[0] = i;
12                 res[1] = map.get(remain);
13                 break;
14             }
15         }
16         return res;
17     }
18 }
时间: 2024-10-31 13:36:57

LeetCode(1) -Two Sum的相关文章

LeetCode --- 64. Minimum Path Sum

题目链接:Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 这道题的要求是在m*n

【Leetcode】Minimum Path Sum

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 思路:简单的动态规划题目,设f(m, n)为从(0, 0)到达(m

leetcode --day12 Surrounded Regions &amp; Sum Root to Leaf Numbers &amp; Longest Consecutive Sequence

1.  Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X X O X X O X X After running your fu

leetcode 练习1 two sum

leetcode 练习1  two sum [email protected] 问题描述 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

[LeetCode] Minimum Size Subarray Sum 解题思路

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subarray [4,3] has the minimal

leetcode第一刷_Combination Sum Combination Sum II

啊啊啊啊,好怀念这种用递归保存路径然后打印出来的题目啊,好久没遇到了. 分了两种,一种是可以重复使用数组中数字的,一种是每个数字只能用一次的.其实没有多大区别,第一种每次进入递归的时候都要从头开始尝试,第二种要找一个标记的数组,把已经用到过的排除掉,就像生成全排列时的做法一样.跟我一样用引用保存中间结果的话,要注意回退的情况.第二种回退时,要把用到的那个数也恢复为可用,就完全像全排列时做的一样.破例贴两个题的代码,因为他们是在是不值得用两片文章来写. class Solution { publi

【一天一道LeetCode】#113. Path Sum II

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22,

leetcode第一刷_Path Sum II

测试策略:静态测试还是动态测试? [对话场景] 成功发布某个软件版本之后,项目团队召开了项目的经验教训总结大会.在会议期间,项目经理小项和测试经理小测进行了如下的对话: 小项:"小测,我们的项目时间压力很大,测试执行是我们的关键路径,测试团队是否可以在测试执行阶段投入更多的人力和物力?"限定时间和人力资源同等条件. 小测:"啊!假如增加我们的测试执行时间,在整个周期不变的情况下,我们就需要压缩前期的学习和评审投入的时间和工作量,是吗?" 小项:"是的,你看

Leetcode 线性表 Two Sum

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Two Sum Total Accepted: 19206 Total Submissions: 103959 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