题解 | #字符串变形#

字符串变形

http://www.nowcoder.com/practice/c3120c1c1bc44ad986259c0cf0f0b80e

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param s string字符串 
# @param n int整型 
# @return string字符串
#
class Solution:
    def trans(self , s: str, n: int) -> str:
        # write code here
        # so we need to flip the word and also lower to capital? we start the flipping from the empty string? 
        # step 1 loop and when it encounters space then call flipping method 
        # step 2 when all flipping is done. Then another parallel loop to revert the string
        
        # string is immutable but slicable. list is mutable and slicable 
        # dist is muttable and slicable, tuple is immutable and slicable?
        # user can fill in or concatenate string elments into the initialize empty string. But Not be able to 
        #change the element in the string that is already there.
        
        #Mutable sequences can be changed after creation. Some of Python’s mutable data types are: lists, byte           #arrays, sets, and dictionaries.
        # immutabel after creation, strings, tuple 
        #https://towardsdatascience.com/immutable-vs-mutable-data-types-in-python-e8a9a6fcfbdc
        
        ls=[0]*len(s)
        for i in range(len(s)):
            if s[i].islower():
                ls[i]=(s[i].upper())
            else:
                ls[i]=(s[i].lower())
        new_str=''
        cur = len(ls)
        for i in range(len(ls)-1,-1,-1):
            if ls[i]==' ':
                new_str=new_str + ''.join(ls[i+1:cur]) +' '
                cur= i # 
        new_str = new_str + ''.join(ls[:cur])
        return new_str
                
        
            
            
全部评论

相关推荐

2024-12-26 20:46
复旦大学 C++
国棉17厂丶小王:拿了offer的那个周末晚上去网吧通宵,去网吧不知道玩什么刷了lc的每日一题,然后试着第一次打开了三角洲行动,从此少了一个已经刷了700道题的lc用户,但是烽火地带多了一只🐭🐭
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务