LeetCode01--两数之和
'''
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的 两个 整数。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
示例: 给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
''' class Solution: def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
for i in range(0, len(nums)):
try:
j = nums.index(target - nums[i])
except:
return '', '没有符合条件的元素'
else:
return i, j if __name__ == '__main__':
nums = [2, 7, 11, 15, 20, 12, 30, 28]
target = 17
s = Solution()
i, j = s.twoSum(nums, target)
print(i, j)
LeetCode01--两数之和的更多相关文章
- LeetCode01 - 两数之和(Java 实现)
LeetCode01 - 两数之和(Java 实现) 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/two-sum 题目描述 给定一个整数数组 ...
- 给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X
题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的 ...
- LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- LeetCode 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 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] 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] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- Leetcode(一)两数之和
1.两数之和 题目要求: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重 ...
- 南大算法设计与分析课程OJ答案代码(1)中位数附近2k+1个数、任意两数之和是否等于给定数
问题1 用来测试的,就不说了 问题2:中位数附近2k+1个数 给出一串整型数 a1,a2,...,an 以及一个较小的常数 k,找出这串数的中位数 m 和最接近 m 的小于等于 m 的 k 个数,以及 ...
- 两数之和,两数相加(leetcode)
我们都知道算法是程序员成长重要的一环,怎么才能提高算法呢, 出来在网上看视频之外,动手练习是非常重要的.leetcode 就是一个非常好的锻炼平台. 1. 两数之和,在 leetcode 里面是属于 ...
随机推荐
- KMP算法笔记(云笔记图片版)
- 通过表单展示不一样的页面(input对象)
表单中包含不一样的样式,不同功能的提交数据的方式.在许多页面中,浏览者不经意间已经不断在使用表单的功能,如留言,设置自己的密码或者是复选框,下拉列表等. input对象下的多种表单表现形式: 通常在页 ...
- php 使用serialize() 和 unserialize() 让对象成超级变量
手册里面的原话和一些总结: php函数serialize()与unserialize()说明及案例.想要将已序列化的字符串变回 PHP 的值,可使用unserialize().serialize()可 ...
- Android开发-浅谈架构(一)
写在前面的话 嗯 聊聊架构. 这段时间一直在维护旧项目. 包括自己之前写的新项目 越来越发现 一个架构清晰的项目往往让人赏心悦目.不至于在一个bug丢过来之后手足无措.包括以后别人接收自己的项目 能很 ...
- C#程序A调用程序B的问题
C#程序A调用程序B,如果程序B中存在 string path1 = System.Environment.CurrentDirectory; 程序A中开启B进程的代码为: System.Diagno ...
- 【转】java编程思想第20章的注解例子用到的com.sun.mirror的jar包
Java編程思想>中的注解代码中引入过这么一个包(com.sun.mirror),书上说的是在Jdk中有个tools.jar中,引入这个包就每这个问题了,但是笔者用的是JDK 1.8,把这个包i ...
- LN : leetcode 516 Longest Palindromic Subsequence
lc 516 Longest Palindromic Subsequence 516 Longest Palindromic Subsequence Given a string s, find th ...
- CF962E Byteland, Berland and Disputed Cities
思路: http://codeforces.com/blog/entry/58869. 实现: #include <bits/stdc++.h> using namespace std; ...
- 【Jenkins】Jenkins配置从节点,实现远程主机调用功能
一.需求 使用Jenkins进行持续集成部署过程中,需要用到远端主机的处理功能.如部署到远程主机.文件备份等功能 二.思路 1.当远端主机为Linux系统时使用Publish Over SSH Plu ...
- 移除sql数据所有链接用户
use master; go declare @temp nvarchar(20) declare myCurse cursor for select spid from sy ...