leetcode第一题(easy)

第一题:题目内容

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, and you may not use the same element twice.
Example: Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].

给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。
示例: 给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1]

今天做了leetcode的第一道题,题意很简单,就是给定一组数和一个目标值,从这组数中找到两个数加起来等于目标值,输出目标值的下标。

刚刚见到这道题,没过脑,直接使用暴力遍历,两个循环嵌套,逐个进行相加运算,复杂度是O(n^2)

def twoSum( nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        l=list()
        for i in range(len(nums)):
            print(‘i=‘,i)
            for j in range(i+1,len(nums)):
                print(‘j=‘,j)
                if nums[i]+nums[j]==target:
                    l.append(i)
                    l.append(j)
        return l

 查找了别人写的之后感觉还是大神厉害的哈哈,自己提交之后AC了

def twoSum( nums, target):
    sum=[]
    for i in range(len(nums)):
        one = nums[i]
        second = target-nums[i]
        if second in nums:
            j = nums.index(second)
            if i!=j:
                sum.append(i)
                sum.append(j)
    return sum       

原文地址:https://www.cnblogs.com/bigdata-stone/p/10217178.html

时间: 2024-07-28 14:52:36

leetcode第一题(easy)的相关文章

LeetCode 第一题,Two Sum

今天早上起来去机房的路上还在想,一直不做算法题老是觉得不踏实.做做题总是让自己觉得自己真的在做做学习.... 这也算是一种强迫症吧. 那就从今天开始做做LeetCode,因为好久没做过了,所以第一题还是看了别人的题解和思路,算是找找感觉. 总的来说第一题是个水.... 题目还原 Two Sum Given an array of integers, find two numbers such that they add up to a specific target number. The fu

LeetCode第一题:Evaluate Reverse Polish Notation

时间:2014.06.11 地点:基地 -------------------------------------------------------------------- 一.题目 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another express

LeetCode 第一题剪彩自己的leetCode之路

Reverse Words in a String Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". click to show clarification. Clarification: What constitutes a word? A sequence of no

LeetCode第一题以及时间复杂度的计算

问题描述:给定一组指定整数数组,找出数组中加和等于特定数的两个数. 函数(方法)twoSum返回这两个数的索引,index1必须小于index2. 另外:你可以假设一个数组只有一组解. 一个栗子: Input: numbers={2, 7, 11, 15}, target=9 Output: index1=1, index2=2 算法实现如下: 1 /** 2 * 时间复杂度O(n) 3 * @param array 4 * @param target 5 * @return Map<Integ

leetcode第一题

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, and you may not use the same element twice. Example: Given nums = [2, 7, 11, 15]

LeetCode 第一题 两数之和

题目描述 给定一个整数数组 nums?和一个目标值 target,请你在该数组中找出和为目标值的那?两个?整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 方法一:暴力法 暴力法很简单,遍历每个元素 xx,并查找是否存在一个值与 target - xtarget?x

LeetCode刷题:第一题 两数之和

从今天开始刷LeetCode 第一题:两数之和 题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 代码如下: 1 /** 2 * Note: The retur

leetcode中第一题twosum问题解答算法的可行性证明

leetcode中第一题twosum问题解答算法的可行性证明 一.引入 关于leetcode中第一题twosum问题,网上已有不少高人做出过解答,并提出了切实可行的算法实现.我在解答该题时参考了博客http://www.zixue7.com/article-9576-1.html的解答.为让读者更直观地阅读和理解本文,先简要摘录以上博客的内容如下: 题目还原 Two Sum Given an array of integers, find two numbers such that they a

新人第一帖 LeetCode做题开始

朋友介绍了leetcode 这个oj网站,包含了很多程序员的试题,我去看了下,对提高算法能力很有针对性,我决定尝试开始解题历程,并和大家分享我的解题,这是第一题: 1: 给定一个int数组,和 一个 int target 值,数组中有两个值的和为target ,给出这两个值的下标,你可以认为解只有一个. 水题,以下是我的代码: public class Solution { public int[] twoSum(int[] nums, int target) { int i=0,j = 0 ;