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].
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
if len(nums) <2:
if nums[0] != target:
return "no this"
return [0]
else:
nums_new = nums
for i in range(len(nums_new)):
if target-nums_new[i] in nums_new[i+1:]:
return [i,nums_new.index(target-nums_new[i],i+1)]
return False result=Solution().twoSum([14,3,5,6,3],6)
print(result)

two sum[easy]的更多相关文章

  1. LeetCode--Array--Two sum (Easy)

    1.Two sum (Easy)# Given an array of integers, return indices of the two numbers such that they add u ...

  2. 【leetcode】Minimum Path Sum(easy)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  3. 【leetcode】Two Sum (easy)

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  4. [leetcode] #112 Path Sum (easy)

    原题链接 题意: 给定一个值,求出从树顶到某个叶(没有子节点)有没有一条路径等于该值. 思路: DFS Runtime: 4 ms, faster than 100.00% of C++ class ...

  5. Leetcode——Two Sum(easy)

    题目:Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1] 代码: ...

  6. Leetcode--1. Two Sum(easy)

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  7. Two Sum [easy] (Python)

    由于题目说了有且只有唯一解,可以考虑两遍扫描求解:第一遍扫描原数组,将所有的数重新存放到一个dict中,该dict以原数组中的值为键,原数组中的下标为值:第二遍扫描原数组,对于每个数nums[i]查看 ...

  8. LeetCode_112. Path Sum

    112. Path Sum Easy Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...

  9. 【目录】Leetcode

    Leetcode 1.动态规划 Palindrome Partitioning II(hard) ☆ Distinct Subsequences(hard) Edit Distance (hard) ...

随机推荐

  1. google vue开发调试插件,简便安装,亲测可用

    前言:开发vue项目,使用谷歌浏览器,不得不使用调试插件便于调试 插件地址如下: 链接:https://pan.baidu.com/s/159HqJMeFSF-w5z-tMi7drw 密码:ueez ...

  2. java设计模式-----15、适配器模式

    概念: Adapter模式也叫适配器模式,是构造型模式之一,通过Adapter模式可以改变已有类(或外部类)的接口形式. 举个例子:我们使用电脑,家里的电源是220V的,而我们的电脑是18V的,这时如 ...

  3. UVAlive6800The Mountain of Gold?(负环)

    题意 题目链接 问从\(0\)出发能否回到\(0\)且边权为负 Sol 先用某B姓算法找到负环,再判一下负环上的点能否到\(0\) #include<bits/stdc++.h> #def ...

  4. CSS响应式:根据分辨率加载不同CSS的几个方法,亲测可用

    有时候你需要把同一个页面在手机和pc同时打开,其中有一个办法就是判断不同分辨路加载不同的css 小编总结了几种分别加载css的方法: 1.比较复杂的使用js判断加载不同css (亲测可用) 但是这种方 ...

  5. JS 时间转化为几分钟前 几小时前 几天前

    背景:最近公司要做动态列表,类似于微信朋友圈.动态创建时间就需要显示为 刚刚.几分钟前.几小时前.几天前.2018-05-15,这样的形式. 代码如下 var minute = 1000 * 60; ...

  6. CentOS7系列--1.3CentOS7用户管理

    CentOS7用户管理 1. 添加用户 [root@centos7 ~]# useradd jack [root@centos7 ~]# passwd jack Changing password f ...

  7. arcgis JavaScript 加载 mapbox地图

    mapbox 地图现在是越来越好看了, 随便试 /** * Created by Administrator on 2018/5/15 0015. */ import * as esriLoader ...

  8. leetCode题解之Reshape the Matrix

    1.题目描述 2.分析 使用了一个队列. 3.代码 vector<vector<int>> matrixReshape(vector<vector<int>& ...

  9. 2.HTML标签

       <a>      链接标签 1) <a href=“#”>这是个链接</a> 表示空链接 2) 未访问过的链接 显示蓝色字体并带下划线;访问过的链接 显示紫色 ...

  10. Docker 报错 error during connect: Get pipe/docker_engine: The system cannot find the file specified. - 摘要: 本文讲的是Docker 报错 error during connect: Get pipe/dock

    error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.37/version: open //./pipe/docker_ ...