leetcode31. Next Permutation
来源:互联网 发布:电脑免费发信息软件 编辑:程序博客网 时间:2024/05/29 17:45
leetcode31. Next Permutation
class Solution {public: void nextPermutation(vector<int>& nums) { int right = nums.size() - 1; if(right<=0) return ; int max = INT_MIN; int maxPos = right; int newPos = 0; int temp = 0; while(right>=0){ if(max>nums[right]){ newPos = floor(binarySearchPos(nums,nums[right],maxPos,nums.size() - 1)); //cout<<newPos<<endl; while(nums[newPos] == nums[right]) newPos++; temp = nums[right]; nums[right] = nums[newPos]; nums[newPos] = temp; quickSort(nums,right+1,nums.size() - 1); break; }else{ //cout<<max<<";"<<nums[right]<<endl; max=nums[right]; maxPos = right; right--; } } if(right<0){ quickSort(nums,0,nums.size() - 1); } // for(int i=0;i<nums.size();i++){ // cout<<nums[i]<<","; // } } double binarySearchPos(vector<int>& nums1, double num, int start, int end) { int left = start, right = end, medium = 0; while (left<right) { medium = left + (right - left) / 2; if (nums1[medium] - num <0.4) { right = medium; } else if (num - nums1[medium] <0.4) { left = medium + 1; } else { return medium; } } double left1; if (nums1[left] - num>0.4 && num - nums1[left] >0.4) left1 = left; else if (nums1[left] - num<0.4) { left1 = left - 0.5; } else { left1 = left + 0.5; } return left1; } void quickSort(vector<int>& nums,int left,int right){ int temp = 0; while(left<right){ temp = nums[left]; nums[left] = nums[right]; nums[right] = temp; left++; right--; } }};
阅读全文
0 0
- [LeetCode31]Next Permutation
- LeetCode31:Next Permutation
- Leetcode31 Next Permutation
- LeetCode31 Next Permutation
- leetcode31:Next Permutation
- leetcode31---Next Permutation
- Leetcode31 Next Permutation
- LeetCode31. Next Permutation
- leetcode31. Next Permutation
- leetcode31~Next Permutation
- LeetCode31. Next Permutation
- Leetcode31. Next Permutation
- leetcode31. Next Permutation
- LeetCode31. Next Permutation
- leetcode31 Next Permutation
- Leetcode31 Next Permutation
- leetcode31. Next Permutation
- LeetCode31. Next Permutation最清晰的解法
- linux 下安装 pip,linux下安装Mysql-Python
- mysql jeesite 大量数据 count 问题
- 关于JAVA程序中的菱形打印问题
- gpio子系统和pinctrl子系统(上)
- 《TCP/IP详解 卷1》 笔记: 概述
- leetcode31. Next Permutation
- 前端项目练习01-瀑布流练习
- 解决VirtualBox虚拟CentOS7时,安装增强功能提示linux
- 求出1到10、20到30和35到45的三个和
- 一条Select语句引发的反思
- leetcode38. Count and Say
- android java javax发送邮件
- 高精度/微秒级线程的实现
- SGU 416 Optimal Dartboard(脑洞YY乱搞)