LeeCode_01_Two sum
- 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
从第二种解法来看比之前单独循环来说要好很多了。
但是还是有缺点,先总结:
- 以空间换时间。
- 第二次解法中的思路是利用hashmap去重新记录 值以及值的索引。利用高效率的hash算法得到数据。
- 缺点: 看了下其他人的算法,整理:
a.没必要去纯粹所有的数据的索引,有浪费。
b.使用hash去提高效率是ok,但自己开始有些地方没想明白,钻牛角尖了。。。。i. 数是由数组中的数据相加得到。
ii.能利用已知数得到另一个需要求的数,,,,,利用的数,不能一开始就假设是第一个。。。。
如果假设利用的数是第二数,那么第一个数必定在 我们的hashmap里面 :只需要去检测hashmap,如果没有则放入,然后循环。
好僵!!!!!!!!!!!!!!
and by the way:
3ms 那个代码,没看懂。。。。。。。。。
LeeCode_01_Two sum的更多相关文章
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [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 ...
随机推荐
- POJ 2299 Ultra-QuickSort 求逆序数 (归并或者数状数组)此题为树状数组入门题!!!
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 70674 Accepted: 26538 ...
- VPP(Vector Packet Processing)浅析
VPP简介 VPP(Vector Packet Processing)是思科旗下的一款可拓展的开源框架,提供容易使用的.高质量的交换.路由功能 特点:高性能.运行在普通的cpu上. 优点:高性能.技术 ...
- cocos2d-x 音效中断问题
做跑酷重吃金币播音效时,播放其它音效会使得音效所有中断,最后发现时音效上限的问题,2.2.3默认的似乎是5个音效,改动成50后问题解决. 在java中的org.cocos2dx.lib包下有一个Coc ...
- ZJOI2008]树的统计(树链剖分,线段树)
题目描述 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w. 我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u ...
- shell习题第6题:监听80端口
[题目要求] 写一个脚本,判断本机的80端口(加入服务为httpd)是否开启,如果开启就什么都不做,如果发现端口不存在,那么重启一下httpd服务,并发邮件通知相关人员 [核心要点] 检测80端口使用 ...
- TP5.0中多图上传文件名重复问题
最近在做项目的时候出现了一个问题,这里记录一下: 问题: 使用TP5.0框架自带的文件上传方法后,发现多图上传可能会出现文件名重复的问题. 问题代码: 找到TP5框架上传文件命名方法,/thinkph ...
- [原]nginx 一下快一下慢的问题
在本机用thinkphp建了一个小网站,没任何问题,发布到云空间,就出现访问很慢的情况,而且是一下快一下慢,奇数次快,偶数次慢 换了一台win10的笔记本,情况一样,更新了phpstudy更新了thi ...
- Flume的介绍和简单操作
Flume是什么 Flume是Cloudera提供的一个高可用的,高可靠的,分布式的海量日志采集.聚合和传输的系统,Flume支持在日志系统中定制各类数据发送方,用于收集数据:同时,Flume提供对数 ...
- 树莓3B+_root密码开启
开启root用户的方法:1.设置密码:sudo passwd2.sudo passwd --unlock root3.root用户登录:su
- exynos4412—链接脚本复习
在u-boot下,定义变量, 编译,编译完后 使用arm-linux-nm arm 没有去头的二进制可执行文件 都在BSS段,均为初始化. 打印之后会出算随机值. 目前还处于uboot阶段,如 ...