LeetCode——TwoSum
题目:
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
我的解法:
public class Solution {
public int[] twoSum(int[] numbers, int target) {
int [] re=new int[2];
int len=numbers.length;
for(int i=0;i<len;i++){
for(int j=i+1;j<len;j++){
if(numbers[i]+numbers[j]==target){
re[0]=i+1;
re[1]=j+1;
}
}
}
return re;
}
}
系统给出结果:
Time Limit Exceeded
显然系统无法忍受我时间复杂度为O(n^2)的时间复杂度。
在讨论版看到一个复杂度为O(n)的算法:
import java.util.Hashtable;
public class Solution {
public int[] twoSum(int[] numbers, int target) {
int [] re=new int[2];
Hashtable<Integer,Integer> hashtable=new Hashtable<Integer,Integer>();
int len=numbers.length;
for(int i=0;i<len;i++){
Integer n=hashtable.get(numbers[i]);
if(n==null) hashtable.put(numbers[i],i);
n=hashtable.get(target-numbers[i]);
if(n!=null&&n<i){
re[0]=n+1;
re[1]=i+1;
return re;
} }
return re;
}
}
LeetCode——TwoSum的更多相关文章
- leetcode — two-sum
package org.lep.leetcode.twosum; import java.util.Arrays; import java.util.HashMap; import java.util ...
- leetCode:twoSum 两数之和 【JAVA实现】
LeetCode 两数之和 给定一个整数数组,返回两个数字的索引,使它们相加到特定目标. 您可以假设每个输入只有一个解决方案,并且您可能不会两次使用相同的元素. 更多文章查看个人博客 个人博客地址:t ...
- [leetcode]TwoSum系列问题
1.普通数组找两个数,哈希表建立数值和下标的映射,遍历时一边判断一边添加 /* 哇,LeetCode的第一题...啧啧 */ public int [] twoSum(int[] nums, int ...
- [LeetCode] TwoSum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- 【C语言工具】AddressSanitizer - 内存检测工具
Github 地址:https://github.com/google/sanitizers Wiki 地址:https://github.com/google/sanitizers/wiki/Add ...
- LeetCode初体验—twoSum
今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...
- LeetCode #1 TwoSum
Description Given an array of integers, return indices of the two numbers such that they add up to a ...
- Leetcode 1——twosum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- leetcode题解 1.TwoSum
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...
随机推荐
- HDU 1000 A + B Problem
#include int main() { int a,b; while(scanf("%d%d",&a,&b)!=EOF) printf("%d\n&q ...
- iOS Dev (67) 单例的实现
iOS Dev (67) 单例的实现 博客:http://blog.csdn.net/prevention 作者:大锐哥 摘自:Learn iPhone and iPad cocos2d Game D ...
- mahout贝叶斯算法开发思路(拓展篇)1
首先说明一点,此篇blog解决的问题是就下面的数据如何应用mahout中的贝叶斯算法?(这个问题是在上篇(...完结篇)blog最后留的问题,如果想直接使用该工具,可以在mahout贝叶斯算法拓展下载 ...
- STM32菜鸟成长记录---RS485通讯协议的应用
写作原因:近来蛋闲?非也 ! 昨天一同事合作的项目代码出现的bug-----他的上位机每200ms给我发送命令向我这边下位机读取一些数据,在此过程会按下按键做一些另外操作并给他返回数据:(通信是通 ...
- c++ 输出虚函数表内容
class Base{ public: virtual void f(){cout<<"Base::f"<<endl;} virtual void g(){ ...
- flash builder 4 编译器参数
accessible=true|false 是否具有可理解性(如为残疾人提供方便的性能) actionscript-file-encoding | 设置文件编码,如Shitf_JIS advanced ...
- Oracle游标循环更新数据案例
declare v_XTXMBH number; v_ZJZJZJRQ varchar2(40); cursor c_job is SELECT XT.XTXMBH AS XTXMBH, QJ.ZJZ ...
- 使用约束给tableview添加暂无数据的提示
在tableview没有数据的时候,我们经常需要在tableview的正中间插入一张图片,来提示用户暂无数据 /** * 添加一个view,提示暂无数据 */ - (void)addPromptVie ...
- BZOJ 1202: [HNOI2005]狡猾的商人( 差分约束 )
好像很多人用并查集写的... 前缀和, 则 sumt - sums-1 = v, 拆成2条 : sumt ≤ sums-1 + v, sums-1 ≤ sumt - v 就是一个差分约束, 建图跑SP ...
- BZOJ 1691: [Usaco2007 Dec]挑剔的美食家( 平衡树 )
按鲜嫩程度排个序, 从大到小处理, 用平衡树维护价值 ---------------------------------------------------------------------- #i ...