Easy-题目68:1. Two Sum(增补1)

原创 2016年05月30日 22:48:03

题目原文:
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.

Example:
Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
题目大意:
给出一个数组和一个目标值target,寻找两个下标i和j,使得nums[i]+nums[j]=target。
题目分析:
这道题先无脑水过去。复杂度O(n2)
源码:(language:java)

public class Solution {
    public int[] twoSum(int[] nums, int target) {
        for(int i = 0;i<nums.length;i++) {
            for(int j=i+1;j<nums.length;j++) {
                if(nums[i]+nums[j]==target)
                    return new int[]{i,j};
            }
        }
        return new int[]{-1,-1};
    }
}

成绩:
42ms,beats 19.97%,众数6ms,35.54%
Cmershen的碎碎念:
这道题不知道何时被改成Easy了,其实可以用HashMap做,应该能在线性时间解决。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

Leetcode #1 - Two Sum - Easy

Problem Given an array of integers, return indices of the two numbers such that they add up to a sp...
  • Arcome
  • Arcome
  • 2016年09月13日 08:55
  • 103

LeetCode解题报告 1. Two Sum[easy]

前言 之前听说过很多次LeetCode在面试中的大量应用,通过本次算法课可以开始题目的练习觉得还是很不错的。 题目描述1. Two Sum[easy] Given an array of intege...

题目1 Two Sum

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

类型系列题目:1. Two Sum 15,3 sum ,16 3sum closet, 18 4sum, 454 4Sum II

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

#1 Two Sum

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

【Leetcode】1. Two Sum

1. Two Sum Total Accepted: 254411Total Submissions: 1031847Difficulty: Easy Given an array...

leetcode No.1 Two Sum

题意: 给定一个数组, 给定一个目标数, 找出数组中和等于目标数的两个数的位置。 题目相对简单。但是第一次接触leetcode觉得挺新鲜,选择C++语言答题后编辑框中是这样的: class Sol...

Leetcode-1 Two Sum(Java) -by zzy

Problems: Given an array of integers, find two numbers such that they add up to a specific target n...

leetcode:1. Two Sum(Java实现)

leetcode测试地址:https://leetcode.com/problems/two-sum/#/description 1. Two Sum Given an array o...

【Leet Code- Algorithm】【1-Two Sum】【Python】

编程小白,想通过博客来记录自己每天编程的心得,每天进步一点点,请大家多多指数!如果有新的想法,我会及时更新。如果内容有误,欢迎大家指出。另外,内容中可能包含我的疑问,多谢大家帮忙解答。 题目: G...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Easy-题目68:1. Two Sum(增补1)
举报原因:
原因补充:

(最多只允许输入30个字)