LeetCode第一题—— Two Sum(寻找两数,要求和为target)
题目描述:
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)的更多相关文章
- LeetCode 18: 4 Sum 寻找4数和
链接 4Sum 难度 Medium 描述 Given an array nums of n integers and an integer target, are there elements a , ...
- leetcode 刷题(2)--- 两数相加
给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...
- LeetCode OJ:Two Sum(两数之和)
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- leetcode第一题--two sum
Problem:Given an array of integers, find two numbers such that they add up to a specific target numb ...
- leetcode 刷题(1)--- 两数之和
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], target ...
- LeetCode算法题-Two Sum II - Input array is sorted
这是悦乐书的第179次更新,第181篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第38题(顺位题号是167).给定已按升序排序的整数数组,找到两个数字,使它们相加到特定 ...
- 乘风破浪:LeetCode真题_040_Combination Sum II
乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...
- leecode刷题(8)-- 两数之和
leecode刷题(8)-- 两数之和 两数之和 描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输 ...
- 乘风破浪:LeetCode真题_039_Combination Sum
乘风破浪:LeetCode真题_039_Combination Sum 一.前言 这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...
随机推荐
- [JZOJ 5813] 计算
题意:求满足题意的方案数. 思路: 显然的计数类\(dp\). 不难发现,令$f(x) = \prod_{i=1}^{2m}{x_i} \(. 在找一个\)x'\(使得\)f(x') = \prod_ ...
- C 函数指针详解
一 通常的函数调用 一个通常的函数调用的例子://自行包含头文件 void MyFun(int x); //此处的申明也可写成:void MyFun( int ); int main(int a ...
- Linux ifconfig 单网卡配置多网段
1 2 3 4 5 6 7 8 9 10 11 ifconfig eth0 down ifconfig eth0 hw ether 01:02:03:04:05:06 ifconfig eth0 ...
- JavaScript笔记 – 程序语法设计
一.基础语法设计 JavaScript是可以与HTML标记语言混合.用于网页交互式的解释型脚本语言.由国际标准ECMAScript提供核心语言功能.文档对象模型(DOM)提供访问和操作网页内容的方法和 ...
- [转] 如何在vps上安装和登录Xwindows
如何VPS也可以拥有像windows一样图形窗口,这里写个教程,据说xwindows是一个比微软windows还强大的linux图形界面,怎样强大,我也是听说的,你可以自己去试,然后告诉我. vps安 ...
- Git及github使用(二)上传项目
接上篇中创建好的项目. 1.进入到相应的目录右键Git bash here打开客户端 2.创建一个readme文本 $ echo "# Python日常记录积累" >> ...
- 对Springdata模块的简单理解
有关于Spring对数据库的操作属于为Spring中的Springdata模块,对数据库的操作.Spring对JDBC和Mybatis都有封装与简化 可以从以下角度学习研究 SpringData: 1 ...
- LinkedHashMap+Spring Aop实现简易的缓存系统
之前介绍说要做在线文库的系统,当数据量大的时候,根据标签tag的对文档信息的查询将是一个很耗时的工作,原来分析LinkedHashMap源码的时候了解到它有一个双向链表的结构,可以通过将刚被访问的元素 ...
- uboot 的启动过程及工作原理
启动模式介绍 大多数 Boot Loader 都包含两种不同的操作模式:"启动加载"模式和"下载"模式,这种区别仅对于开发人 员才有意义.但从最终用户的角度看, ...
- 如何使用flow进行静态类型检查
Flow 是 facebook 出品的 JavaScript 静态类型检查⼯具.Vue.js 的源码利⽤了 Flow 做了静态类型检查,所以了解 Flow 有助于我们阅读源码. 为什么⽤ Flow? ...