小E学Python PART2 Leetcode_String

小E学Python PART2 Leetcode_String


光说不练假把式,要多做题了。
写的都很繁琐而且可能有BUG,要继续修修补补,望各位不吝赐教:)


easy_part

2019.05.25

T13 Roman to Integer
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

SymbolValue
I1
V5
X10
L50
C100
D500
M1000

I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.**
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
Example 1:
Input: “III”
Output: 3

class Solution:
    def romanToInt(self, s: str,) -> int:
        dic={'I':1,'V':5,'X':10,'L':50,'C':100,
             'D':500,'M':1000,'IV':-2,'IX':-2,'XL':-20,'XC':-20,'CD':-200,'CM':-200}
        list_key=list(dic)
        sum=0
        for i in list_key:
            num=s.count(i)
            sum+=num*dic[i]
        return sum

总结:
这是leetcode上做的第二题,犯了很多傻问题
①用了input()->不用自己输入
②用了output()->这是一个函数,只需要用return
③想要简化多个键对应一个值,改成了(str1,str2):value,发现提示s是’str’而不是‘tuple’
因为多个键组成了一个元组而不是或的关系
④注意缩进!!(报错现象“dic” is undefined)

Runtime: 68 ms, faster than 73.85% of Python3 online submissions for Roman to Integer.
Memory Usage: 13.2 MB, less than 70.16% of Python3 online submissions for Roman to Integer.

T14 Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string “”.

class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        str_re=''
        try:
            minlen_str=strs[0]
            lens=len(strs)
            for i in range(lens):
                length=len(minlen_str)
                if (length>len(strs[i])):
                    minlen_str=strs[i] 
            length=len(minlen_str)
            for k in range(length):
                num=0
                for j in range(lens):
                    if minlen_str[k]==strs[j][k]:
                        num+=1
                if num==lens:
                    str_re+=minlen_str[k]
                else:
                    break
        except:
            return('')
        return str_re

总结:
:(我是个渣渣,交了六次才AC,最大的原因,没读完整题目,整体方向错误。
①仔细读题,不会的英文单词请查清楚! prefix-前缀
②循环后检查是否更新了变量的值,我爱debug:)
写得太杂了,内存占用太多,要看看discussion区域进行改进,还有,学习数据结构!!

Runtime: 40 ms, faster than 75.30% of Python3 online submissions for Longest Common Prefix.
Memory Usage: 13.3 MB, less than 20.35% of Python3 online submissions for Longest Common Prefix.


2019.05.27

T28. Implement strStr()
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:
Input: haystack = “hello”, needle = “ll”
Output: 2
Example 2:
Input: haystack = “aaaaa”, needle = “bba”
Output: -1
Clarification:
What should we return when needle is an empty string? This is a great question to ask during an interview.
For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C’s strstr() and Java’s indexOf().

class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
            if needle == '':
                return 0
            elif strh<strn:
                return -1
            else:
                if (needle not in haystack):
                    return -1
                else:
                    index=haystack.find(needle)        
                    return index

总结:
第一次这么快的速度!!
学了一个新的函数 str.find(),可以索引到字符串中第一个匹配的下标
Runtime: 24 ms, faster than 99.96% of Python3 online submissions for Implement strStr().
Memory Usage: 13.3 MB, less than 54.11% of Python3 online submissions for Implement strStr().


2019.05.29

T67. Add Binary
Given two binary strings, return their sum (also a binary string).
The input strings are both non-empty and contains only characters 1 or 0.
Example 1:
Input: a = “11”, b = “1”
Output: “100”
Example 2:
Input: a = “1010”, b = “1011”
Output: “10101”

def addBinary( a: str, b: str) -> str:
    final=0
    sum=str(int(a)+int(b))
    for i in range(-1,-(len(sum)+1),-1):
        final+=int(sum[i])*(2**abs(i+1))
    final=bin(final).replace('0b', '')
    return final

总结:
睡太少果然脑子都不好使,重写了好几次
bin()转换成二进制会自带前缀0b
Runtime: 36 ms, faster than 97.06% of Python3 online submissions for Add Binary.
Memory Usage: 13.3 MB, less than 20.90% of Python3 online submissions for Add Binary.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值