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

answer:

 public class TwoSum {
//最low的方法
public int[] twoSum(int[] nums, int target) {
int[] ret = new int[2];
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
ret[0] = i + 1;
ret[1] = j + 1;
}
}
}
return ret; } // 利用map,只需要遍历一次即可
public int[] twoSum1(int[] numbers, int target) {
int[] ret = new int[2]; //初始化一个数组,默认值为0
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < numbers.length; i++) {
if (map.containsKey(target - numbers[i])) {
ret[1] = i + 1; //map中放的数字必然是比较早遍历过的
ret[0] = map.get(target - numbers[i]);
return ret;
}
map.put(numbers[i], i + 1);
}
return ret;
} //测试
public static void main(String[] args) {
int[] numbers = { 2, 7, 11, 15 };
int target = 9;
TwoSum test = new TwoSum();
int[] result = test.twoSum1(numbers, target);
System.out.println(Arrays.toString(result));
} }

(1)Two Sum--求数组中相加为指定值的两个数的更多相关文章

  1. 【Data Structure & Algorithm】在排序数组中查找和为定值的两个数

    在排序数组中查找和为定值的两个数 题目:输入一个已经按升序排序过的数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字,要求时间复杂度是O(n).如果有多对数字的和等于输入的数字,输出 ...

  2. 在数组中寻找和为定值的n个数

    /*-------------------------------------------------------*/ /*寻找和为定值的两个数 输入一个数组A[0,N-1]和一个数字Sum,在数组中 ...

  3. js数组去重 数组拼接 替换数组中的指定值 递归数组 判断数组中是否存在指定值 数组求和 根据条件判数组值

    这是学习过程中记录的一些关于数组操作的常用属性或方法,记录一下方便以后使用. // 数组去重 var arr1 = [1,1,2,3,4,5,6,3,2,4,5,'a','b','c','a',6,7 ...

  4. 求数组中两两相加等于20的组合(Python实现)

    题目 求数组中两两相加等于20的组合. 例:给定一个数组[1, 7, 17, 2, 6, 3, 14],这个数组中满足条件的有两对:17+3=20, 6+14=20. 解析 分为两个步骤: 先采用堆排 ...

  5. Leetcode39--->Combination Sum(在数组中找出和为target的组合)

    题目: 给定一个数组candidates和一个目标值target,求出数组中相加结果为target的数字组合: 举例: For example, given candidate set [2, 3, ...

  6. php实现求数组中出现次数超过一半的数字(isset($arr[$val]))(取不同数看剩)(排序取中)

    php实现求数组中出现次数超过一半的数字(isset($arr[$val]))(取不同数看剩)(排序取中) 一.总结 1.if(isset($arr[$val])) $arr[$val]++; //1 ...

  7. 常用的函数式接口_Supplier和常用的函数式接口Supplier接口练习_求数组中元素最大值

    Supplier接口 package com.yang.Test.SupplierStudy; import java.util.function.Supplier; /** * 常用的函数式接口 * ...

  8. 《剑指offer》第五十六题(数组中只出现一次的两个数字)

    // 面试题56(一):数组中只出现一次的两个数字 // 题目:一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序 // 找出这两个只出现一次的数字.要求时间复杂度是O(n),空间复杂度 ...

  9. 使用基础知识完成java小作业?强化练习-1.输入数组计算最大值-2.输出数组反向打印-3.求数组平均值与总和-4.键盘输两int,并求总和-5.键盘输三个int,并求最值;

    完成几个小代码练习?让自己更加强大?学习新知识回顾一下基础? 1.输入数组计算最大值 2.输出数组反向打印 3.求数组平均值与总和 4.键盘输两int,并求总和 5.键盘输三个int,并求最值 /* ...

随机推荐

  1. Hadoop集群出现no data node to stop的解决方案

    问题描述: 今天stop hadoop集群的时候出现no datanode to stop ,寻找解决方案,并不是网上资料所说的什么DFS Used .Non DFS Used等于0 .所有的节点都是 ...

  2. ActiveMQ in Action(6) - Features

    关键字: activemq 2.6 Features    ActiveMQ包含了很多功能强大的特性,下面简要介绍其中的几个.2.6.1 Exclusive Consumer    Queue中的消息 ...

  3. eclipse背景主题

    长时间对着电脑敲代码,眼睛受不了啊,果然程序员就没人权吗?之前咱给codeblocks背景换了颜色,去官网上粘粘代码然后替换一下就行了,这里不多说, 这里说的是给ecllipse换主题,按理说ecli ...

  4. sql server 查询出的结果集,拼接某一列赋值给一个变量

    现有表Area 如下: SELECT [Areaid] ,[Areaname] ,[Areapid] FROM [Northwind].[dbo].[Area] 查询结果如下图: 需求:用 “-“ ” ...

  5. 关于centos连接mssql的问题

    今天要搬迁服务器,需要连接sqlserver,然后装了freetds之后死活就是连接不上mssql,最后才发现,原来需要配置freetds,只需要加上下面一段即可: [192.168.10.12] h ...

  6. ubuntu tips

    1.ibus-setup 2.tips:

  7. Major and minor numbers

    The major nuber is the driver associated with the device, while the minor number is used by the kern ...

  8. Memory and Scores

    Memory and Scores 题目链接:http://codeforces.com/contest/712/problem/D dp 因为每轮Memory和Lexa能取的都在[-k,k],也就是 ...

  9. Chapter 2 Open Book——24

    Mike kept up a string of complaints on the way to building four. mike去教学楼的路上一直嘀咕抱怨着. Once inside the ...

  10. IOS开发小技巧,知识点

    1.IOS模拟器第一次打开需要进入“设置”中关掉"Auto-Capitalization"选项. 2.NSInteger转化 NSString类型: [NSString strin ...