File tree 4 files changed +108
-0
lines changed
4 files changed +108
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ # @param {string} version1
3
+ # @param {string} version2
4
+ # @return {integer}
5
+ def compareVersion (self , version1 , version2 ):
6
+ version1_len = len (version1 )
7
+ version2_len = len (version2 )
8
+
9
+ v1 = 0
10
+ endmark1 = 1
11
+ sub_version1 = ""
12
+ if version1_len > 0 :
13
+ for idx1 in range (version1_len ):
14
+ if version1 [idx1 ] == '.' :
15
+ endmark1 = 0
16
+ break
17
+ if endmark1 == 1 :
18
+ idx1 += 1
19
+ v1 = int (version1 [0 :idx1 ])
20
+ sub_version1 = version1 [idx1 + 1 :]
21
+
22
+ v2 = 0
23
+ endmark2 = 1
24
+ sub_version2 = ""
25
+ if version2_len > 0 :
26
+ for idx2 in range (version2_len ):
27
+ if version2 [idx2 ] == '.' :
28
+ endmark2 = 0
29
+ break
30
+ if endmark2 == 1 :
31
+ idx2 += 1
32
+ v2 = int (version2 [0 :idx2 ])
33
+ sub_version2 = version2 [idx2 + 1 :]
34
+
35
+ if v1 > v2 :
36
+ return 1
37
+ elif v1 < v2 :
38
+ return - 1
39
+ else :
40
+ if endmark1 == 1 and endmark2 == 1 :
41
+ return 0
42
+ else :
43
+ return self .compareVersion (sub_version1 ,sub_version2 )
44
+
45
+
46
+
47
+
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ # @param {string} haystack
3
+ # @param {string} needle
4
+ # @return {integer}
5
+ def strStr (self , haystack , needle ):
6
+ needle_len = len (needle )
7
+ if needle_len == 0 :
8
+ return 0
9
+
10
+ for idx in range (0 , len (haystack )- needle_len + 1 ):
11
+ substr = haystack [idx :idx + needle_len ]
12
+ if substr == needle :
13
+ return idx
14
+
15
+ return - 1
16
+
17
+
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ # @param {string} s
3
+ # @return {integer}
4
+ def lengthOfLongestSubstring (self , s ):
5
+ stack = []
6
+ max_sublen = 0
7
+ for c in s :
8
+ if c not in stack :
9
+ stack .append (c )
10
+ max_sublen = max (max_sublen , len (stack ))
11
+ else :
12
+ idx = stack .index (c )
13
+ stack = stack [idx + 1 :]
14
+ stack .append (c )
15
+ return max_sublen
16
+
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ # @param {string} path
3
+ # @return {string}
4
+ def simplifyPath (self , path ):
5
+ stack = []
6
+ pathset = path .split ('/' )
7
+ for p in pathset :
8
+ if p == '' :
9
+ pass
10
+ elif p == '.' :
11
+ pass
12
+ elif p == '..' :
13
+ if len (stack ) > 0 :
14
+ stack .pop ()
15
+ else :
16
+ pass
17
+ else :
18
+ stack .append (p )
19
+
20
+ simple_path = ''
21
+ if len (stack ) > 0 :
22
+ for p in stack :
23
+ simple_path += '/' + p
24
+ return simple_path
25
+ else :
26
+ return '/'
27
+
28
+
You can’t perform that action at this time.
0 commit comments