[LeetCode&Python] Problem 1: Two Sum
Problem Description:
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].
Approach 1: Brute Force
At the beginning, I want to use the Brute Froce method to use this problem. Then I write this code:
class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
n=len(nums) for j in range(n):
for i in range(j+1,n):
if nums[j]+nums[i]==target:
return j,i
However, this method wastes too much time.
Solution:
A better method to solve this problem is to use a dictionary to store all the pairs' indices.
class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
n=len(nums) d={} for x in range(n):
a = target-nums[x]
if nums[x] in d:
return d[nums[x]],x
else:
d[a]=x
[LeetCode&Python] Problem 1: Two Sum的更多相关文章
- [LeetCode&Python] Problem 167. Two Sum II - Input array is sorted
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- [LeetCode&Python] Problem 653. Two Sum IV - Input is a BST
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
- [LeetCode&Python] Problem 371. Sum of Two Integers
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- [LeetCode&Python] Problem 599. Minimum Index Sum of Two Lists
Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...
- [LeetCode&Python] Problem 404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- [LeetCode&Python] Problem 682. Baseball Game
You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 ...
- [LeetCode&Python] Problem 883. Projection Area of 3D Shapes
On a N * N grid, we place some 1 * 1 * 1 cubes that are axis-aligned with the x, y, and z axes. Each ...
- [LeetCode&Python] Problem 807. Max Increase to Keep City Skyline
In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located the ...
- 【leetcode❤python】 303. Range Sum Query - Immutable
#-*- coding: UTF-8 -*- #Tags:dynamic programming,sumRange(i,j)=sum(j)-sum(i-1)class NumArray(object) ...
随机推荐
- Java逻辑
1.开发简单Java应用程序 1-1:什么是程序 程序:为了让计算机执行某些操作或解决某个问题而编写的一系列有序指令的集合.1-2:为什么学习Java Java是现在的主流1-3:Java可以做什么 ...
- 部署Linux项目
部署Linux项目 1● 下载软件 ftp 安装 2● 创建连接 3● java项目 gunzip –c *.gz tar –xzf *.gz rm –rf rm -r ...
- post和get的使用场景和区别
使用场景: 区别: ①传送方式不同:get通过地址栏传输,post通过报文传输. ②get产生一个TCP数据包,post产生两个数据包,对于get方式的请求,浏览器会把http header和data ...
- ubuntu默认启动方式修改 psensor命令
Check UUID sudo blkid Then sudo gedit /etc/default/grub & to pull up the boot loader configurati ...
- OO作业总结报告3
规格化设计的发展史 下面部分来源:https://www.cnblogs.com/eggert/p/9098446.html: 随着计算机硬件的飞速发展,以及应用复杂度越来越高,软件规模越来越大,原有 ...
- Unix分类学习
调试 shell # bash -x script.sh 设置终端背景色 setterm -background black 一.网络 1.网卡状态 mii-tool -v ethtool eth0 ...
- block,inline-block,行内元素区别及浮动
1.block: 默认占据一行空间,盒子外的元素被迫另起一行 2.inline-block: 行内块盒子,可以设置宽高 3.行内元素: 宽度即使内容宽度,不能设置宽高,如果要设置宽高,需要转换成行内块 ...
- MATLAB 地图工具箱 m_map 的安装和入门技巧(转)
reference: http://blog.sina.com.cn/s/blog_8fc890a20102v6pm.html 需要用一些地图工具,arcgis懒得装了,GMT(generic m ...
- struts请求参数注入的三种方式
.请求参数的注入 在Struts2框架中,表单的提交的数据会自动注入到与Action对象相对应的属性.它与Spring框架中的IoC的注入原理相同,通过Action对象为属性提供setter方法注入 ...
- uiautomator2 获取APP Toast内容
前言:appium必须是1.6以上的版本 环境(安装和安装都可以运行成功,我也不确定要不要这个): 1.抓到toast需要安装uiautomator2,安装npm:npm install -g cnp ...