力扣6~10题

题6(中等):

思路:

这个相较于前面只能是简单,个人认为,会print打印菱形都能搞这个,直接设置一个2阶数组就好了,只要注意位置变化就好了

python代码:

def convert(self, s: str, numRows: int) -> str:

#创建n堆空字符串

new_list=['' for i in range(numRows)]

#遍历字符串

k=0

flag=1

for i in range(len(s)):

new_list[k]+=s[i]

k+=flag

if numRows-1==0:

k=0

else:

if k==0:

flag=1

elif k==numRows-1:

flag=-1

return ''.join(new_list)

题7(中等):

思路:

怎么感觉难度降下来了,这个用python太容易了啊,用c++应该也不难吧,翻转字符串一个栈不就好了吗?

python代码:

class Solution:

def reverse(self, x: int) -> int:

x_str=str(x)

if x_str[0]=='-':

x_str='-'+x_str[:0:-1]

else:

x_str=x_str[::-1]

i=int(x_str) if int(x_str)>=-2**31 and int(x_str)<= 2**31-1 else 0

return i

题8(中等):

思路:

这个截断和我想的截断有点不同啊,我以为还要转二进制然后断呢

python代码:

class Solution:

def myAtoi(self, s: str) -> int:

s=s.strip()

s_num=''

for i in range(len(s)):

if i==0 and (s[i]=='+' or s[i]=='-'):

s_num+=s[i]

continue

if ord(s[i])>=ord('0') and ord(s[i])<=ord('9'):

s_num+=s[i]

continue

else:

break

if s_num=='+' or s_num=='-':

s_num=0

s_num=int(s_num if s_num!='' else 0)

if s_num<=-2**31:

s_num=-2**31

if s_num>=2**31-1:

s_num=2**31-1

return s_num

题9(简单):

思路:

这个对于python有点过于简单了吧

python代码:

class Solution:

def isPalindrome(self, x: int) -> bool:

return str(x)==str(x)[::-1]

题9(困难):

思路:

我只能说我不是理解正则,毕竟爬虫我都不管啥,直接.*?,导致我理解错了题意思,我当时以为*是可以匹配任意了,然后写一晚上都没成功,看评论才理解意思,其实理解了写起来就清晰了,采用的方法是递归,时间比较消耗,所以要预处理一下,不然超时

python代码:

复制代码
class Solution:
    def isMatch(self, s: str, p: str) -> bool:
        if p=='':
            return s==''
        if s=='':
            if len(p)!=2 and p[1]!='*':
                return False
            if len(p)==2 and p[1]=='*':
                return True

        i=0
        #预处理,
        while 1:
            if p[i]=='*':
                if i+2<len(p) and p[i+2]=='*':
                    if p[i-1]==p[i+1]:
                        p=p[:i+1]+p[i+3:]
            i+=1
            if i>=len(p):
                break

        s_p=0
        p_p=0
        while 1:
            if s_p>=len(s) and p_p>=len(p):
                return True
            if p_p>=len(p):
                return False


            if p_p+1<=len(p)-1 and p[p_p+1]=='*':
                for i in range(s_p,len(s)):
                    if s[i]!=p[p_p] and p[p_p]!='.':
                        break
                    else:
                        if self.isMatch(s[i:],p[p_p]+p[p_p+2:]):
                            return True
                p_p+=2
            else:
                if s_p>=len(s):
                    return False
                if p[p_p]==s[s_p] or p[p_p]=='.':
                    p_p+=1
                    s_p+=1
                else:
                    return False

写得很气,所以赶工,注释都没有,再看的话又烦,感觉屎山一样,做的最久的一次,写了3个版本的代码

相关推荐
惯导马工1 小时前
【论文导读】IDOL: Inertial Deep Orientation-Estimation and Localization
深度学习·算法
老姜洛克1 小时前
自然语言处理(NLP)之n-gram从原理到实战
算法·nlp
1白天的黑夜11 小时前
哈希表-49.字母异位词分组-力扣(LeetCode)
c++·leetcode·哈希表
CoovallyAIHub2 小时前
基于YOLO集成模型的无人机多光谱风电部件缺陷检测
深度学习·算法·计算机视觉
CoovallyAIHub2 小时前
几十个像素的小目标,为何难倒无人机?LCW-YOLO让无人机小目标检测不再卡顿
深度学习·算法·计算机视觉
怀旧,2 小时前
【C++】19. 封装红⿊树实现set和map
linux·c++·算法
往事随风去2 小时前
Redis的内存淘汰策略(Eviction Policies)有哪些?
redis·后端·算法
神里流~霜灭2 小时前
(C++)数据结构初阶(顺序表的实现)
linux·c语言·数据结构·c++·算法·顺序表·单链表
一只乔哇噻3 小时前
java后端工程师进修ing(研一版 || day41)
java·开发语言·学习·算法
愚润求学3 小时前
【贪心算法】day7
c++·算法·leetcode·贪心算法