[leetcode]31. Next Permutation -Find next greater number with same set of digits
2016-09-04 21:10
489 查看
Find next greater number with same set of digits
Given a number n, find the smallest number that has same set of digits as n and is greater than n. If x is the greatest possible number with its set of digits, then print “not possible”.Examples:For simplicity of implementation, we have considered input number as a string.Input: n = "218765"Output: "251678"Input: n = "1234"Output: "1243"Input: n = "4321"Output: "Not Possible"Input: n = "534976"Output: "536479"Following are few observations about the next greater number.1) If all digits sorted in descending order, then output is always “Not Possible”. For example, 4321.2) If all digits are sorted in ascending order, then we need to swap last two digits. For example, 1234.3) For other cases, we need to process the number from rightmost side (why? because we need to find the smallest of all greater numbers)You can now try developing an algorithm yourself.Following is the algorithm for finding the next greater number.I) Traverse the given number from rightmost digit, keep traversing till you find a digit which is smaller than the previously traversed digit. For example, if the input numberis “534976”, we stop at 4 because 4 is smaller than next digit 9. If we do not find such a digit, then output is “Not Possible”.II) Now search the right side of above found digit ‘d’ for the smallest digit greater than ‘d’. For “534976″,the right side of 4 contains “976”. The smallest digit greater than 4 is 6.III) Swap the above found two digits, we get 536974 inabove example.IV) Now sort all digits from position next to ‘d’ to the end of number. The number that we get after sorting is the output. For above example, we sort digits in bold 536974.We get “536479” which is the next greater number for input 534976.Following is C++ implementation of above approach.
Next number with same set of digits is 536479The above implementation can be optimized in following ways.1) We can use binary search in step II instead of linear search.2) In step IV, instead of doing simple sort, we can apply some clever technique to do it in linear time. Hint: We know that all digits are linearly sorted in reverse order except one digit which was swapped.With above optimizations, we can say that the time complexity of this method is O(n).This article is contributed by Rahul Jain. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above/**全排列的下一个值* 123* 132* 213* 231* 312* 321* 则654 8751的递增位置为4,然后找到那个比4大的值5交换 655 8741,然后重排列8741 得到6551478* 首先 交换1和4 651 8754**如果没有找到递增位置,则说明此序列递减,将其变成递增*/class Solution {public:void nextPermutation(vector<int>& nums){int n=nums.size();int index=n-1;for(int i=n-1;i>0;i--)//找到最后一个递增位置{if(nums[i]<=nums[i-1])index--;elsebreak;}if (index==0)//若全为降序,则逆置全部{int start=index;int end=n-1;while(start<end){swap(nums[start],nums[end]);start++;end--;}}else{index--;for(int i=n-1;i>index;i--)//交换{if(nums[i]>nums[index]){swap(nums[i],nums[index]);break;}}int start=index+1;int end=n-1;while(start<end)//逆置{swap(nums[start],nums[end]);start++;end--;}}}};
相关文章推荐
- Find next greater number with same set of digits
- [LeetCode] Next Permutation - Next bigger number with the same set of digits
- Next Number with Same Set of Digits
- Next higher number with same number of set bits
- (CareerCup)Find next higher number with same digits
- [面试题] Find next higher number with same digits
- Given a number,find the next higher number using the same digits in the number.
- Kettle解析JSON错误,We MUST have the same number of values for all paths,We can not find and data with path [$.
- Leetcode--->Given n points on a 2D plane, find the maximum number of points that lie on the same str
- [LeetCode] Search for a Range (sorted integers array,find start & end position of a target number)
- Find number of subsets with equal sum
- 【LeetCode with Python】 Letter Combinations of a Phone Number
- Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
- rsync - find - perl - Super fast delete a folder with large number of files
- LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number
- Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
- [LeetCode] Number of Subarrays with Bounded Maximum 有界限最大值的子数组数量
- 题目描述 Given n points on a 2D plane, find the maximum number of points that lie on the same straight
- LeetCode之Prime Number of Set Bits in Binary Representation(Kotlin)
- The importance of tuning APC for sites with a high number of Drupal modules[转载]