-
Notifications
You must be signed in to change notification settings - Fork 17
/
next-permutation.py
66 lines (45 loc) · 1.92 KB
/
next-permutation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import unittest
class Solution:
def nextPermutation(self, nums):
left, right = 0, len(nums) - 1
for i in range(len(nums) - 1, 0, -1):
if nums[i - 1] < nums[i]:
smallest = i
for j in range(i + 1, len(nums)):
if nums[i - 1] < nums[j] <= nums[smallest]:
smallest = j
nums[smallest], nums[i - 1] = nums[i - 1], nums[smallest]
left, right = i, len(nums) - 1
break
while left < right:
nums[left], nums[right] = nums[right], nums[left]
left += 1
right -= 1
return nums
class TestSolution(unittest.TestCase):
def setUp(self):
self.sol = Solution()
def test_empty(self):
self.assertListEqual(self.sol.nextPermutation([]), [])
def test_one(self):
self.assertListEqual(self.sol.nextPermutation([1]), [1])
def test_two_right(self):
self.assertListEqual(self.sol.nextPermutation([1, 2]), [2, 1])
def test_two_wrong(self):
self.assertListEqual(self.sol.nextPermutation([2, 1]), [1, 2])
def test_custom1(self):
self.assertListEqual(self.sol.nextPermutation([1,2,3]), [1,3,2])
def test_custom2(self):
self.assertListEqual(self.sol.nextPermutation([3,2,1]), [1,2,3])
def test_custom3(self):
self.assertListEqual(self.sol.nextPermutation([1,1,5]), [1,5,1])
def test_custom4(self):
self.assertListEqual(self.sol.nextPermutation([1,3,2]), [2,1,3])
def test_custom5(self):
self.assertListEqual(self.sol.nextPermutation([2,3,4,5,6,7,8,1,1,5]), [2,3,4,5,6,7,8,1,5,1])
def test_custom6(self):
self.assertListEqual(self.sol.nextPermutation([4,2,0,2,3,2,0]), [4,2,0,3,0,2,2])
def test_custom7(self):
self.assertListEqual(self.sol.nextPermutation([2,3,1,3,3]), [2,3,3,1,3])
if __name__ == "__main__":
unittest.main()