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 ...
随机推荐
- [转]WIBKIT技术资料
WebKit结构和流程分析 http://inedx.blog.hexun.com/28830354_d.html webkit架构 http://inedx.blog.hexun.com/28795 ...
- SPOJ LCS(Longest Common Substring-后缀自动机-结点的Parent包含关系)
1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...
- 学习日记之模板方法模式和 Effective C++
模板方法模式: 定义:定义一个操作中的算法的骨架.而将一些步骤延伸到子类中.模板方法使得子类能够不改变算法的结构就可以重定义该算法的某些特定步骤. (1),用了继承,而且肯定这个继承有意义的情况下.就 ...
- 如何让HTML的编写更具结构性
首先声明,我不是搞技术的,很多词汇写的不够专业,但作为一枚菜鸟,我站在菜鸟的角度,来讲述我在学习技术的过程中所遇到的问题,和解决的方案. 入门HTML还算简单,无非是先写好固定的三对开闭标签结构:ht ...
- 原生JS实现字符串分割
window.onload = function(){ var str = 'abc,dbc,qqq,aaa'; var sp = split(str,',')//与字符串的分隔符要一直. alert ...
- linux命令:rsync, 同步文件和文件夹的命令
Usage: rsync [OPTION]... SRC [SRC]... DEST or rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST or ...
- wget 无法下载jdk的处理办法
完整语句:wget --no-cookie --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle. ...
- Stbdroid之ShapeDrawable
Shape可以定义矩形.椭圆形.线条.圆形 <?xml version="1.0" encoding="utf-8"?> <shape xml ...
- 4.7 《硬啃设计模式》 第24章 麻烦的多角关系 - 中介者模式(Mediator Pattern)简介
在Windows程序中,有时候界面控件之间的交互会很麻烦,如:A控件显示什么的时候,B控件要显示什么,另外C控件要不可用,同样其它控件也会有类似的复杂要求.控件与控件之间很容易形成复杂的多角关系了.现 ...
- Android的回调模拟
想要彻底理解安卓中用的回调,最好的办法是自己写一个类似的实现安卓中回调功能的实现方法. 我自己写了一个可以实现setOnClickListener回调的工程: 具体目录: 工程源码的具体地址:http ...