第一题:题目内容

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+,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

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

  1. 有人相爱,有人夜里开车看海,有人leetcode第一题都做不出来。

    第一题 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数 ...

  2. LeetCode第一题:Two Sum

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

  3. leetcode第一题--two sum

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

  4. leetcode第一题两数之和击败了 98.11% 的用户的答案(C++)

    虽然题目简单,但我这好不容易优化到前2%,感觉也值得分享给大家(方法比较偷机) 题目: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们 ...

  5. leetcode 第一题 Two Num java

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

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

    问题描述:给定一组指定整数数组,找出数组中加和等于特定数的两个数. 函数(方法)twoSum返回这两个数的索引,index1必须小于index2. 另外:你可以假设一个数组只有一组解. 一个栗子: I ...

  7. LeetCode第一题—— Two Sum(寻找两数,要求和为target)

    题目描述: Given an array of integers, return indices of the two numbers such that they add up to a speci ...

  8. LeetCode 第一题 两数之和

    题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组 ...

  9. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

随机推荐

  1. Pku2978 Colored stones

    题目链接:Click here Solution: 状压dp,考虑\(f[i][j][k]\)表示当前到了第i个石头,颜色状态为j,选取的最后一个石头颜色为k时能够留下的石头的最大数量 转移也很好转移 ...

  2. sh_04_第1个函数改造

    sh_04_第1个函数改造 name = "小明" # say_hello() # Python 解释器知道下方定义了一个函数 def say_hello(): "&qu ...

  3. android字符串工具类

    package com.ctbri.weather.utils.calendar; /** * Created by IntelliJ IDEA. * User: zhouxin@easier.cn ...

  4. ArrayList类源码浅析(一)

    1.首先来看一下ArrayList类中的字段 可以看出,ArrayList维护了一个Object数组,默认容量是10,size记录数组的长度: 2.ArrayList提供了三个构造器:ArrayLis ...

  5. MongoDB通过JavaDriver执行shell命令,例如创建sharding collection

    Mongodb的java driver本身的接口 void createCollection(String collectionName, CreateCollectionOptions create ...

  6. jQuery file upload上传图片的流程

    先触发_onChange[jquery.fileupload.js] _onChange: function (e) { var that = this, data = { fileInput: $( ...

  7. Android NDK下载

    http://dl.google.com/android/ndk/android-ndk-r10d-linux-x86_64.bin https://dl.google.com/android/rep ...

  8. sed扩展命令使用

    [root@b ~]# cat f.txt inet addr:192.168.0.110 Bcast:192.168.0.255 Mask:255.255.255.0[root@b ~]# cat ...

  9. 十一、RF操作滚动条

    两种方式: 方式一:window.scrollBy(0, document.body.scrollHeight) 方式二:window.scrollTo(0, document.body.scroll ...

  10. 转 HTTP请求报文格式 GET和POST

    https://blog.csdn.net/h517604180/article/details/79802914 最近在做安卓客户端图片上传插件功能,供后台调用.其中涉及到了拼接HTTP请求报文,所 ...