LeetCode解题笔记 - 1. Two Sum
1. Two Sum
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]. 给一个数组,和数,找到数组中合为目标数的两个数,返回其index。
//我的思路叫做没有思路,用了最原始的方案...
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
var i,j;
for (i=0;i<nums.length;i++){
for (j=i+1;j<nums.length;j++){
if(nums[i]+nums[j] === target){
return [i,j]
}
}
}
};
这题确实没有什么思路,去学习一下热门答案
public int[] twoSum(int[] numbers, int target) {
int[] result = new int[2];
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < numbers.length; i++) {
if (map.containsKey(target - numbers[i])) {
result[1] = i + 1;
result[0] = map.get(target - numbers[i]);
return result;
}
map.put(numbers[i], i + 1);
}
return result;
}
其解法利用Map检测是否包含指定key的方法,以给定数组中的value为key,index为value,当Map有(目标值-遍历到的值)的key,则返回key对应的value(index),反之把其添加到Map中。
//那么现在是用js写一遍
//没有引用库,直接使用map,意外吗?不要怀疑,chrome和Firefox已经有Map了,IE10不支持,IE11支持但少了几个方法
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
var map = new Map(),
i;
for (i = 0; i < nums.length; i++) {
if (map.has(target - nums[i])) {
return [map.get(target - nums[i]), i];
}
map.set(nums[i], i);
}
return [];
};
//如果在低版本浏览器怎么办呢,有低版本兼容方法吗
//思考:其解法对map的应用里,一需要可以存储键值对,二需要可以快捷查找key值是否存在;究其本质,只要满足这两个特点,就可以使用
//首先我想到的是对象,但属性名标识符规则不能数字开头,不想再拼接处理,所以放弃了。其实要用对象也很简单,无非key加前缀罢了
//我使用了另外一个东西,数组。咱js数组有个特点,不需要申明长度,不会越界(手动滑稽)恰好解决这个问题key只涉及正整数,果断利用它
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
var arr = [],
i;
for (i = 0; i < nums.length; i++) {
if (arr[target - nums[i]] !== undefined) {
return [arr[target - nums[i]], i];
}
arr[nums[i]] = i;
}
return [];
};
虽然写了js实现,但解法本质是还是学习模仿,希望自己能不断进步,不借鉴,自己写出好方法!
LeetCode解题笔记 - 1. Two Sum的更多相关文章
- LeetCode解题笔记 - 2. Add Two Numbers
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...
- 122. Best Time to Buy and Sell Stock(二) leetcode解题笔记
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
- 121. Best Time to Buy and Sell Stock (一) leetcode解题笔记
121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- 2016/9/21 leetcode 解题笔记 395.Longest Substring with At Least K Repeating Characters
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- LeetCode解题笔记 - 3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- 188. Best Time to Buy and Sell Stock IV leetcode解题笔记
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 123. Best Time to Buy and Sell Stock (三) leetcode解题笔记
123. Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the pric ...
随机推荐
- 松软科技web课堂:随机Math.random()
Math.random() 返回 0(包括) 至 1(不包括) 之间的随机数: 实例 Math.random(); // 返回随机数 JavaScript 随机整数 Math.random() 与 M ...
- 速查 objc中可变集合和不可变集合的遍历性能
次数 : 5,000,000 NSMutableArray //0.131999/0.116085/0.112128 NSArray //0.116842/0.111675/0.108623 NSMu ...
- linux 安装redis服务
下载地址:http://redis.io/download,下载最新稳定版本. 本教程使用的最新文档版本为 2.8.17,下载并安装: $ wget http://download.redis.io/ ...
- No package gcc48-c++ available
yum install gcc48-c++ linux 下编译安装 rocksdb,发现没有这个 gcc48-c++,感觉这个48 应该是版本号,于是在 yum install gcc-c++,安装成 ...
- 使用hutool进行二维码制作
2.在IDEA中使用代码块生成二维码
- Leetcode题解 - 树部分简单题目代码+思路(105、106、109、112、897、257、872、226、235、129)
树的题目中递归用的比较多(但是递归是真难弄 我
- sublime插件开发教程4
写几个简单的例子详解下 import sublime import sublime_plugin class ExampleCommand(sublime_plugin.TextCommand): d ...
- win7怎么使用远程桌面连接(win10类似,同样适用)
win7使用远程桌面比mac要简单多了,只需在桌面点击“开始”,找到并打开“附件”,点击“远程桌面连接”即可 mac使用远程桌面连接:https://www.cnblogs.com/tu-071 ...
- 使用过Redis,我竟然还不知道Rdb
目录 使用过Redis,那就先说说使用过那些场景吧 Rdb文件是什么,它是干什么的 分析工具 小结 联想 推荐阅读 使用过Redis,那就先说说使用过那些场景吧 字符串缓存 //举例 $redis-& ...
- NFS客户端挂载及永久生效
1.NFS客户端挂载的命令格式: 挂载命令 挂载的格式类型 NFS服务器提供的共享目录 NFS客户端要挂载的目录mount -t nfs 服务器IP:/共享目录 /本地的挂载点(必须存在) 重启失效 ...