File tree Expand file tree Collapse file tree 3 files changed +78
-0
lines changed
Expand file tree Collapse file tree 3 files changed +78
-0
lines changed Original file line number Diff line number Diff line change 1+ #
2+ # @lc app=leetcode.cn id=1991 lang=python3
3+ #
4+ # [1991] 找到数组的中间位置
5+ #
6+
7+ # @lc code=start
8+ class Solution :
9+ def findMiddleIndex (self , nums : List [int ]) -> int :
10+ s = sum (nums )
11+ r = 0
12+ for i in range (len (nums )):
13+ n = nums [i ]
14+ s -= n
15+ if r == s :
16+ return i
17+ r += n
18+ return - 1
19+ # @lc code=end
Original file line number Diff line number Diff line change 1+ #
2+ # @lc app=leetcode.cn id=724 lang=python3
3+ #
4+ # [724] 寻找数组的中心下标
5+ #
6+
7+ # @lc code=start
8+ class Solution :
9+ def pivotIndex (self , nums : List [int ]) -> int :
10+ s = sum (nums )
11+ r = 0
12+ for i in range (len (nums )):
13+ n = nums [i ]
14+ s -= n
15+ if r == s :
16+ return i
17+
18+ r += n
19+ return - 1
20+ # @lc code=end
Original file line number Diff line number Diff line change 1+ #
2+ # @lc app=leetcode.cn id=728 lang=python3
3+ #
4+ # [728] 自除数
5+ #
6+
7+ # @lc code=start
8+ class Solution :
9+
10+ numbers = set ()
11+
12+ def _selfDividingNumbers (self , n ):
13+ if n in Solution .numbers :
14+ return True
15+
16+ i = n % 10
17+ j = n // 10
18+
19+ while i or j :
20+ if i == 0 or n % i != 0 :
21+ return False
22+
23+ i = j % 10
24+ j = j // 10
25+
26+ Solution .numbers .add (n )
27+
28+ return True
29+
30+ def selfDividingNumbers (self , left : int , right : int ) -> List [int ]:
31+
32+ res = []
33+ for i in range (left , right + 1 ):
34+ if self ._selfDividingNumbers (i ):
35+ res .append (i )
36+
37+ return res
38+ # @lc code=end
39+
You can’t perform that action at this time.
0 commit comments