第一题:题目内容

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. Bootstap

    Bootstrap框架 Bootstrap框架 Bootstrap介绍 Bootstrap是Twitter开源的基于HTML.CSS.JavaScript的前端框架. 它是为实现快速开发Web应用程序 ...

  2. C++学习一二

    为了更深入的学习程序编写,以及进行相关算法的编写.决定每天花点时间学习C++:以下是每天的学习笔记. 一.std代表命名空间,可以用using来省略. 二.std:endl.输出一个换行符,并且“刷新 ...

  3. 关于如何解决TeamViewer限制时间问题

    最近在弄一个项目,我们是乙方,甲方离我们比较远,所以需要用到远程操作软件.也就是TeamViewer. 这个软件一开始运行还行,后来时间用久了,很容易被限制时间.在网上查了大部分资料,都是一些修改MA ...

  4. eclipse 简单配置

    1.安装STS4 help-->Eclipse Maketplace-->search 'STS'-->install 2.代码提示 Window-->preferences- ...

  5. Vue 中 双向绑定数据

    1.文本 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...

  6. OOM和SOF代码

    OutOfMemoryError大数组,例如图片加载. public class MockOutOfMemoryError { public static void main(String[] arg ...

  7. 一款基于jQuery Ajax的等待效果

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  8. gitblit 数据迁移(复制)

    gitblit 数据迁移 完全拷贝方式: 将原服务器上的gitblit的安装目录.数据目录等相关目录拷到另一台服务器上即可,这样启动方式和使用端口及数据和原服务上的一模一样.(因为gitblit是不用 ...

  9. 在HTML标签元素中,绑定JS函数

    <a onclick="ShowMsg(this)" id="myA" href="#">按钮</a> //JS方法 ...

  10. 微信小程序 API 路由

    路由:由于页面的跳转: wx.switchTab() 跳转到 tabBar 页面,并关闭掉其他所有非 tabBar 页面: 参数:为对象, 对象的属性: url:需要跳转的 tabBar 的页面路径( ...