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. jQuery实现下拉框默认选中

    <form class="form-inline" method="post" action="/score_result/"> ...

  2. Execl数据上传到数据库

    =============================================================asp.net================================ ...

  3. CSS雪碧图(精灵图)使用

    1:CSS雪碧图:CSS雪碧图 即 CSS Sprites,也有人叫它CSS精灵图. 2:雪碧图的由来:一个网站的页面需要大量的小图片或者小图标,但是大量的图片如果放在服务器上,每次当打开网站并且向服 ...

  4. 配置基于服务器认证的Dynamics 365 Customer Engagement和SharePoint Online集成

    我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...

  5. synchronized凭什么锁得住?

    相关链接: <synchronized锁住的是谁?> 我们知道synchronized是重量级锁,我们知道synchronized锁住的是一个对象上的Monitor对象,我们也知道sync ...

  6. Redis和MongoDB区别

    MongoDB 更类似 MySQL,支持字段索引.游标操作,其优势在于查询功能比较强大,擅长查询 JSON 数据,能存储海量数据,但是不支持事务.Redis 是一个开源(BSD许可)的,内存中的数据结 ...

  7. Samba共享文件

    1 安装samba yum install -y samba* 2 添加用户 useradd smbuser 3 设置共享文件用户的密码 smbpasswd -a smbuser 4 创建公共共享文件 ...

  8. mysql 事务四要素杂谈

    事务四要素 对于数据库来说,并发性和准确性是数据库需要权衡的两个点. 类似于我们的应用系统,又要要性能还要要准确. 数据准确性这一条来说,最好的控制就是串行化,都别急,一个一个来.这样数据就没问题了. ...

  9. [译]Vulkan教程(26)描述符池和set

    [译]Vulkan教程(26)描述符池和set Descriptor pool and sets 描述符池和set Introduction 入门 The descriptor layout from ...

  10. java之JavaBean

    JavaBean是一种java语言编写而成的可重用组件. 所谓JavaBean,是指符合以下标准的java类: 类是公共的: 有一个无参的公共构造器: 有属性,属性一般是私有的,且有对应的set.ge ...