LeetCode题目(python)
1.给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
示例:
给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
计算:
方法一:(5848 ms)
class Solution:
def twoSum(self, nums, target):
a=[]
length=len(nums)
for i in range(0,length-1):
for j in range(i+1,length):
if(nums[i]+nums[j]==target):
return [i,j]
方法二:(48 ms)
class Solution:
def twoSum(self, nums, target):
a=[]
length=len(nums)
for i in range(0,length-1):
for j in range(i+1,length):
if(nums[i]+nums[j]==target):
return [i,j]
方法三:(1152 ms)
class Solution:
def twoSum(self, nums, target):
length=len(nums)
dict={}
for i in range(length):
var=target-nums[i]
if var in nums and i!=nums.index(var):
return [i,nums.index(var)]
两数之和
2.计算最长回文字串
def longestPalindrome( s):
if len(s) < 2 or s == s[::-1]:
return s
n = len(s)
# 定义起始索引和最大回文串长度,odd奇,even偶
start, maxlen = 0, 1
# 因为i=0的话必然是不可能会有超过maxlen情况出现,所以直接从1开始
for i in range(1, n):
# 取i及i前面的maxlen+2个字符
odd = s[i - maxlen - 1:i + 1] # len(odd)=maxlen+2
# 取i及i前面的maxlen+1个字符
even = s[i - maxlen:i + 1] # len(even)=maxlen+1 print("i="+str(i)+";\t"+"maxlen="+str(maxlen)+";\todd="+odd+";"+"\t"+"even="+even)
if i - maxlen - 1 >= 0 and odd == odd[::-1]:
start = i - maxlen - 1
maxlen += 2
continue
if i - maxlen >= 0 and even == even[::-1]:
start = i - maxlen
maxlen += 1
#print(s[start:start + maxlen])
return s[start:start + maxlen] s="abaqw"
# b=s[-1:2]
# print(b)
a=longestPalindrome(s)
print(a)
最长回文字串
LeetCode题目(python)的更多相关文章
- LeetCode专题-Python实现之第9题:Palindrome Number
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- leetcode题目清单
2016-09-24,开始刷leetcode上的算法题,下面整理一下leetcode题目清单.Github-leetcode 1.基本数学 Two Sum Palindrome Number Cont ...
- LeetCode 题目总结/分类
LeetCode 题目总结/分类 利用堆栈: http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ http://oj.l ...
- LeetCode专题-Python实现之第28题: Implement strStr()
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第27题:Remove Element
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第21题:Merge Two Sorted Lists
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第20题:Valid Parentheses
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第14题:Longest Common Prefix
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第13题:Roman to Integer
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
随机推荐
- summernote(富文本编辑器)将附件与图片上传到自己的服务器(vue项目)
1.上传图片至自己的服务器(这个官方都有例子,重点介绍附件上传)图片上传官方网址 // onChange callback $('#summernote').summernote({ callback ...
- 20175215 2018-2019-2 第七周java课程学习总结
第八章 常用实用类 8.1 String类 Java专门提供了用来处理字符序列的String类.String类在java.lang包中,由于java.lang包中的类被默认引入,因此程序可以直接使用S ...
- JVM系列2:HotSpot虚拟机对象
1.对象创建过程: ①.类加载检查:当java虚拟机遇到一条new指令时,首先会去检查该指令的参数能否在常量池中定位到这个类的符号引用,并且检查这个符号引用代表的类是否已被加载.解析.初始化过,如果没 ...
- httpparase + httpclient 的运用
这篇文章介绍了 HtmlParser 开源包和 HttpClient 开源包的使用,在此基础上实现了一个简易的网络爬虫 (Crawler),来说明如何使用 HtmlParser 根据需要处理 Inte ...
- 写python获取android设备的GPS及姿态信息
在android上,我们可以使用QPython来编写.执行Python脚本.它对很多android 系统函数进行了方便的封装,使用QPython编写功能简单的小程序异常方便. 这个示例是我之前用来读取 ...
- failed to create cublas handle: CUBLAS_STATUS_ALLOC_FAILED 错误解决方法
解决: config = tf.ConfigProto() config.gpu_options.allow_growth = True session = tf.Session(config=con ...
- [Python]if语句的练习
习题: 小明身高1.75,体重80.5kg.请根据BMI公式(体重除以身高的平方)帮小明计算他的BMI指数,并根据BMI指数:低于18.5:过轻 18.5-25:正常 25-28:过重 28-32:肥 ...
- Custom Configuration 的两种方法:2.XmlSerializer XmlAttribute
第二种:XmlSerializer XmlAttribute 1.CustomConfiguration.xml 2.CustomConfigurationSetting.cs 3.CustomCon ...
- python 2.7 error: Microsoft Visual C++ 9.0 is required
参考:https://stackoverflow.com/questions/43645519/microsoft-visual-c-9-0-is-required 解决方法: 下载并安装Micros ...
- 阶段3 2.Spring_03.Spring的 IOC 和 DI_5 BeanFactory和ApplicationContext的区别
核心容器的两个接口.ApplicationContext和BeanFactory 怎么知道对象被创建了呢 我们只需要在实现类里面构造函数内打印输出一段话 然后再这里加上一个断点 运行程序,光标停在这个 ...