Skip to content

Commit 33c0d5b

Browse files
committed
add Array/next-permutation.py (dev)
1 parent c7564c2 commit 33c0d5b

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@
8787
016 | [3 Sum Closest](https://leetcode.com/problems/3sum-closest/) | [Python](https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Array/3sum-closest.py) | _O(n^2)_ | _O(1)_ | Medium |`Two Pointers`, `basic`, `trick`| AGAIN**
8888
018| [4 Sum](https://leetcode.com/problems/4sum/) | [Python](https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Array/4sum.py) | _O(n^3)_ | _O(1)_ | Medium | `k sum`, `Two Pointers`, `trick`, `hard` | AGAIN (not start)
8989
026 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [Python](https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Array/remove-duplicates-from-sorted-array.py) | _O(n)_ | _O(1)_ | Easy | `Two Pointers`, `basic`, `trick` | AGAIN*
90-
027 | [Remove Element](https://leetcode.com/problems/remove-element/) |[Python](https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Array/remove-element.py) | _O(n)_ | _O(1)_ | Easy |`basic`| AGAIN*
91-
031 | [Next Permutation](https://leetcode.com/problems/next-permutation/)| [Python](./Python/next-permutation.py) | _O(n)_ | _O(1)_ | Medium | Tricky |
90+
027 | [Remove Element](https://leetcode.com/problems/remove-element/) | [Python](https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Array/remove-element.py) | _O(n)_ | _O(1)_ | Easy |`basic`| AGAIN*
91+
031 | [Next Permutation](https://leetcode.com/problems/next-permutation/)| [Python](https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Array/next-permutation.py) | _O(n)_ | _O(1)_ | Medium | `trick`, `hard` | AGAIN (not start*)
9292
048 | [Rotate Image](https://leetcode.com/problems/rotate-image/) | [Python](./Python/rotate-image.py) | _O(n^2)_ | _O(1)_ | Medium ||
9393
054 | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [Python](./Python/spiral-matrix.py) | _O(m * n)_ | _O(1)_ | Medium ||
9494
059 | [Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/) | [Python](./Python/spiral-matrix-ii.py) | _O(n^2)_ | _O(1)_ | Medium ||
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# V0
2+
3+
# V1
4+
# http://bookshadow.com/weblog/2016/09/09/leetcode-next-permutation/
5+
class Solution(object):
6+
def nextPermutation(self, nums):
7+
"""
8+
:type nums: List[int]
9+
:rtype: void Do not return anything, modify nums in-place instead.
10+
"""
11+
size = len(nums)
12+
for x in range(size - 1, -1, -1):
13+
if nums[x - 1] < nums[x]:
14+
break
15+
if x > 0:
16+
for y in range(size - 1, -1, -1):
17+
if nums[y] > nums[x - 1]:
18+
nums[x - 1], nums[y] = nums[y], nums[x - 1]
19+
break
20+
for z in range((size - x) / 2):
21+
nums[x + z], nums[size - z - 1] = nums[size - z - 1], nums[x + z]
22+
23+
# V1'
24+
# https://blog.csdn.net/fuxuemingzhu/article/details/82113409
25+
class Solution(object):
26+
def nextPermutation(self, nums):
27+
"""
28+
:type nums: List[int]
29+
:rtype: void Do not return anything, modify nums in-place instead.
30+
"""
31+
n = len(nums)
32+
i = n - 1
33+
while i > 0 and nums[i] <= nums[i - 1]:
34+
i -= 1
35+
self.reverse(nums, i, n - 1)
36+
if i > 0:
37+
for j in range(i, n):
38+
if nums[j] > nums[i-1]:
39+
self.swap(nums, i-1, j)
40+
break
41+
42+
def reverse(self, nums, i, j):
43+
"""
44+
contains i and j.
45+
"""
46+
for k in range(i, (i + j) / 2 + 1):
47+
self.swap(nums, k, i + j - k)
48+
49+
50+
def swap(self, nums, i, j):
51+
"""
52+
contains i and j.
53+
"""
54+
nums[i], nums[j] = nums[j], nums[i]
55+
56+
# V2
57+
# Time: O(n)
58+
# Space: O(1)
59+
class Solution(object):
60+
# @param {integer[]} nums
61+
# @return {void} Do not return anything, modify nums in-place instead.
62+
def nextPermutation(self, num):
63+
k, l = -1, 0
64+
for i in range(len(num) - 1):
65+
if num[i] < num[i + 1]:
66+
k = i
67+
68+
if k == -1:
69+
num.reverse()
70+
return
71+
72+
for i in range(k + 1, len(num)):
73+
if num[i] > num[k]:
74+
l = i
75+
num[k], num[l] = num[l], num[k]
76+
num[k + 1:] = num[:k:-1]

0 commit comments

Comments
 (0)