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 dd53604

Browse files
authored
165 solved. (#50)
1 parent 1133d7d commit dd53604

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ continually updating 😃.
4747
* [67. Add Binary](./src/0067_add_binary/add_binary.go)   *`brute force`*
4848
* [76. Minimum Window Substring](./src/0076_minimum_window_substring/minimum_window_substring.go)    *`sliding window`*
4949
* [125. Valid Palindrome](./src/0125_valid_palindrome/valid_palindrome.go)   *`string;`*  *`double index`*
50+
* [165. Compare Version Numbers](src/0165_compare_version_numbers/compare_version_numbers.go)
5051
* [344. Reverse String](./src/0344_reverse_string/reverse_string.go)   *`string;`*  *`double index`*
5152
* [345. Reverse Vowels of a String](./src/0345_reverse_vowels_of_a_string/reverse_vowels.go)   *`string;`*  *`double index`*
5253
* [438. Find All Anagrams in a String](./src/0438_all_anagrams_in_a_string/all_anagrams_in_a_string.go)   *`sliding window`*
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
165. Compare Version Numbers
3+
https://leetcode.com/problems/compare-version-numbers/
4+
5+
Compare two version numbers version1 and version2.
6+
If version1 > version2 return 1; if version1 < version2 return -1;otherwise return 0.
7+
8+
You may assume that the version strings are non-empty and contain only digits and the . character.
9+
10+
The . character does not represent a decimal point and is used to separate number sequences.
11+
12+
For instance, 2.5 is not "two and a half" or "half way to version three",
13+
it is the fifth second-level revision of the second first-level revision.
14+
15+
You may assume the default revision number for each level of a version number to be 0.
16+
For example, version number 3.4 has a revision number of 3 and 4 for its first and second level revision number.
17+
Its third and fourth level revision number are both 0.
18+
19+
Note:
20+
21+
Version strings are composed of numeric strings separated by dots . and this numeric strings may have leading zeroes.
22+
Version strings do not start or end with dots, and they will not be two consecutive dots.
23+
*/
24+
// time: 2019年01月07日
25+
26+
package compareversionnumbers
27+
28+
import (
29+
"strconv"
30+
"strings"
31+
)
32+
33+
// time complexity: O( max n, m )
34+
// space complexity: O(n+m)
35+
func compareVersion(version1 string, version2 string) int {
36+
var (
37+
v1 = strings.Split(version1, ".")
38+
v2 = strings.Split(version2, ".")
39+
)
40+
for i := 0; i < len(v1) || i < len(v2); i++ {
41+
var v1N, v2N int
42+
if i < len(v1) {
43+
v1N, _ = strconv.Atoi(v1[i])
44+
}
45+
if i < len(v2) {
46+
v2N, _ = strconv.Atoi(v2[i])
47+
}
48+
49+
if v1N > v2N {
50+
return 1
51+
} else if v1N < v2N {
52+
return -1
53+
}
54+
}
55+
return 0
56+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package compareversionnumbers
2+
3+
import "testing"
4+
5+
func TestCompareVersion(t *testing.T) {
6+
type arg struct {
7+
version1 string
8+
version2 string
9+
}
10+
11+
testCases := []arg{
12+
{version1: "0.1", version2: "1.1"},
13+
{version1: "1.0.1", version2: "1"},
14+
{version1: "1.01", version2: "1.001"},
15+
}
16+
expected := []int{-1, 1, 0}
17+
18+
for index, data := range testCases {
19+
if res := compareVersion(data.version1, data.version2); res != expected[index] {
20+
t.Errorf("expected %d, got %d", expected[index], res)
21+
}
22+
}
23+
}

‎src/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
|0144|[144. Binary Tree Preorder Traversal](0144_binary_tree_preorder_traversal/binary_tree_preorder_traversal.go)|Medium|*`binary tree`*|
6161
|0150|[150. Evaluate Reverse Polish Notation](0150_evaluate_reverse_polish_notation/evaluate_reverse_polish_notation.go)|Medium|*`stack`*|
6262
|0153|[153. Find Minimum in Rotated Sorted Array](0153_find_minimum_in_rotated_sorted_array/fmirsa.go)|Medium|*`binary search`*|
63+
|0165|[165. Compare Version Numbers](0165_compare_version_numbers/compare_version_numbers.go)|Medium|*`string`*|
6364
|0167|[Two Sum II - Input array is sorted](./0167_two_sum2/two_sum2.go)|Easy|*`对撞指针(双索引)`*|
6465
|0198|[House Robber](./0198_house_robber/house_robber.go)|Easy|*`memory search;`* *`dynamic programming`*|
6566
|0209|[Minimum Size Subarray Sum](./0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)|Medium|*`sliding window`*|

0 commit comments

Comments
(0)

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