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的更多相关文章

  1. LeetCode解题笔记 - 2. Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

  2. 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 ...

  3. 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 ...

  4. 110.Balanced Binary Tree Leetcode解题笔记

    110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...

  5. 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 ...

  6. LeetCode解题笔记 - 3. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  7. LeetCode解题笔记 - 20. Valid Parentheses

    这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. ViewAnimator

    ViewAnimator是一个基类,它继承了FrameLayout,因此它表现出FrameLayout的特征,可以将多View组件叠在一起.ViewAnimator额外增加的功能正如它的名字所暗示的, ...

  2. 理解ES6的新数据类型:Symbol

    ES6之前的数组类型 在ES6之前JS只有6种数据类型,分别是:Undefined.Null.布尔值(Boolean).字符串(String).数值(Number).对象(Object). ES6引入 ...

  3. 使用 Anydesk 5.1 TCP 通道(端口映射)功能从外网方便访问内网的 web/数据库等资源

    Anydesk 5.1 带来一个新的功能:TCP 通道,在家办公时,通过互联网进行远程桌面连接到公司电脑,可以将家用电脑的某个端口,映射到公司网络的某个电脑( IP + 端口),不局限于被远程桌面连接 ...

  4. CODING 受邀参与 DevOps 标准体系之系统和工具&技术运营标准技术专家研讨会

    2019 年 5 月 24-25 日,国内领先的一站式 DevOps 解决方案供应商 CODING 作为腾讯云的深度合作伙伴,受邀参加在成都举行的由 TC608 云计算标准和开源推进委员会主办,中国信 ...

  5. Zabbix自定义监控项(模板)

    虽然Zabbix提供了很多的模板(简单理解为监控项的集合),在zabbix界面点击share按钮就可以直接跳到模板大全的官方网站,但是由于模板内的监控项数量太多不好梳理且各种模板质量参差不齐,还是建议 ...

  6. 清除Windows系统图标缓存

    如果改变程序图标重新编译之后看到的图标并未改变,这可能不windows缓存了之前的图标导致的,需要清除Window的图标缓存来显示正确的图标. 下面是清除Windows系统图标缓存的批处理代码: re ...

  7. 给定数轴上的n个点,求距离最近的两个点的距离

    public class MinimumSpacing { //给定平面上的n个点,求距离最近的两个点的距离. //无从下手的话,先分解问题,分解成简单的,逐个分析,然后再合在一起考虑 //这是个2维 ...

  8. Vue+Vuex初体验

    首先: 安装vuex npm install vuex -S 需要有两个组件(HelloWord.vue 和 HelloDemo.vue)[组件自定义] 注册路由 注册store 测试 一.需要有两个 ...

  9. java基础 - 形参和实参,值传递和引用传递

    形参和实参 形参:就是形式参数,用于定义方法的时候使用的参数,是用来接收调用者传递的参数的. 形参只有在方法被调用的时候,虚拟机才会分配内存单元,在方法调用结束之后便会释放所分配的内存单元. 因此,形 ...

  10. 数组类的创建——DynamicArray.h

    完成DynamicArray类的具体实现 DynamicArray设计要点——类模板 动态确定内部数组空间的大小 实现函数返回数组长度 拷贝构造和赋值操作 DynamicArray类的声明 templ ...