LeetCode【第一题】Two Sum
准备刷一刷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. Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
'''
中文题目:
'''
给定一个整数数组,返回两个元素的索引,使得这两个元素相加的值等于一个给定的target。 您可以假设每个输入都恰有一个解决方案。 例:
给定nums = [2,7,11,15],target = 9, 因为nums [0] + nums [1] = 2 + 7 = 9,
return [0,1]。
'''
解法一和笔记:
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
arr = {} # 定义一个空字典
length = len(nums)
for i in range(length):
if (target - nums[i]) in arr: # 如果target减去当前的元素的值存在于空字典中时,符合题意。
return [arr[target - nums[i]], i] # 返回target-nums[i]和i的索引
arr[nums[i]] = i # 否则将该元素加入字典arr # 该方法accepted了,不知道是否有更优的解法
a = Solution().twoSum([3, 2, 4], 6)
print(a)
LeetCode【第一题】Two Sum的更多相关文章
- leetcode第一题--two sum
Problem:Given an array of integers, find two numbers such that they add up to a specific target numb ...
- LeetCode第一题—— Two Sum(寻找两数,要求和为target)
题目描述: Given an array of integers, return indices of the two numbers such that they add up to a speci ...
- LeetCode算法题-Two Sum II - Input array is sorted
这是悦乐书的第179次更新,第181篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第38题(顺位题号是167).给定已按升序排序的整数数组,找到两个数字,使它们相加到特定 ...
- 乘风破浪:LeetCode真题_040_Combination Sum II
乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...
- 乘风破浪:LeetCode真题_039_Combination Sum
乘风破浪:LeetCode真题_039_Combination Sum 一.前言 这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...
- LeetCode算法题-Two Sum IV - Input is a BST(Java实现)
这是悦乐书的第280次更新,第296篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第148题(顺位题号是653).给定二进制搜索树和目标数,如果BST中存在两个元素,使得 ...
- LeetCode算法题-Path Sum III(Java实现)
这是悦乐书的第227次更新 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第94题(顺位题号是437).您将获得一个二叉树,其中每个节点都包含一个整数值.找到与给定值相加的路径数 ...
- LeetCode算法题-Range Sum Query Immutable(Java实现)
这是悦乐书的第204次更新,第214篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第70题(顺位题号是303).给定整数数组nums,找到索引i和j(i≤j)之间的元素之 ...
- LeetCode算法题-Path Sum(Java实现)
这是悦乐书的第169次更新,第171篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第28题(顺位题号是112).给定二叉树和整数sum,确定树是否具有根到叶路径,使得沿路 ...
- leetcode第一题(easy)
第一题:题目内容 Given an array of integers, return indices of the two numbers such that they add up to a sp ...
随机推荐
- 转换流--OutputStreamWriter类与InputStreamReader类
12.4 转换流--OutputStreamWriter类与InputStreamReader类 整个IO包实际上分为字节流和字符流,可是除了这两个流之外,还存在一组字节流-字符流的转换类. Out ...
- 在JAVA中线程到底起到什么作用
这是javaeye上非常经典的关于线程的帖子,写的非常通俗易懂的,适合任何读计算机的同学. 线程同步 我们可以在计算机上运行各种计算机软件程序.每一个运行的程序可能包括多个独立运行的线程(Thread ...
- mysql_config_editor
加入账号: [root@server-mysql bin]# ./mysql_config_editor set --login-path=client --user=root --password ...
- jsp----在jsp中写java代码(变量和函数方法)
<%@page import="java.text.SimpleDateFormat"%><%@page language="java" im ...
- 玩转Android之在线视频播放控件Vitamio的使用
其实Android中自带的MediaPlayer本身就能播放在线视频,MediaPlayer结合SurfaceView播放在线视频也是不错的选择(如果你没有性能或者用户体验上的要求),关于MediaP ...
- iOS 网络编程:XML解析
1 XML文档结构 1.1 简介 XML 指可扩展标记语言(eXtensible Markup Language).XML 被设计用来传输和存储数据.其非常像HTML的标记语言,但与之不同的是,XML ...
- iOS 跳转至 指定 StoryBoard 中 指定的 Scene
UIStoryboard *stryBoard=[UIStoryboard storyboardWithName:@"second" bundle:nil]; UIViewCont ...
- Spring整合JMS(一)——基于ActiveMQ实现
1.1 JMS简介 JMS的全称是Java Message Service,即Java消息服务.它主要用于在生产者和消费者之间进行消息传递,生产者负责产生消息,而消费者负责接收消息.把它应用到 ...
- 20160322 javaweb 学习笔记--response验证码实现
package com.dzq.servlet; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; im ...
- 20151222jquery学习笔记--验证注册表单
$(function () { $('#search_button').button({ icons : { primary : 'ui-icon-search', }, }); $('#reg'). ...