Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
📢 preface
🚀 Algorithm 🚀 |
- 🌲 punch in an algorithm every day, which is not only a learning process, but also a sharing process 😜
- 🌲 tip: the programming languages used in this column are C# and Java
- 🌲 to maintain a state of learning every day, let us work together to become a god of algorithms 🧐!
- 🌲 today is the force button algorithm continues to punch the card 18 days 🎈!
🚀 Algorithm 🚀 |
🌲 Example of original problem
You are given a string s, consisting of several words separated by space characters. Returns the length of the last word in the string.
A word is the largest substring that consists only of letters and does not contain any space characters.
The sample1: Enter: s ="Hello World"Output:5
Copy the code
The sample2: Enter: s =" fly me to the moon "Output:4
Copy the code
The sample3: Input: nums = [0] output:0
Copy the code
The sample3: Enter: s ="luffy is still joyboy"Output:6
Copy the code
Tip:
- 1 <= s.length <= 104
- The s consists of only letters and Spaces
- There is at least one word in s
🌻C# method 1: traverse
Thinking analytical
- Topic request
The length of the last word
Directly,Reverse traversal
It is good - Encounter blank direct
Jump out of the
, not space lengthGal.
!
Code:
public class Solution {
public int LengthOfLastWord(string s) {
int count = 0;
for (int i = s.Length - 1; i >= 0; i--)
{
if(s[i] ! =' ') count++; // Letters detected
if (s[i] == ' '&& count ! =0) break; // Exit when the last word meets a space, and solve the case where there is a space after the last word ("a ")
}
returncount; }}Copy the code
The execution result
By execution time:72Ms, in all CDefeated 69.06% of users in # commitMemory consumption:22.2MB, in all C# beat 64.90% of users in submission
Copy the code
Complexity analysis
Time: O(n) Space: O(1)
Copy the code
🌻Java Method 1: Traversal
Thinking analytical
- Since it’s about the length of the last word, let’s just
Iterate through the string from back to front
- Two things will happen at this time:
1. Take the string Hello World as an example. The string is traversed from the back to the front until the end of the string or a space is encountered, that is, the length of the last word World is 5 2. Take the string Hello World as an example. You need to filter out the Spaces at the end of the string before performing the operation in the first case. That is, the last word is considered to be World and the length is 5
- The complete process is:
Filter out the last string to be a space, traverse backwards, then find the word header (that is, before the next space), subtract the length of the last word!
Code:
class Solution {
public int lengthOfLastWord(String s) {
if(s == null || s.length() == 0) return 0;
int count = 0;
for(int i = s.length()-1; i >= 0; i--){
if(s.charAt(i) == ' ') {if(count == 0) continue;
break;
}
count++;
}
returncount; }}Copy the code
The execution result
By execution time:0Ms, beat out all Java commits100% user memory consumption:36.3MB, beat out all Java commits96.52% of the userCopy the code
Complexity analysis
Time: O(n) Space: O(1)
Copy the code
💬 summary
- Today is the 18th day of buckle algorithm clocking!
- The article USES the
C#
andJava
Two programming languages to solve the problem - Some methods are also written by the god of reference force buckle, and they are also shared while learning, thanks again to the algorithm masters
- That’s the end of today’s algorithm sharing, see you tomorrow!