The sword refers to offer 2. Replace space (String)

Source: Internet
Author: User

Title Description

Implement a function that replaces each space in a string with "%20". For example, when the string is we are Happy. The string after substitution is we%20are%20happy.

Title Address

https://www.nowcoder.com/practice/4060ac7e3e404ad1a894ef3e17650423?tpId=13&tqId=11155&tPage=1&rp=2 &ru=%2fta%2fcoding-interviews&qru=%2fta%2fcoding-interviews%2fquestion-ranking

Idea 1: Open a new string, traverse the given string, and add a% 20 after the new string when the character is a space, otherwise, add the original character

Idea 2: Convert a string to a list, traverse a string, and, in turn, add '% ', ' 2 ', ' 0 ' to the list after the character is a space; otherwise, add the original character

Idea 3: If you add '%20 ' every time you encounter a space, the number behind the space needs to be moved back frequently. With this kind of moving problem, we can try to give the final desired length, then scan backwards, and then given two pointers to ensure the positioning.

Python

#-*-coding:utf-8-*-classSolution:#s Source string    defReplacespace (self, s):#Write code here        ifLen (s) = =0:return "'        #Idea 1        #newstring = ' '        #For I in S:        #if i = = ':        #newstring + = '%20 '        #Else:        #newstring + = i        #return newstring        #Idea 2:        #lis = List (s)        #newlis = []        #For i in LIS:        #if i = = ':        #newlis.append ('% ')        #newlis.append (' 2 ')        #newlis.append (' 0 ')        #Else:        #newlis.append (i)        #return "". Join (Newlis)        #Idea 3Count =0 forIinchS:ifi = =' ': Count+ = 1Newlen= Len (s) + count * 2Newstr= Newlen *[None] indexofnew, Indexofori= Len (newstr)-1, Len (s)-1 whileIndexofnew >= 0 andIndexofnew >=Indexofori:ifS[indexofori] = =' ': Newstr[indexofnew-2:INDEXOFNEW+1] = ['%','2','0'] Indexofnew-= 3Indexofori-= 1Else: Newstr[indexofnew]=S[indexofori] Indexofnew-= 1Indexofori-= 1return "". Join (NEWSTR)if __name__=='__main__': Result= Solution (). Replacespace ('We are happy.')    Print(Result)

The sword refers to offer 2. Replace space (String)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.