python 每日一练 Day1

Q1:

最后一个单词长度:给你一个字符串 s,由若干单词组成,单词之间用空格隔开。返回字符串中最后一个单词的长度。如果不存在最后一个单词,请返回 0 。
单词 是指仅由字母组成、不包含任何空格字符的最大子字符串。

class solution():
    def length(self,s):
        if len(s)==0:
            return 0
        temp=s.split(' ')
        temp=[t for t in temp if len(t)>0]
        if len(temp)==0:
            return 0
        else:
            return len(temp[-1])
s=solution()
print(s.length(s='hello world'))

ex2:


class Solution(object):
    def lengthOfLastWords(self, s: str) -> int:
        return [len(t) for t in s.split(" ")][-1]


if __name__ == "__main__":
    s = Solution()
    print(s.lengthOfLastWords(s="hello world"))

关于python的新特性函数注释(定义函数时使用“:”及“ ->”符号)​​​​​​https://blog.csdn.net/finalkof1983/article/details/87943032?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163841770416780271551974%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=163841770416780271551974&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-87943032.first_rank_v2_pc_rank_v29&utm_term=self%2C+s%3A+str&spm=1018.2226.3001.4187https://blog.csdn.net/finalkof1983/article/details/87943032?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163841770416780271551974%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=163841770416780271551974&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-87943032.first_rank_v2_pc_rank_v29&utm_term=self%2C+s%3A+str&spm=1018.2226.3001.4187icon-default.png?t=LA92https://blog.csdn.net/finalkof1983/article/details/87943032?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163841770416780271551974%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=163841770416780271551974&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-87943032.first_rank_v2_pc_rank_v29&utm_term=self%2C+s%3A+str&spm=1018.2226.3001.4187

s.split()用法:分割;

#split()分隔符,无视空格
s0='we are students'#一个空格
s1='we are   students'#三个空格

s0=s0.split()
print(s0)
print(len(s0))

s1=s1.split()
print(s1)
print(len(s1))

##输出
['we', 'are', 'students']
3
['we', 'are', 'students']
3
#split('')分隔符,,分割多个空格
s0='we are students'#一个空格
s1='we are   students'#三个空格

s0=s0.split(' ')
print(s0)
print(len(s0))

s1=s1.split(' ')
print(s1)
print(len(s1))
##输出
['we', 'are', 'students']
3
['we', 'are', '', '', 'students']
5

Q2:

给你一个只包含 '(' 和 ')' 的字符串,找出最长有效(格式正确且连续)括号子串的长度。
 
示例 1:
输入:s = "(()"
输出:2
解释:最长有效括号子串是 "()"
示例 2:
输入:s = ")()())"
输出:4
解释:最长有效括号子串是 "()()"
示例 3:
输入:s = ""
输出:0

import pdb
class Solution(object):
    def jj(self,s):
        ls=len(s)
        stack=[]
        data=[0]*ls
        for i in range(ls):
            curr=s[i]
            if curr=='(':
                stack.append(i)
            else:
                if len(stack)>0:
                    data[i]=1
                    data[stack.pop(-1)]=1
                    print(data)
        tep,res=0,0
        for t in data:
            print(t)
            if t<1:
                tep+=1
                print('tep=',tep)
                print('res=',res)
            else:
                res=max(tep,res)
                tep=0
                print('tep=', tep)
                print('res=', res)
        return max(tep,res)
s=Solution()
print(s.jj(')()())'))

THE END
分享
二维码
< <上一篇

)">
下一篇>>