Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 39054d6

Browse files
authored
58 solved. (#37)
1 parent bc3804b commit 39054d6

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ continually updating 😃.
4343
* [17. Letter Combinations of a Phone Number](./src/0017_letter_combination_of_a_phone_number/letter_combination_of_phone_number.go)   *`tree`*
4444
* [20. Valid Parentheses](./src/0020_valid_parentheses/valid_parentheses.go)   *`stack`*
4545
* [28. Implement strStr()](src/0028_implement_strstr/implement_strstr.go)   *`double index`*
46+
* [58. Length of Last Word](src/0058_length_of_last_word/len_of_last_word.go)
4647
* [67. Add Binary](./src/0067_add_binary/add_binary.go)   *`brute force`*
4748
* [76. Minimum Window Substring](./src/0076_minimum_window_substring/minimum_window_substring.go)    *`sliding window`*
4849
* [125. Valid Palindrome](./src/0125_valid_palindrome/valid_palindrome.go)   *`string;`*  *`double index`*
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
58. Length of Last Word
3+
https://leetcode.com/problems/length-of-last-word/
4+
5+
Given a string s consists of upper/lower-case alphabets and empty space characters ' ',
6+
return the length of last word in the string.
7+
If the last word does not exist, return 0.
8+
9+
Note: A word is defined as a character sequence consists of non-space characters only.
10+
*/
11+
// time: 2019年01月02日
12+
13+
package lenoflastword
14+
15+
// time complexity: O(n)
16+
// space complexity: O(1)
17+
func lengthOfLastWord(s string) int {
18+
var (
19+
n = len(s)
20+
cur = n - 1
21+
)
22+
23+
for cur >= 0 {
24+
if n-1 == cur && s[cur] == 32 {
25+
cur--
26+
n--
27+
continue
28+
}
29+
if s[cur] != 32 {
30+
cur--
31+
} else {
32+
break
33+
}
34+
}
35+
return n - cur - 1
36+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package lenoflastword
2+
3+
import "testing"
4+
5+
func TestLengthOfLastWord(t *testing.T) {
6+
s := "hello world "
7+
expected := 5
8+
if res := lengthOfLastWord(s); res != expected {
9+
t.Errorf("expected %d, got %d", expected, res)
10+
}
11+
}

‎src/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
|0035|[35. Search Insert Position](0035_search_insert_position/search_insert_position.go)|Easy|*`binary search`*|
2727
|0048|[48. Rotate Image](0048_rotate_image/rotate_image.go)|Medium|*`array`*|
2828
|0053|[53. Maximum Subarray](0053_maximum_subarray/maximum_subarray.go)|Easy|*`dynamic programming`*|
29+
|0058|[58. Length of Last Word](0058_length_of_last_word/len_of_last_word.go)|Easy||
2930
|0061|[Rotate List](./0061_rotate_list/rotate_list.go)|Medium|*`linked list`*|
3031
|0062|[Unique Paths](./0062_unique_paths/unique_paths.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*|
3132
|0063|[Unique Paths 2](./0063_unique_paths_2/unique_paths2.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*|

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /