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. WinForm 无边框窗体和timer控件

    一.无边框窗体 1.控制按钮如何制作就是放置可以点击的控件,不局限于使用按钮或是什么别的,只要可以点击能触发点击事件就可以了 做的好看一点,就是鼠标移入,移出,按下三个事件会让按钮改变样式 如何获取图 ...

  2. yii2.0 面包屑的使用

    yii2中面包屑是yii2自带的小部件,类似本网站的导航栏应该就是采用面包屑来完成的 例子如下,需要引入 yii\widgets\Breadcrumbs echo Breadcrumbs::widge ...

  3. Linux中的shell函数编写

    function huge_cp() { while read line1; do cp $line1 ../; done; } function huge_rm() { while read lin ...

  4. OC 调用JS 代码 处理HTML5 实战

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; ...

  5. 批量安装XP补丁的命令

    方法一: 新建一个bat文件,把这个文件和所有补丁文件放在同一个目录下,双击运行. BAT代码如下 @echo offfor %%i in (*.exe) do %%i /passive /nores ...

  6. servlet之过滤器(转载)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 一.Filter 1.过滤器的概念 Java中的Filter 并不是一个标准的Servlet ...

  7. 第四天 内置函数2 随机码 装饰器 迭代器、生成器 递归 冒泡算法 JSON

    关于函数的return li = [11,22,33,44] def f1(arg): arg.append(55) li = f1(li) print(li) 因为li = f1(li) 实际赋值的 ...

  8. Python os.path模板函数

    os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径) ...

  9. 23个mysql查询语句

    一查询数值型数据: SELECT * FROM tb_name WHERE sum > 100; 查询谓词:>,=,<,<>,!=,!>,!<,=>,= ...

  10. 使用Toad创建存储过程出现错误并解决

    存储过程中遇到ora-00942表或视图不存在 CREATE OR REPLACE PROCEDURE p IS CURSOR c IS SELECT * FROM scott.emp FOR UPD ...