【leetcode❤python】 1. Two Sum
#-*- coding: UTF-8 -*-
#AC源码【意外惊喜,还以为会超时】
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
for i in xrange(len(nums)):
tmp=target-nums[i]
curList=nums[i+1:]
if curList.__contains__(tmp):
return [i,i+curList.index(tmp)+1]
else:continue
return []
sol=Solution()
print sol.twoSum([-1,-2,-3,-4,-5],-8)
【leetcode❤python】 1. Two Sum的更多相关文章
- 【leetcode❤python】 303. Range Sum Query - Immutable
#-*- coding: UTF-8 -*- #Tags:dynamic programming,sumRange(i,j)=sum(j)-sum(i-1)class NumArray(object) ...
- 【leetcode❤python】 112. Path Sum
#-*- coding: UTF-8 -*-# Definition for a binary tree node.# class TreeNode(object):# def __init_ ...
- 【leetcode❤python】Sum Of Two Number
#-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...
- 【leetcode❤python】 Sum of Left Leaves
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init ...
- 【leetcode❤python】 58. Length of Last Word
#-*- coding: UTF-8 -*-#利用strip函数去掉字符串去除空格(其实是去除两边[左边和右边]空格)#利用split分离字符串成列表class Solution(object): ...
- 【leetcode❤python】 8. String to Integer (atoi)
#-*- coding: UTF-8 -*-#需要考虑多种情况#以下几种是可以返回的数值#1.以0开头的字符串,如01201215#2.以正负号开头的字符串,如'+121215':'-1215489' ...
- 【leetcode❤python】 396. Rotate Function
#-*- coding: UTF-8 -*- #超时# lenA=len(A)# maxSum=[]# count=0# while count ...
- 【leetcode❤python】 299. Bulls and Cows
#-*- coding: UTF-8 -*-class Solution(object): def getHint(self, secret, guess): " ...
- 【leetcode❤python】299. Bulls and Cows
class Solution(object): def getHint(self, secret, guess): """ :type ...
随机推荐
- java 关于hashcode和equals的测试
package thinkingInJava; import java.util.HashMap; /* * 测试在向以hashcode为基础的集合(HashSet , HashMap , HashT ...
- Android课程---布局管理器
- 在VPS上部署fq环境
VPS购买地址 1. 由于我选择的是CentOS 6 x86版本, 需要安装如下准备工具: git, gcc-c++, zlib-devel, openssl-devel, pcre-devel 2. ...
- Java String类型数据的字节长度
问题描述: 向Oracle数据库中一varchar2(64)类型字段中插入一条String类型数据,程序使用String.length()来进行数据的长度校验,如果数据是纯英文,没有问题,但是如果数据 ...
- linux下缓存的查看/修改
起因: 安装openstack过程中内存不够大,提高内存后想起缓存一般设置为内存的两倍. 缓存的实质是硬盘开辟一个空间,然后设置这个空间为缓存. 查看缓存大小 free -m free -m tota ...
- C#编程利器之二:结构与枚举(Structure and enumeration)【转】
C#编程利器之二:结构与枚举(Structure and enumeration) 在上一篇文章中,介绍了类如何封装程序中的对象.而实际中,出了类可以封装对象外,结构和枚举也可以封装一些对象,本文将着 ...
- Spring MVC Integration,Spring Security
http://docs.spring.io/spring-security/site/docs/4.2.0.RELEASE/reference/htmlsingle/#authorize-reque ...
- wkwebview a target="_blank" 打不开链接的解决方案
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigatio ...
- transform原点
Safari 4 Firefox3.5 Opera10.5 Chrome Internet Explorer 目前这两个属性得到了除去ie以外各个主流浏览器webkit,firefox,opera的支 ...
- js方式找出数组中重复数最多的那个数,并返回该数以及重复次数
function findNum(a){ var result = [0,0]; for (var i = 0; i < a.length; i++) { for (var j = 0,coun ...