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. Zabbix自定义监控项(模板)

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

  2. 12.2 中的Data Guard Standby 密码文件自动同步 (Doc ID 2307365.1)

    Data Guard Standby Automatic Password file Synchronization in 12.2 (Doc ID 2307365.1) APPLIES TO: Or ...

  3. STM32 HAL_Deleay() 函数 导致程序卡死

    出现问题场景:   我的程序有RTOS操作系统.使用的驱动库是STM32官方最新的HAL库. 移植好LwIP以太网协议后,在初始化网卡阶段程序卡死.   出现问题原因:   后经过蠢笨的printf打 ...

  4. centos8 yum 安装 rabbitmq

    进入/etc/yum.repos.d/ 文件夹创建rabbitmq-erlang.repo 文件内容如下[rabbitmq-erlang] name=rabbitmq-erlangbaseurl=ht ...

  5. Linux系统学习 十一、DHCP服务器—相关文件、配置文件、服务器配置

    2.DHCP服务器相关文件 安装SHCP服务器 yum install dhcp 对应的端口 端口号: ipv4 udp67.udp68(不推荐改端口) ipv6 udp546.udp547(暂时还没 ...

  6. 用python暴力破解压缩文件并不是万能,至少这个场景我告诉你密码你用代码也破解不了

    看到论坛上各种贴子写用python进行暴力破解的文章,于是自己也想去尝试一下,不试不知道,一试吓一跳,真的就像那句有名的”python由入门到放弃“,把论坛上别人的脚本全部自己敲一遍,运行不报错,但也 ...

  7. 如何使用第三方ui库vant-weapp

    如何使用第三方ui库vant-weapp 1==>创建文件夹demo 2==> 在小程序 中打开 注意 要先在小程序中打开 如果要想在小程序的开发工具中打开某一个 文件夹 要么是空文件夹 ...

  8. python操作excel表

    1.新增表并添加数据: 2.给工作表添加表名称,给表数据添加格式: import xlsxwriterdatas=(['Rent',1000], ['Gas',100], ['fish','画画'], ...

  9. 使用ClickOnce发布Windows应用程序

    前言 因本人工作需要,在一名非常非常好的老师的指导下,入门了C#,再次向老师表示感谢. 本人平时经常遇到的业务就是将数据下发给各部门,并让各部门再上报,此过程中经常会遇到数据格式不正确,数据错误等诸多 ...

  10. 开发一个这样的 APP 要多长时间?

    作者:蒋国刚 www.cnblogs.com/guogangj/p/4676836.html 这是一个“如有雷同,纯属巧合”的故事,外加一些废话,大家请勿对号入座.开始了…… 我有些尴尬地拿着水杯,正 ...