题目:

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. Android学习笔记(十五)——碎片的生命周期(附源代码)

    碎片的生命周期 点击下载源代码 与活动类似.碎片具有自己的生命周期.理解了碎片的生命周期后.我们能够在碎片被销毁时正确地保存事实上例,在碎片被重建时将其还原到前一个状态. 1.使用上一篇的项目Frag ...

  2. Sql server 事务 存储过程

    事务( Transaction )是并发控制的单位,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位. 通过事务,SQL Server能将逻辑相关的一组操作绑定在一起, ...

  3. Node.js入门-Node.js 介绍

    Node.js 是什么 Node.js 不是一种独立的语言,与 PHP,Python 等"既是语言优势平台"不同,它也不是一个 JavaScrip 框架,不同于 CakePHP,D ...

  4. 页面布局之BFC 微微有点坑

    一.什么是BFC: 在解释 BFC 是什么之前,需要先介绍 Box.Formatting Context的概念. Box: CSS布局的基本单位 Box 是 CSS 布局的对象和基本单位, 直观点来说 ...

  5. HDU 1222 Wolf and Rabbit(gcd)

    HDU 1222   Wolf and Rabbit   (最大公约数)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  6. HTML5 总结-拖放-3

    HTML5 拖放 拖放(Drag 和 drop)是 HTML5 标准的组成部分. 拖放 拖放是一种常见的特性,即抓取对象以后拖到另一个位置. 在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放 ...

  7. django中间件templates写法

    def templates_context_process(request): from django.contrib.sites.models import Site from accounts.m ...

  8. C++ ABI之名字改变,编译器生成符号研究(以Qt为例)

    在C++中,由于重载等技术的存在,编译器要将函数.结构体.类等等的信息传递给链接器,就不能像C语言那样简单地通过函数名来完成,它需要提供额外的参数信息,而还要和C语言共用链接器,这就需要用到名字改编( ...

  9. 基于visual Studio2013解决C语言竞赛题之0402奇偶求和

      题目 解决代码及点评 这道题考察我们对循环和判断的综合应用 #include <stdio.h> #include <stdlib.h> #include < ...

  10. BZOJ 1087

    var f:..,..,..] of int64; a:Array[..] of longint; count:..] of longint; ans:int64; n,m:longint; proc ...