Two Sum(leetcode1)

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 public class Solution {
 2     public int[] twoSum(int[] nums, int target) {
 3         HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
 4         for(int i = 0; i < nums.length; i++){
 5             Integer diff = (Integer)(target - nums[i]);
 6             if(hm.containsKey(diff)){
 7                 int result[] = {hm.get(diff)+1, i+1};
 8                 return result;
 9             }
10             hm.put(nums[i], i);
11         }
12         return null;
13
14     }
15 }
时间: 2024-10-14 19:09:18

Two Sum(leetcode1)的相关文章

[LeetCode] Binary Tree Maximum Path Sum(递归)

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNo

HDU 6058 Kanade&#39;s sum(链表)

http://acm.hdu.edu.cn/showproblem.php?pid=6058 题意:找出所有区间第K大的值之和. 思路: 又有点贡献值的味道,就是考虑当前这个数贡献了几个区间. 然后往左和往右分别找大于当前数的k-1个数,这样就可以确定区间的个数,这要求我们从小到大找 并且找完之后需要删除这个数,用链表来维护. 删除元素的目的是为了加速,保证了当前查找的元素是最小值,所以只需要跳跃寻找k次就可以.具体看代码. 1 #include<iostream> 2 #include<

HDU 1003:Max Sum(DP)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 142742    Accepted Submission(s): 33225 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max s

1003 Max Sum(动态规划)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 137169    Accepted Submission(s): 31787 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a

HDU-1003:Max Sum(优化)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 287192    Accepted Submission(s): 68202 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max su

leetcode 1 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

LeetCode:39. Combination Sum(Medium)

1. 原题链接 https://leetcode.com/problems/combination-sum/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值target,找出数组中累加之后等于target的所有元素组合 注意:(1)数组中的每一个元素可以重复用:(2)数组中不存在重复元素:(3)数组中都是正整数 3. 解题思路 采用迭代的方法检验所有元素组合 4. 代码实现 1 import java.util.ArrayList; 2 import

第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)

124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节点为结尾(包含它或者不包含)的最大值,有两种情况,分别来自左儿子和右儿子设为Vnow. 然后考虑经过这个节点的情况来更新最终答案.更新答案后返回Vnow供父节点继续更新. 代码很简单. 有一个类似的很有趣的题目,给定一个二叉树,选择一条路径,使得权值最大的和最小的相差最大.参考POJ3728 cla

LibreOJ #6220. sum(数论)

题目大意:在数组中找出一些数,使它们的和能被n整除 这题标签是数学,那我就标题就写数论好了... 显然如果数组中有n的倍数直接取就行. 那假设数组中没有n的倍数,把数组中的数求前缀和后全部%n,会得到一堆1~n-1的数(注意没有0,因为数组中没有n的倍数),那根据抽屉原理一定有两个相同的数,设这两个相同的数所在的位置为l和r,那么下标在[l+1,r]的这些数的和一定是n的倍数 记得开LL,我还RE两发QAQ #include<iostream> #include<cstdlib>