【LeetCode】31.Next Permutation(Medium)解题报告
2018-01-03 20:17
513 查看
【LeetCode】31.Next Permutation(Medium)解题报告
题目地址:https://leetcode.com/problems/next-permutation/description/
题目描述:
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place, do not allocate extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
可能有重复数字,输出全部排列。推荐解法一。面试常出现
Solution1:
Date:2018年1月3日
题目地址:https://leetcode.com/problems/next-permutation/description/
题目描述:
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place, do not allocate extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
可能有重复数字,输出全部排列。推荐解法一。面试常出现
Solution1:
//时间复杂度O(n) //空间复杂度O(1) /* 1 2 7 4 3 1 ^ 1 2 7 4 3 1 ^ 1 3 7 4 2 1 1 3 1 2 4 7 */ class Solution { public void nextPermutation(int[] nums) { if(nums==null || nums.length==0) return; int firstSmall = -1; for(int i=nums.length-2 ; i>=0 ; i--){ if(nums[i]<nums[i+1]){ firstSmall = i; break; } } if(firstSmall==-1){ reverse(nums,0,nums.length-1); return; } int firstLarge = -1; for(int i=nums.length-1 ; i>=0 ; i--){ if(nums[i]>nums[firstSmall]){ firstLarge = i; break; } } swap(nums,firstSmall,firstLarge); reverse(nums,firstSmall+1,nums.length-1); } public void swap(int[] nums,int i,int j){ int temp = nums[i]; nums[i++] = nums[j]; nums[j--] = temp; } public void reverse(int[] nums,int i,int j){ while(i<j){ swap(nums,i++,j--); } } }
Date:2018年1月3日
相关文章推荐
- 【LeetCode】382.Linked List Random Node(medium)解题报告
- 【LeetCode】216.Combination Sum III(Medium)解题报告
- 【LeetCode】449.Serialize and Deserialize BST(Medium)解题报告
- 【LeetCode】370.Range Addition(Medium)解题报告
- 【LeetCode】435.Non-overlapping Intervals(Medium)解题报告
- 【LeetCode】245.Shortest Word Distance III(Medium)(加锁题)解题报告
- 【LeetCode】228.Summary Ranges(Medium)解题报告
- 【LeetCode】236.Lowest Common Ancestor of a Binary Tree(Medium)解题报告
- LeetCode解题报告 108. Convert Sorted Array to Binary Search Tree [medium]
- 【LeetCode】96.Unique Binary Search Trees(Medium)解题报告
- 【LeetCode】419.Battleships in a Board(Medium)解题报告
- 【LeetCode】80.Remove Duplicates from Sorted Array II(Medium)解题报告
- 【LeetCode】698.Partition to K Equal Sum Subsets(Medium)解题报告
- 【LeetCode】230.Kth Smallest Element in a BST(Medium)解题报告
- LeetCode解题报告 452. Minimum Number of Arrows to Burst Balloons [medium]
- 【LeetCode】377.Combination Sum IV(Medium)解题报告
- 【LeetCode】95.Unique Binary Search Trees II(Medium)解题报告
- 【LeetCode】253.Meeting Rooms II(Medium)解题报告
- 【LeetCode】413.Arithmetic Slices(Medium)解题报告
- [leetcode] 31. Next Permutation 解题报告