当前位置: 首页 > news >正文

LeetCode -- Next Permutation

题目描述:


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


给定一个序列,找到下一个排序。


思路:


将index, index2 = -1;
1. 从右向左找,如果不满足 num[i+1] < num [i],index = i;
2. 再从右向左找到第一个num[i] > num[index] 的数,index 2 = i;
3. 交换num[index]和num[index2]
4. 将num[index+1... n] 的数逆置。


实现代码:





public class Solution {
    public void NextPermutation(int[] nums) {
        var index = -1;
    	for(var i = nums.Length - 1; i > 0; i--){
    		if(nums[i] > nums[i-1]){
    			index = i-1;
    			break;
    		}
    	}
    	
    	var index2 = -1;
    	if(index != -1){
    		for(var i = nums.Length - 1; i >= 0; i--){
    			if(nums[i] > nums[index]){
    				index2 = i;
    				break;
    			}
    		}
    	}
    	
    	
    	if(index != -1 && index2 != -1){
    		var t = nums[index];
    		nums[index] = nums[index2];
    		nums[index2] = t;
    	}
    	
    	var k = index + 1;
    	var j = nums.Length - 1;
    	
    	while(k < j){
    		var tmp = nums[k];
    		nums[k] = nums[j];
    		nums[j] = tmp;
    		
    		k++;
    		j--;
    	}
    }
}


相关文章:

  • [Real world Haskell] 中文翻译:第三章 定义类型,流式函数
  • LeetCode -- Search Matrix
  • LeetCode -- Sort Colors
  • 理解HTTP消息头
  • LeetCode -- Convert SortedList To BST
  • HTTP协议返回状态码表
  • LeetCode -- Insert Interval
  • 推荐WPF的好书
  • LeetCode -- Longest Valid Parentheses
  • 利用Intel博锐技术解决桌面管理难题
  • LeetCode -- Permutations
  • LeetCode -- Construct Binary Tree from Inorder and Postorder Traversal
  • 王小云:十年破译五部顶级密码
  • LeetCode -- Factorial Trailing Zeroes
  • LeetCode -- Gas Station
  • JavaScript-如何实现克隆(clone)函数
  • gitlab-ci配置详解(一)
  • IDEA常用插件整理
  • iOS帅气加载动画、通知视图、红包助手、引导页、导航栏、朋友圈、小游戏等效果源码...
  • SpiderData 2019年2月25日 DApp数据排行榜
  • spring + angular 实现导出excel
  • win10下安装mysql5.7
  • 搭建gitbook 和 访问权限认证
  • 基于axios的vue插件,让http请求更简单
  • 技术:超级实用的电脑小技巧
  • 每天10道Java面试题,跟我走,offer有!
  • 使用 Xcode 的 Target 区分开发和生产环境
  • 微信小程序:实现悬浮返回和分享按钮
  • 我是如何设计 Upload 上传组件的
  • 延迟脚本的方式
  • 异步
  • 译米田引理
  • 白色的风信子
  • 1.Ext JS 建立web开发工程
  • 带你开发类似Pokemon Go的AR游戏
  • (C++20) consteval立即函数
  • (pojstep1.1.2)2654(直叙式模拟)
  • (vue)el-checkbox 实现展示区分 label 和 value(展示值与选中获取值需不同)
  • (非本人原创)史记·柴静列传(r4笔记第65天)
  • (机器学习-深度学习快速入门)第三章机器学习-第二节:机器学习模型之线性回归
  • (南京观海微电子)——COF介绍
  • (转)Sql Server 保留几位小数的两种做法
  • (转)微软牛津计划介绍——屌爆了的自然数据处理解决方案(人脸/语音识别,计算机视觉与语言理解)...
  • *Algs4-1.5.25随机网格的倍率测试-(未读懂题)
  • .NET 8.0 中有哪些新的变化?
  • .NET CLR Hosting 简介
  • .NET 编写一个可以异步等待循环中任何一个部分的 Awaiter
  • .net 中viewstate的原理和使用
  • .net 桌面开发 运行一阵子就自动关闭_聊城旋转门家用价格大约是多少,全自动旋转门,期待合作...
  • .net/c# memcached 获取所有缓存键(keys)
  • .NET开源全面方便的第三方登录组件集合 - MrHuo.OAuth
  • [C++]priority_queue的介绍及模拟实现
  • [C语言]——内存函数
  • [GXYCTF2019]禁止套娃
  • [Java] 图说 注解