File tree Expand file tree Collapse file tree 3 files changed +49
-0
lines changed Expand file tree Collapse file tree 3 files changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ const twoSum = ( nums , target ) => {
2
+ const prevNums = { } ; // 存放出现过的数字,和对应的索引
3
+ for ( let i = 0 ; i < nums . length ; i ++ ) { // 遍历每一项
4
+ const curNum = nums [ i ] ; // 当前项
5
+ const targetNum = target - curNum ; // 希望从过去的数字中找到的呼应项
6
+ const targetNumIndex = prevNums [ targetNum ] ; // 在prevNums中找targetNum的索引
7
+ if ( targetNumIndex !== undefined ) { // 如果能找到
8
+ return [ targetNumIndex , i ] ; // 直接返回targetNumIndex和当前的i
9
+ } // 如果找不到,说明之前没出现过targetNum
10
+ prevNums [ curNum ] = i ; // 往prevNums存当前curNum和对应的i
11
+ }
12
+ }
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def minFlips (self , target ):
3
+ """
4
+ :type target: str
5
+ :rtype: int
6
+ """
7
+ i = 0
8
+ flag = 0
9
+ pre = None
10
+ res = 0
11
+ for i , ch in enumerate (target ):
12
+ if not flag and ch == "0" :
13
+ continue
14
+ else :
15
+ if ch != pre :
16
+ res += 1
17
+ flag = 1
18
+ pre = ch
19
+ return res
Original file line number Diff line number Diff line change
1
+ # Definition for singly-linked list.
2
+ # class ListNode(object):
3
+ # def __init__(self, x):
4
+ # self.val = x
5
+ # self.next = None
6
+ class Solution (object ):
7
+ def isPalindrome (self , head ):
8
+ """
9
+ :type head: ListNode
10
+ :rtype: bool
11
+ """
12
+ s = []
13
+ while head :
14
+ s .append (head .val )
15
+ head = head .next
16
+ return s == s [::- 1 ]
17
+
18
+
You can’t perform that action at this time.
0 commit comments