File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- 学习笔记
1+ Atoi代码演示模板
2+
3+ class Solution(object):
4+ def myAtoi(self, s):
5+ if len(s) == 0 : return 0
6+ ls = list(s.strip())
7+
8+ sign = -1 if ls[ 0] == '-' else 1
9+ if ls[ 0] in [ '-','+'] : del ls[ 0]
10+ ret, i = 0, 0
11+ while i < len(ls) and ls[ i] .isdigit() :
12+ ret = ret* 10 + ord(ls[ i] ) - ord('0')
13+ i += 1
14+ return max(-2** 31, min(sign * ret,2** 31-1))
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def findAnagrams (self , s : str , p : str ) -> List [int ]:
3+ len_s = len (s )
4+ len_p = len (p )
5+ if len_s < len_p :
6+ return []
7+ char_dict = {}
8+ win_dict = {}
9+ diff = len_p
10+ for ch in p :
11+ char_dict [ch ] = char_dict .get (ch , 0 ) + 1
12+ win_dict [ch ] = 0
13+ for i in range (len_p ):
14+ if s [i ] in win_dict :
15+ if win_dict [s [i ]] < char_dict [s [i ]]:
16+ diff -= 1
17+ win_dict [s [i ]] += 1
18+ p , q = 0 , len_p - 1
19+ res = []
20+ if diff == 0 :
21+ res .append (0 )
22+ while q + 1 < len_s :
23+ q += 1
24+ if s [q ] in win_dict :
25+ if win_dict [s [q ]] < char_dict [s [q ]]:
26+ diff -= 1
27+ win_dict [s [q ]] += 1
28+ if s [p ] in win_dict :
29+ if win_dict [s [p ]] <= char_dict [s [p ]]:
30+ diff += 1
31+ win_dict [s [p ]] -= 1
32+ p += 1
33+ if diff == 0 :
34+ res .append (p )
35+ return res
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def firstUniqChar (self , s : str ) -> int :
3+ count = collections .Counter (s )
4+ for idx , ch in enumerate (s ):
5+ if count [ch ] == 1 :
6+ return idx
7+ return - 1
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def longestPalindrome (self , s : str ) -> str :
3+ if s == s [::- 1 ]:
4+ return s
5+ max_len = 1
6+ res = s [0 ]
7+ for i in range (len (s ) - 1 ):
8+ for j in range (i + 1 , len (s )):
9+ if j - i + 1 > max_len and s [i :j + 1 ] == s [i :j + 1 ][::- 1 ]:
10+ max_len = j - i + 1
11+ res = s [i :j + 1 ]
12+ return res
You can’t perform that action at this time.
0 commit comments