Skip to content

Commit 3996e1d

Browse files
length of last word
1 parent cc226b2 commit 3996e1d

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

‎EASY/src/easy/LengthofLastWord.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package easy;
2+
3+
public class LengthofLastWord {
4+
5+
public int lengthOfLastWord(String s) {
6+
if(s == null || s.length() == 0) return 0;
7+
s = s.trim();
8+
int n = s.length()-1;
9+
while(n >= 0 && s.charAt(n) != ' '){
10+
n--;
11+
}
12+
return s.length() - n - 1;
13+
}
14+
15+
}

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
|75|[Sort Colors](https://leetcode.com/problems/sort-colors/)|[Solution]|O(n)|O(1)|Medium
4141
|68|[Text Justification](https://leetcode.com/problems/text-justification/)|[Solution](../../blob/master/HARD/src/hard/TextJustification.java)|O(n)|O(1)|Hard|
4242
|67|[Add Binary](https://leetcode.com/problems/add-binary/)|[Solution](../../blob/master/EASY/src/easy/AddBinary.java)|O(n)|O(1)|Easy|
43+
|58|[Length of Last Word](https://leetcode.com/problems/length-of-last-word/)|[Solution](../../blob/master/EASY/src/easy/LengthofLastWord.java)|O(n)|O(1)|Easy|
4344
|56|[Merge Intervals](https://leetcode.com/problems/merge-intervals/)|[Solution](../../blob/master/HARD/src/hard/MergeIntervals.java)|O(n*logn)|O(1)|Hard|
4445
|43|[Multiply Strings](https://leetcode.com/problems/multiply-strings/)|[Solution]|||Medium
4546
|23|[Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/)|[Solution](../../blob/master/HARD/src/hard/MergeKSortedList.java)|O(n*logk)|O(logk)|Hard|Heap

0 commit comments

Comments
 (0)