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 ...
随机推荐
- 深度好文:PHP写时拷贝与垃圾回收机制(转)
原文地址:http://www.php100.com/9/20/87255.html 写入拷贝(Copy-on-write,简称COW)是一种计算机程序设计领域的优化策略.其核心思想是,如果有多个调用 ...
- Complete_NGINX_Cookbook
Complete NGINX Cookbook 下载地址:Complete NGINX Cookbook
- windows重装系统后grub引导菜单修复方法(亲自实验过)
问题: 电脑安装的是windows7+ubuntu 15.10双系统.windows重装后,grub引导界面消失. 解决方法有两大步: 1.进入ubuntu; 2.在ubuntu中修复grub. 一. ...
- Struts2.0
流程详解: 配置详解: constant (常用常量配置) 使用Struts2 框架 ,先要导入Struts2 需要的jar 包 , 通过配置中央控制器 以及web.xml 来实现 Str ...
- Nginx + FastCGI + Django在windows上部署及nginx常用命令
一般应用都是部署在linux系统上,不会在windows上部署,emmm..所以有兴趣的就瞧瞧吧哈哈 nginx工作原理: nginx用于处理静态文件,动态部分经由fastcgi .scgi或uWSG ...
- MVC(实战二:网址映射)
一.默认网址 Global.asax 默认网址配置: 此时打开编辑页面,效果如下: ?后面包含name参数和Email参数 在Global中修改网址路由: public static void Re ...
- pytorch 中的Variable一般常用的使用方法
Variable一般的初始化方法,默认是不求梯度的 import torch from torch.autograd import Variable x_tensor = torch.randn(2, ...
- 12. Go 语言文件处理
Go 语言文件处理 本章我们将带领大家深入了解一下 Go语言中的文件处理,重点在于文件而非目录或者通用的文件系统,特别是如何读写标准格式(如 XML 和 JSON 格式)的文件以及自定义的纯文本和二进 ...
- faster-rcnn训练自己数据+测试
准备使用faster-rcnn进行检测实验.同时笔者也做了mask-rcnn,yolo-v3,ssd的实验,并进行对比. window下使用faster-rcnn https://blog.csdn ...
- 【Java】导入项目时,出现The project cannot be built until build path errors are resolved错误解决方法
先检查jar包,jar包的地址如果不一样需要remove后重新导入的,右键项目→Build Path. 看额外的jar包有没有×,地址正不正确,要是不正确,remove错误jar包,再点击Add Ex ...