题目:

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的更多相关文章

  1. leetcode — two-sum

    package org.lep.leetcode.twosum; import java.util.Arrays; import java.util.HashMap; import java.util ...

  2. leetCode:twoSum 两数之和 【JAVA实现】

    LeetCode 两数之和 给定一个整数数组,返回两个数字的索引,使它们相加到特定目标. 您可以假设每个输入只有一个解决方案,并且您可能不会两次使用相同的元素. 更多文章查看个人博客 个人博客地址:t ...

  3. [leetcode]TwoSum系列问题

    1.普通数组找两个数,哈希表建立数值和下标的映射,遍历时一边判断一边添加 /* 哇,LeetCode的第一题...啧啧 */ public int [] twoSum(int[] nums, int ...

  4. [LeetCode] TwoSum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  5. 【C语言工具】AddressSanitizer - 内存检测工具

    Github 地址:https://github.com/google/sanitizers Wiki 地址:https://github.com/google/sanitizers/wiki/Add ...

  6. LeetCode初体验—twoSum

    今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...

  7. LeetCode #1 TwoSum

    Description Given an array of integers, return indices of the two numbers such that they add up to a ...

  8. Leetcode 1——twosum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  9. leetcode题解 1.TwoSum

    1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...

随机推荐

  1. [置顶] SQL日期类型

    在做机房收费系统的时候,上下机,我觉得是我在整个系统中遇到最棘手的问题了,现在就给大家,分享一下,我是怎样解决的. SQL中有3中数据类型是关于日期的,每一种的用法是不同的,当你用错了,就会出现下面这 ...

  2. Js 返回页面 or 跳转页面

    跳出 iframe 在当前页跳转, window.parent.frames.location.href=www.baidu.com" 跳转页面 onclick="history. ...

  3. JavaSript模块化 && AMD CMD 详解.....

    模块化是指在解决某一个复杂问题或者一系列的杂糅问题时,依照一种分类的思维把问题进行系统性的分解以之处理.模块化是一种处理复杂系统分解为代码结构更合理,可维护性更高的可管理的模块的方式.可以想象一个巨大 ...

  4. Judge loop in directed graph

    1 深度优先方法 首先需要更改矩阵初始化函数init_graph() 然后我们需要初始化vist标记数组 深度优先访问图,然后根据是否存在back edge判断是否存在环路 算法如下: #includ ...

  5. VC6集成开发环境使用参考

    VC6集成开发环境使用参考 1.VC6的常用菜单命令项 (1)File菜单 New:打开“new”对话框,以便创建新的文件.工程或工作区. Close Workspace:关闭与工作区相关的所有窗口. ...

  6. HAMA

    http://hama.apache.org/run_examples.html http://www.binospace.com/ http://57832638.iteye.com/blog/20 ...

  7. CMake学习小结

    假定有vegagis工程,工程的目录结构如下: #--vegagis#  |--src 源文件目录#     |--gui 界面工程,输出类型:dll,依赖于QT的QtCore.QtGui.QtXml ...

  8. 正确的lnamp支持SSI的方法!即支持SHTML和include调用!

    正确的lnamp支持SSI的方法!即支持SHTML和include调用! 个地方:一个是apache和nginx里的conf文件 第一步:修改apache里的httpd.conf文件 查找:AddTy ...

  9. ThinkPHP - 模板引擎

    1.导入css/js文件 - CSS文件 <!--<link rel="stylesheet" type="text/css" href=" ...

  10. java生成压缩图

    链接地址:http://blog.sina.com.cn/s/blog_407a68fc0100nrba.html package util; import java.awt.image.Buffer ...