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. Java 序列化 序列化与单例模式 [ 转载 ]

    Java 序列化 序列化与单例模式 [ 转载 ] @author Hollis 本文将通过实例+阅读Java源码的方式介绍序列化是如何破坏单例模式的,以及如何避免序列化对单例的破坏. 单例模式,是设计 ...

  2. 【jquery、XML】jquery通过按钮使打开select

    <select> <option>aaaaa</option> <option>bbbbb</option> <option>c ...

  3. C# 语言规范_版本5.0 (第6章 转换)

    1. 转换 转换(conversion) 使表达式可以被视为一种特定类型.转换可导致将给定类型的表达式视为具有不同的类型,或其可导致没有类型的表达式获得一种类型.转换可以是隐式的 (implicit) ...

  4. Form提交时隐藏Token验证

    前端声称一个Token @Html.AntiForgeryToken() 后台对Token进行验证 AntiForgery.Validate();

  5. abowman

    http://abowman.com/google-modules/ball-clock/

  6. 定时帧:NSTimer和CADisplayLink

    学习参考了:http://www.jianshu.com/p/c35a81c3b9ebhttps://zsisme.gitbooks.io/ios-/content/chapter11/frame-t ...

  7. Boolean对象 识记

    Boolean 对象表示两个值:"true" 或 "false". 1.创建 new Boolean(value); //构造函数 返回 对象+返回值 Bool ...

  8. Chloe and pleasant prizes

    Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  9. redis34--string 操作

    String类型操作 1.set key value 设置key对应的值为string类型的value  2.mset key1 value1 - keyN valueN 一次设置多个key的值 3. ...

  10. 重点+超详细:ajax和json及案例

    不用jQuery的ajax流程 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "ht ...