题目描述:

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

My solution(50ms,38.5MB)

class Solution {
public int[] twoSum(int[] nums, int target) {
int[] result = 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){
result[0] = i;
result[1] = j;
}
}
}
return result;
}
}

以下是标准答案:

Approach 1: Brute Force(16ms,38.5MB)

class Solution {
public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[j] == target - nums[i]) {
return new int[] { i, j };
}
}
}
throw new IllegalArgumentException("No two sum solution");
}
}

Approach 2: Two-pass Hash Table(2ms,38.1MB)

class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i);
}
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement) && map.get(complement) != i) {
return new int[] { i, map.get(complement) };
}
}
throw new IllegalArgumentException("No two sum solution");
}
}

Approach 3: One-pass Hash Table(2ms,38.2MB)

class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {//判断键名是否包含complement
return new int[] { map.get(complement), i };
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("No two sum solution");
}
}

LeetCode第一题—— Two Sum(寻找两数,要求和为target)的更多相关文章

  1. LeetCode 18: 4 Sum 寻找4数和

    链接 4Sum 难度 Medium 描述 Given an array nums of n integers and an integer target, are there elements a , ...

  2. leetcode 刷题(2)--- 两数相加

    给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...

  3. LeetCode OJ:Two Sum(两数之和)

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  4. leetcode第一题--two sum

    Problem:Given an array of integers, find two numbers such that they add up to a specific target numb ...

  5. leetcode 刷题(1)--- 两数之和

    给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], target ...

  6. LeetCode算法题-Two Sum II - Input array is sorted

    这是悦乐书的第179次更新,第181篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第38题(顺位题号是167).给定已按升序排序的整数数组,找到两个数字,使它们相加到特定 ...

  7. 乘风破浪:LeetCode真题_040_Combination Sum II

    乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...

  8. leecode刷题(8)-- 两数之和

    leecode刷题(8)-- 两数之和 两数之和 描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输 ...

  9. 乘风破浪:LeetCode真题_039_Combination Sum

    乘风破浪:LeetCode真题_039_Combination Sum 一.前言     这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...

随机推荐

  1. mysql 导出导入数据 -csv

    MySql数据库导出csv文件命令: mysql> select first_name,last_name,email from account into outfile 'e://output ...

  2. Devexpress ProgressBarControl的使用

    1 private void simpleButton1_Click(object sender, EventArgs e) 3 { 4 //设置一个最小值 5 progressBarControl1 ...

  3. Day 7 :一句话Python(匿名函数-lambda,三元运算,列表表达式,生成器表达式)

    注意: 1.所有的列表表达式都可以转换成生成器表达式 2.经量让标傲世简化你得操作,增加代码可读性 3.如果代码过于复杂,应该转换成普通代码 4.再代码中尽可能多使用生成器表达式. 三元运算符:简化代 ...

  4. 牛客B-Xor Path /// 求所有Path( i->j )( j >= i )路径的异或和

    题目大意: https://ac.nowcoder.com/acm/contest/272/B?&headNav=acm 给定一棵n个点的树,每个点有权值.定义表示  到  的最短路径上,所有 ...

  5. QTP,自己主动化測试学习笔记,六月九号

    測试自己主动化实现的两个难点设计--功能分解 实现--对象的识别 測试自己主动化实现的两个难点-功能分解 清晰画出业务流程图 依据业务流程分解业务功能.能够被复用的功能也要被分解出来. 依照路径覆盖的 ...

  6. 整理及优化CSS代码的7个原则

    作为网页设计师(前端工程师),你可能还记得曾经的那个网页大小建议:一个网页(包括HTML.CSS.Javacript.Flash和图片)尽量不要超过30KB的大小,随着互联网的日益庞大,网络带宽也在飞 ...

  7. PHP算法之猜数字

    小A 和 小B 在玩猜数字.小B 每次从 1, 2, 3 中随机选择一个,小A 每次也从 1, 2, 3 中选择一个猜.他们一共进行三次这个游戏,请返回 小A 猜对了几次? 输入的guess数组为 小 ...

  8. 数据库MySQL--分组查询

    事例使用文件:https://files.cnblogs.com/files/Vera-y/myemployees.zip 分组数据:group by 子句 分组查询语法: select 分组函数,列 ...

  9. LeetCode第二题—— Add Two Numbers(模拟两数相加)

    Description: You are given two non-empty linked lists representing two non-negative integers. The di ...

  10. Eclipse中如何使用Hibernate

    首先创建一个java web项目,其目录如下: (1)创建文件夹hibernate4(用于存放下载的hibernate工具包lib/required文件夹下所有的jar包),jar包目录如下: (2) ...