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

翻译:

Sum : 和

indices:索引

assume:假设

给定一系列数,以及一个指定的值

现在要选出两个数,使其和等于指定数。

假设:1.必有唯一解;2.给出的数字仅能使用一次。

解法

a.循环的去求解。

时间复杂度n(n-1)

class Solution {
public int[] twoSum(int[] nums, int target) {
int [] result =new int[2];
int result_01;
int result_02; int numsSize = nums.length;
for(int i = 0; i < numsSize; i++){
result_01 = nums[i];
result_02 = target - result_01;
for(int j =i+1; j < numsSize; j++ ) {
if(nums[j] == result_02){
result[0] = i;
result[1] = j;
return result;
}
}
}
return result;
}
}

19 / 19 test cases passed.

Status: Accepted

Runtime: 38 ms

b.需要对数组有更多的理解。。。。

自己觉得这道题的意义应该不是想上面的解法那样,单纯是对数组循环。

数组

下标:偏移量。

值:数组中存放的值

偏移量和值一一对应。

求解:两个特定值,值得和等于指定数。

数组中对对应值的查找方式: 遍历,二分等。应用场景是有区分的。。。。

现成的方法? 对方法内部实现的理解?对下标或值的操作的封装。

提高点:

1.map/字典/hashmap

应该可以把字典转换一下形式。利用高效率的方法。

------------------------------------2017/09/20--------------------------------------

我的其他解法:

class Solution {
public int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
Integer result_01;
Integer result_02;
int i; // 数值 ,,位置
List<Integer> indexList;
HashMap<Integer, List<Integer>> dataHashMap = new HashMap<>();
for (i = 0; i < nums.length; i++) {
result_01 = nums[i];
if (dataHashMap.containsKey(result_01)) {
indexList = dataHashMap.get(result_01);
indexList.add(i);
} else {
indexList = new ArrayList<>();
indexList.add(i);
}
dataHashMap.put(result_01, indexList);
} for (i = 0; i < nums.length; i++) {
result_01 = nums[i];
result_02 = target - result_01;
if (dataHashMap.containsKey(result_02)) {
indexList = dataHashMap.get(result_02);
if (indexList.get(0) != i) {
result[0] = i;
result[1] = indexList.get(0);
return result;
} else if (indexList.size() > 1) {
result[0] = i;
result[1] = indexList.get(1);
return result;
}
}
}
return result;
}
}

19 / 19 test cases passed.

Status: Accepted

Runtime: 13 ms

从第二种解法来看比之前单独循环来说要好很多了。

但是还是有缺点,先总结:

  1. 以空间换时间。
  2. 第二次解法中的思路是利用hashmap去重新记录 值以及值的索引。利用高效率的hash算法得到数据。
  3. 缺点: 看了下其他人的算法,整理:

    a.没必要去纯粹所有的数据的索引,有浪费。

    b.使用hash去提高效率是ok,但自己开始有些地方没想明白,钻牛角尖了。。。。

i. 数是由数组中的数据相加得到。

ii.能利用已知数得到另一个需要求的数,,,,,利用的数,不能一开始就假设是第一个。。。。

如果假设利用的数是第二数,那么第一个数必定在 我们的hashmap里面 :只需要去检测hashmap,如果没有则放入,然后循环。

好僵!!!!!!!!!!!!!!

and by the way:

3ms 那个代码,没看懂。。。。。。。。。

LeeCode_01_Two sum的更多相关文章

  1. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  2. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  3. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  8. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  9. [LeetCode] Sum of Left Leaves 左子叶之和

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

随机推荐

  1. Luogu_4886 快递员

    Luogu_4886 快递员 一道淀粉质的题目. 先考虑最简单的算法,那便是对每个点都求一边.时间复杂度O(NM) 然后如果我们把每个点的结果对应一个高度,我们会发现.最优解是在这个对应高度形成的三维 ...

  2. Linux内存管理-高端内存(二)

    在支持MMU的32位处理器平台上,Linux系统中的物理存储空间和虚拟存储空间的地址范围分别都是从0x00000000到0xFFFFFFFF,共4GB,但物理存储空间与虚拟存储空间布局完全不同.Lin ...

  3. Web—13-判断网站请求来自手机还是pc浏览器

    判断网站请求来自手机还是pc浏览器 #判断网站来自mobile还是pc def checkMobile(request): """ demo : @app.route(' ...

  4. 温故vue对vue计算属性computed的分析

    vue 复习笔记(1)一段时间没有看过vue的官方文档了,温故而知新,所以我决定将vue的文档在看一遍 1计算属性computed在vue的computed中声明的是计算属性,可以使用箭头函数来进行定 ...

  5. C++练习 | 模板与泛式编程练习

    #include <iostream> #include <cmath> #include <cstring> #include <string> #i ...

  6. MYSQL 5.7.25最后一个5.x版本记录

      一:下载 位 https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.25-win32.zip 位 https://dev.mysql.co ...

  7. activeMQ的request-response请求响应模式

    一:为什么需要请求响应模式 在消息中间中,生产者只负责生产消息,而消费者只负责消费消息,两者并无直接的关联.但是如果生产者想要知道消费者有没有消费完,或者用不用重新发送的时候,这时就要用到请求响应模式 ...

  8. 前端使用mobx时,变量已经修改了,为什么组件还是没变化,map类型变量,对象类型变量的值获取问题(主要矛盾发生在组件使用时)

    前天我在使用一个前端多选框组件时遇到了一个问题,明明对象内的值已经修改了,但是组件显示的还是没有效果改变,以下是当时打出的log,我打印了这个对象的信息 对象内的值已经修改了但是组件还是不能及时更改, ...

  9. Call to a member function allowField() on null 错误总结

    Call to a member function allowField() on null 在空对象上调用  allowField() 没有该模型对象无法调用,需要创建相应的模型 错误版本: if ...

  10. Java设计模式(24)——行为模式之解释器模式(Interpreter)

    一.概述 概念 自己定义文法,实际中还是很少出现的,作了解 给出一篇网友的参考博文:http://blog.csdn.net/ylchou/article/details/7594135