diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..bb38db8 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "C:\\ProgramData\\Anaconda2\\envs\\py3\\python.exe" +} \ No newline at end of file diff --git "a/0280.\346\221\206\345\212\250\346\216\222\345\272\217/0280-\346\221\206\345\212\250\346\216\222\345\272\217.py" "b/0280.\346\221\206\345\212\250\346\216\222\345\272\217/0280-\346\221\206\345\212\250\346\216\222\345\272\217.py" new file mode 100644 index 0000000..42a9b22 --- /dev/null +++ "b/0280.\346\221\206\345\212\250\346\216\222\345\272\217/0280-\346\221\206\345\212\250\346\216\222\345\272\217.py" @@ -0,0 +1,17 @@ +class Solution(object): + def wiggleSort(self, nums): + """ + :type nums: List[int] + :rtype: None Do not return anything, modify nums in-place instead. + """ + if not nums: + return nums + i = 0 + while i < len(nums): + if i % 2 == 0 and i < len(nums) - 1 and nums[i] > nums[i + 1]: + nums[i], nums[i + 1] = nums[i + 1], nums[i] + elif i % 2 and i < len(nums) - 1 and nums[i] < nums[i + 1]: + nums[i], nums[i + 1] = nums[i + 1], nums[i] + else: + i += 1 + return nums diff --git "a/0281.\351\224\257\351\275\277\350\277\255\344\273\243\345\231\250/0281-\351\224\257\351\275\277\350\277\255\344\273\243\345\231\250.py" "b/0281.\351\224\257\351\275\277\350\277\255\344\273\243\345\231\250/0281-\351\224\257\351\275\277\350\277\255\344\273\243\345\231\250.py" index 0ae455c..e70ddb8 100644 --- "a/0281.\351\224\257\351\275\277\350\277\255\344\273\243\345\231\250/0281-\351\224\257\351\275\277\350\277\255\344\273\243\345\231\250.py" +++ "b/0281.\351\224\257\351\275\277\350\277\255\344\273\243\345\231\250/0281-\351\224\257\351\275\277\350\277\255\344\273\243\345\231\250.py" @@ -6,35 +6,36 @@ def __init__(self, v1, v2): :type v1: List[int] :type v2: List[int] """ - self.list = [] - if v1 and v2: - i = 0 - for i in range(min(len(v1), len(v2))): - self.list.append(v1[i]) - self.list.append(v2[i]) - if v1[i + 1:]: - self.list += v1[i + 1:] - else: - self.list += v2[i + 1:] - - elif v1: - self.list = v1 - else: - self.list = v2 - self.index = 0 + self.i1, self.i2 = 0, 0 + self.v1, self.v2 = v1, v2 + self.indicator = 0 # 0 for v1, 1 for v2 + def next(self): """ :rtype: int """ - self.index += 1 - return self.list[self.index - 1] + if not self.indicator: + self.indicator = 1 + if self.i1 < len(self.v1): + self.i1 += 1 + return self.v1[self.i1 - 1] + else: + self.i2 += 1 + return self.v2[self.i2 - 1] + else: + self.indicator = 0 + if self.i2 < len(self.v2): + self.i2 += 1 + return self.v2[self.i2 - 1] + else: + self.i1 += 1 + return self.v1[self.i1 - 1] def hasNext(self): """ :rtype: bool """ - return self.index != len(self.list) - + return self.i1 < len(self.v1) or self.i2 < len(self.v2) # Your ZigzagIterator object will be instantiated and called as such: # i, v = ZigzagIterator(v1, v2), [] diff --git "a/0283.\347\247\273\345\212\250\351\233\266/0283-\347\247\273\345\212\250\351\233\266.py" "b/0283.\347\247\273\345\212\250\351\233\266/0283-\347\247\273\345\212\250\351\233\266.py" index 3dbbd55..897a7c7 100644 --- "a/0283.\347\247\273\345\212\250\351\233\266/0283-\347\247\273\345\212\250\351\233\266.py" +++ "b/0283.\347\247\273\345\212\250\351\233\266/0283-\347\247\273\345\212\250\351\233\266.py" @@ -2,15 +2,12 @@ class Solution(object): def moveZeroes(self, nums): """ :type nums: List[int] - :rtype: void Do not return anything, modify nums in-place instead. + :rtype: None Do not return anything, modify nums in-place instead. """ - l = len(nums) - for index,item in enumerate(nums): - if item != 0: - continue - else: - for index0 in range(index+1,l): - if nums[index0] != 0: - print nums[index], nums[index0] - nums[index], nums[index0] = nums[index0], nums[index] - break \ No newline at end of file + i = 0 + for j in range(len(nums)): + if nums[j] != 0: + nums[i], nums[j] = nums[j], nums[i] + i += 1 + + # return nums \ No newline at end of file diff --git "a/0284.\351\241\266\347\253\257\350\277\255\344\273\243\345\231\250/0284-\351\241\266\347\253\257\350\277\255\344\273\243\345\231\250.py" "b/0284.\351\241\266\347\253\257\350\277\255\344\273\243\345\231\250/0284-\351\241\266\347\253\257\350\277\255\344\273\243\345\231\250.py" index 4eb1e35..a29ee7e 100644 --- "a/0284.\351\241\266\347\253\257\350\277\255\344\273\243\345\231\250/0284-\351\241\266\347\253\257\350\277\255\344\273\243\345\231\250.py" +++ "b/0284.\351\241\266\347\253\257\350\277\255\344\273\243\345\231\250/0284-\351\241\266\347\253\257\350\277\255\344\273\243\345\231\250.py" @@ -25,32 +25,32 @@ def __init__(self, iterator): Initialize your data structure here. :type iterator: Iterator """ - self.list = list() - while(iterator.hasNext()): - self.list.append(iterator.next()) + self.l = [] + while iterator.hasNext(): + self.l.append(iterator.next()) + # self.l = iterator + self.index = 0 + def peek(self): """ Returns the next element in the iteration without advancing the iterator. :rtype: int """ - return self.list[0] - + return self.l[self.index] def next(self): """ :rtype: int """ - return self.list.pop(0) - - + self.index += 1 + return self.l[self.index - 1] def hasNext(self): """ :rtype: bool """ - return len(self.list) != 0 - + return self.index < len(self.l) # Your PeekingIterator object will be instantiated and called as such: # iter = PeekingIterator(Iterator(nums)) diff --git "a/0285.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\351\241\272\345\272\217\345\220\216\347\273\247/0285-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\351\241\272\345\272\217\345\220\216\347\273\247.py" "b/0285.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\351\241\272\345\272\217\345\220\216\347\273\247/0285-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\351\241\272\345\272\217\345\220\216\347\273\247.py" new file mode 100644 index 0000000..fbca755 --- /dev/null +++ "b/0285.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\351\241\272\345\272\217\345\220\216\347\273\247/0285-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\351\241\272\345\272\217\345\220\216\347\273\247.py" @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def inorderSuccessor(self, root, p): + """ + :type root: TreeNode + :type p: TreeNode + :rtype: TreeNode + """ + if not root: + return None + def inorder(node): + if not node: + return [] + return inorder(node.left) + [node] + inorder(node.right) + l = inorder(root) + i = l.index(p) + return l[i + 1] if i < len(l) - 1 else None \ No newline at end of file