Skip to content

Commit

Permalink
add Array/next-permutation.py (dev)
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Aug 9, 2019
1 parent c7564c2 commit 33c0d5b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@
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**
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)
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*
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*
031 | [Next Permutation](https://leetcode.com/problems/next-permutation/)| [Python](./Python/next-permutation.py) | _O(n)_ | _O(1)_ | Medium | Tricky |
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*
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*)
048 | [Rotate Image](https://leetcode.com/problems/rotate-image/) | [Python](./Python/rotate-image.py) | _O(n^2)_ | _O(1)_ | Medium ||
054 | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [Python](./Python/spiral-matrix.py) | _O(m * n)_ | _O(1)_ | Medium ||
059 | [Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/) | [Python](./Python/spiral-matrix-ii.py) | _O(n^2)_ | _O(1)_ | Medium ||
Expand Down
76 changes: 76 additions & 0 deletions leetcode_python/Array/next-permutation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# V0

# V1
# http://bookshadow.com/weblog/2016/09/09/leetcode-next-permutation/
class Solution(object):
def nextPermutation(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
size = len(nums)
for x in range(size - 1, -1, -1):
if nums[x - 1] < nums[x]:
break
if x > 0:
for y in range(size - 1, -1, -1):
if nums[y] > nums[x - 1]:
nums[x - 1], nums[y] = nums[y], nums[x - 1]
break
for z in range((size - x) / 2):
nums[x + z], nums[size - z - 1] = nums[size - z - 1], nums[x + z]

# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/82113409
class Solution(object):
def nextPermutation(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
n = len(nums)
i = n - 1
while i > 0 and nums[i] <= nums[i - 1]:
i -= 1
self.reverse(nums, i, n - 1)
if i > 0:
for j in range(i, n):
if nums[j] > nums[i-1]:
self.swap(nums, i-1, j)
break

def reverse(self, nums, i, j):
"""
contains i and j.
"""
for k in range(i, (i + j) / 2 + 1):
self.swap(nums, k, i + j - k)


def swap(self, nums, i, j):
"""
contains i and j.
"""
nums[i], nums[j] = nums[j], nums[i]

# V2
# Time: O(n)
# Space: O(1)
class Solution(object):
# @param {integer[]} nums
# @return {void} Do not return anything, modify nums in-place instead.
def nextPermutation(self, num):
k, l = -1, 0
for i in range(len(num) - 1):
if num[i] < num[i + 1]:
k = i

if k == -1:
num.reverse()
return

for i in range(k + 1, len(num)):
if num[i] > num[k]:
l = i
num[k], num[l] = num[l], num[k]
num[k + 1:] = num[:k:-1]

0 comments on commit 33c0d5b

Please sign in to comment.