博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leet Code OJ 简单(一)
阅读量:7294 次
发布时间:2019-06-30

本文共 2194 字,大约阅读时间需要 7 分钟。

1.两数之和  3780ms

class Solution(object):    def twoSum(self, nums, target):        """        :type nums: List[int]        :type target: int        :rtype: List[int]        """        for i in range(len(nums)):            for m in range(i+1, len(nums)):                if nums[i]+nums[m] == target:                    return [i, m]

7.反转整数 80ms (python的负数取余和C语言 不同)

class Solution:    def reverse(self, x):        """        :type x: int        :rtype: int        """        y = 0        while x:            if x > 0:                y *= 10                y += x % 10                x = x // 10            else:                y *= 10                if x % 10:                    y += x % 10 - 10                    x = x // 10 + 1                else:                    y += x % 10                    x = x // 10        if y > -pow(2, 31) and y < pow(2, 31) - 1:            return y        else:            return 0

9.回文数  408ms

class Solution:    def isPalindrome(self, x):        """        :type x: int        :rtype: bool        """        x = str(x)        for i in range(len(x)):            if x[i] != x[-i-1]:                return False        return True

13.罗马数字转整数 212ms

class Solution:    def romanToInt(self, s):        """        :type s: str        :rtype: int        """        rec = 0        num = {'I': 1, 'IV': 4, 'V': 5, 'IX': 9, 'X': 10, 'XL': 40, 'L': 50, 'XC': 90, 'C': 100, 'CD': 400, 'D': 500, 'CM': 900, 'M': 1000}        while s:            for i in range(2, 0, -1):                if s[:i] in num:                    rec += num[s[:i]]                    s = s[i:]                    break        return rec

14.最长公共前缀 54ms

class Solution:    def longestCommonPrefix(self, strs):        """        :type strs: List[str]        :rtype: str        """        if strs:            for i in range(len(strs[0])+1):                for s in range(1, len(strs)):                    if strs[0][:i] != strs[s][:i]:                        if strs[0][:i - 1]:                            return strs[0][:i-1]                        else:                            return ""            return strs[0]        else:            return ""

 

转载于:https://www.cnblogs.com/FanMLei/p/10501004.html

你可能感兴趣的文章
IE9上特定网站不断崩溃的故障
查看>>
Oracle序列使用:建立、删除
查看>>
我的友情链接
查看>>
秀一秀我的微软MVP(最有价值专家)的大礼包和水晶奖杯!
查看>>
Unix command to find CPU Utilization
查看>>
打开.264后缀名格式的文件
查看>>
MYSQL查看 table 表状态常用的命令
查看>>
Java——容器(Comparable)
查看>>
二叉树的非递归遍历
查看>>
数据库连接客户端使用(db2,oracle,mysql)
查看>>
Spring的事务管理难点剖析:应用分层的迷惑
查看>>
mysql主从配置
查看>>
基于QT Plugin框架结构
查看>>
Windows Server 2008 R2 如何启用WINS服务
查看>>
一天一种设计模式之五-----代理模式
查看>>
抽象类和模板模式
查看>>
《ASP.NET MVC企业实战》(二) MVC开发前奏
查看>>
32.C#--方法中使用out参数做登录判断
查看>>
error
查看>>
quartz+spring框架动态调整频率实践
查看>>