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+,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)的更多相关文章
- 有人相爱,有人夜里开车看海,有人leetcode第一题都做不出来。
第一题 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数 ...
- LeetCode第一题:Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- leetcode第一题--two sum
Problem:Given an array of integers, find two numbers such that they add up to a specific target numb ...
- leetcode第一题两数之和击败了 98.11% 的用户的答案(C++)
虽然题目简单,但我这好不容易优化到前2%,感觉也值得分享给大家(方法比较偷机) 题目: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们 ...
- leetcode 第一题 Two Num java
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- LeetCode第一题以及时间复杂度的计算
问题描述:给定一组指定整数数组,找出数组中加和等于特定数的两个数. 函数(方法)twoSum返回这两个数的索引,index1必须小于index2. 另外:你可以假设一个数组只有一组解. 一个栗子: I ...
- LeetCode第一题—— Two Sum(寻找两数,要求和为target)
题目描述: Given an array of integers, return indices of the two numbers such that they add up to a speci ...
- LeetCode 第一题 两数之和
题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组 ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
随机推荐
- k8s安装报错 Error: unknown flag: --experimental-upload-certs
今天安装k8sV1.16的版本时候,执行突然发现命令不对,之前安装V1.15的时候是可以的,可能是版本升级的原因. 解决: unknown flag: --experimental-upload-ce ...
- 计蒜客 A1607 UVALive 8512 [ACM-ICPC 2017 Asia Xi'an]XOR
ICPC官网题面假的,要下载PDF,点了提交还找不到结果在哪看(我没找到),用VJ交还直接return 0;也能AC 计蒜客题面 这个好 Time limit 3000 ms OS Linux 题目来 ...
- [CSP-S模拟测试]:Walker(数学)
题目传送门(内部题86) 输入格式 第一行$n$接下来$n$行,每行四个浮点数,分别表示变换前的坐标和变换后的坐标 输出格式 第一行浮点数$\theta$以弧度制表示第二行浮点数$scale$第三行两 ...
- 《第40天 : JQuery - 手风琴列表》
源码下载地址:链接:https://pan.baidu.com/s/1x9c1... 提取码:2bzr 如果有赞就很幸福了. 今天要和你们分享的是我看了JQuery库的手风琴列表样式.它的核心在于它的 ...
- 20175221 曾祥杰 数据库MySQL(课下作业,必做)
数据库MySQL(课下作业,必做) 题目要求: 1. 下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB ...
- CentOS7服务器配置
CentOS7服务器配置 1.更换yum软件源 下载阿里源 cd /etc/yum.repos.d sudo wget -nc http://mirrors.aliyun.com/repo/Cento ...
- jQuery.parseJSON()
https://api.jquery.com/jQuery.parseJSON/ https://api.jquery.com/category/deprecated/deprecated-3.0/ ...
- LoadRunner之检查点
一.什么是检查点 LoadRunner中检查点是用来判断脚本是否执行成功的.如果不加检查点,只要服务器返回的HTTP状态码是200,VuGen就认为脚本执行通过了.但是很多情况下服务器返回200并不代 ...
- JDK7新特性
二进制字面量 数字字面量可以出现下划线 switch语句可以用字符串 泛型简化 异常的多个catch合并 try..with...resource语句 import java.io.FileReade ...
- 6.824 Lab 3: Fault-tolerant Key/Value Service 3B
Part B: Key/value service with log compaction Do a git pull to get the latest lab software. As thing ...