preface
As the name suggests, the main purpose of this blog is to record the process of writing problems in Leetcode. The whole code is written in Python3. The website is Leetcode China. The advantages of this website are: 1) Pure Chinese, to solve the problem of difficult reading English; 2) Fast webpage opening. In addition to the python3 solution code for each problem, this column will try to use a variety of solutions, and also record some of my own insights and thoughts during the process of doing the problem. Facilitate later review consult.
The title
Given a string containing only uppercase and lowercase letters and Spaces, return the length of its last word.
If the last word does not exist, return 0.
Description: A word is a string of letters that does not contain any Spaces.
Example:
Input: “Hello World” Output: 5
One) Do not call any Python package version
1, first, to understand the algorithm idea, first without calling any function manual manual complex, the idea is as follows: first remove the end of the string space: traversal backwards, determine the space is the length of the string minus one, z until you encounter the word letter. It then iterates from the beginning, moving the start pointer to the first word after a space. Until you get to the last letter of the last word. Return n-start (n is larger than the index at the end, so you don’t need to add one)
class Solution:
def lengthOfLastWord(self, s):
""" :type s: str :rtype: int """
n = len(s)
i = n- 1
while i>= 0:
if s[i].isspace():[I] ==" "
n -= 1
i -= 1
else:
break
if n == 0:return 0
start = 0
for j in range(n):
if s[j].isspace():
start = j+1
return n-start
Copy the code
2. Of course, it is also possible to set a counter directly.
class Solution(object):
def lengthOfLastWord(self, s):
n = len(s)
i = n- 1
while i>= 0:
if s[i].isspace():[I] ==" "
n -= 1
i -= 1
else:
break
counter=0
for i in (s.strip()):
if i == "":
counter = 0
else:
counter += 1
return counter
Copy the code
If a space is returned, the length between the current index and start will be returned. If a space is returned, the length between start and index will be returned.
Call the Python-specific package version
Of course, in real life encounter this situation, of course, there is no need to work hard to implement, just need to use python’s two functions to implement. 1) strip (). This function is used to remove as many characters (defaults to Spaces or newlines) or sequences of characters specified at the beginning and end of a string as possible. Interestingly, the word “strip” means “strip”, which demonstrates the colloquial and colorful naming of Python functions.
Such as:
str = "0000000 hello 0000000";
str.strip( '0' ); # STR = "Hello"
str2 = "Hello?"
str2.strip(); #str2=" Hello"
Copy the code
Note: this method can only delete the beginning or end of the character, not the middle part of the character.
Alternatively, this function can match strings out of order:
str = "Hello 321231 123"
print (str.strip( '123' )) # STR = "Hello"
Copy the code
2) the split (). This function returns a list by slicing the string with the specified delimiter. Its English meaning has cut, cut meaning, named also very image. Use method: str.split(STR =” separator “, num= split times where STR defaults to all null characters, including Spaces, newlines (\n), tabs (\t), etc. You can also specify it yourself.
Usage Examples:
str = "Hi, I'm fine. We're all fine."
str.split(', '); # STR = [" Hello "," I'm fine "," We're all fine "]
str.split(', ', 1);# STR = [" Hello "," I'm fine, we're all fine "]
print str.split() # STR = [" Hello "," ah, I'm fine "," ok, we're all fine "," ok "]
Copy the code
Len (s.trip ().split(” “)[-1]) return len(s.trip ().split(” “)[-1])
Or that sentence, in actual use can be so used, but do the algorithm or as far as possible their own manual implementation, to understand the idea of the algorithm.