leetcode 刷题 数组类 Two Sum
---恢复内容开始---
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 inclices 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 must assume that each input would have exactly one solution.
Input :number ={2,7,11,15},target =9;
Output :index1=1,index2=2
给定一个整数数组,找到两个数字,这样它们就可以加到一个特定的目标数。
函数二和应该返回两个数字的inclices,它们加起来到目标,在哪里。
index1必须小于index2。请注意您返回的答案(包括index1和index2)
不是从零开始的。
您必须假定每个输入都只有一个解决方案。
输入:数量= { 2、7、11、15 },目标= 9;
输出:index1 = 1,index2 = 2
---恢复内容结束---
public int[] twoSum(int[] nums, int target) {
if (nums == null || nums.length <= ) {
return new int[];
}
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
// key = target - nums[i], just one solution
for (int i = ; i < nums.length; i++) {
map.put(target - nums[i], i);
}
for (int i = ; i < nums.length; i++) {
Integer v = map.get(nums[i]);
// can't use itself
if (v != null && v != i) {
return new int[] { i + , v + };
}
}
return null;
}
leetcode 刷题 数组类 Two Sum的更多相关文章
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
- (python)leetcode刷题笔记 01 TWO SUM
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...
- 【leetcode刷题笔记】Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- 【leetcode刷题笔记】Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【leetcode刷题笔记】Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- 【leetcode刷题笔记】Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- LeetCode刷题总结-数组篇(上)
数组是算法中最常用的一种数据结构,也是面试中最常考的考点.在LeetCode题库中,标记为数组类型的习题到目前为止,已累计到了202题.然而,这202道习题并不是每道题只标记为数组一个考点,大部分习题 ...
- LeetCode刷题总结-数组篇(下)
本期讲O(n)类型问题,共14题.3道简单题,9道中等题,2道困难题.数组篇共归纳总结了50题,本篇是数组篇的最后一篇.其他三个篇章可参考: LeetCode刷题总结-数组篇(上),子数组问题(共17 ...
- LeetCode刷题总结-数组篇(中)
本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...
随机推荐
- 《剑指offer》第五十三题(数字在排序数组中出现的次数)
// 面试题53(一):数字在排序数组中出现的次数 // 题目:统计一个数字在排序数组中出现的次数.例如输入排序数组{1, 2, 3, 3, // 3, 3, 4, 5}和数字3,由于3在这个数组中出 ...
- C# ---- GC中代的递增规律
只有当对象所在代被 Collect 了,改对象所在代才会加 1 ,代值最大为 2 示例1: using System; namespace myMethod { class People{} clas ...
- delphi 程 序从exe运行改成dll库
第一种方法: 具体步骤: 1.生成新的或着打开已经存在的工程文件(DPR)(Project1.dpr). 2.选择[View]—>[Project Manager],选中[ProjectGrou ...
- 20165303学习基础和C语言基础调查
20165303学习基础和C语言基础调查 技能学习心得 我认为我的乒乓球打的还不错,不能说非常好,但是基本的一些技巧都还是会的,小时候爸爸就非常爱看乒乓球比赛,有时候也带着我一起看,最开始看的时候我发 ...
- Mac必备神器之Go2Shell
一.作用 可以快速在当前目录打开Shell命令行窗口 二.安装 1.打开官网 http://zipzapmac.com/go2shell 2.点击下载并安装 3.点击应用图标 三. ...
- python模块--pickle&json&shelve
使用file文件处理时,写入的必须是str ,否则会报错. 例如:要把一个字典写入文件,写入时会报错 ,就算转换成str格式写入,读取的时候也不能按照dict格式读. >>> inf ...
- php url处理
http_build_query() $data = array("name"=>"callback" , "value"=>& ...
- python3 设置滚动条
#!python3#coding=utf-8from selenium import webdriverfrom selenium.webdriver.common.by import Byimpor ...
- python基础之lambda,sort,filter,map,递归函数的运用
内容梗概:1. lamda 匿名函数2. sorted()3. filter()4. map()5. 递归函数 1.lambda 形式: lambda 参数:返回值 f = lambda x,y: x ...
- php 中输入输出提交
</head> <body> 输出的两个位置 <? echo $_POST['sub']; ?> <form action="" meth ...