【LeetCode】C# 31、Next Permutation

0 阅读 作者:qq_39643935 2017-10-18

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

也就是重新排列数组,使数组变成比原数大的最小的情况。
如果无法再大,就返回最小值。

思路:要实现上述条件
这里写图片描述
主要是思路不好想到。参考自http://www.cnblogs.com/etcow/archive/2012/10/02/2710083.html

public class Solution {
    public void NextPermutation(int[] nums) {
        if (nums.Length <= 1) return;
            for (int i = nums.Length - 2; i >= 0; i--)
            {
                if (nums[i+1] > nums[i])
                {
                    //i++;
                    for (int j = i+1; j < nums.Length; j++)
                    {
                        if (nums[j] <= nums[i])
                        {
                            if (j == i + 1)
                            {
                                int e = nums[nums.Length - 1];
                                nums[nums.Length - 1] = nums[i];
                                nums[i] = e;
                                return;
                            }
                            int n = nums[j - 1];
                            nums[j - 1] = nums[i];
                            nums[i] = n;
                            for (int k = i + 1, l = nums.Length - 1; k <= (nums.Length - i - 1) / 2 + i; k++, l--)
                            {
                                int t = nums[k];
                                nums[k] = nums[l];
                                nums[l] = t;
                            }
                            foreach (int item in nums)
                                Console.WriteLine(item);
                            return;
                        }
                    }
                    int temp = nums[nums.Length - 1];
                    nums[nums.Length - 1] = nums[i];
                    nums[i] = temp;
                    for (int k = i + 1, l = nums.Length - 1; k <= (nums.Length - i - 1) / 2 + i; k++, l--)
                    {
                        temp = nums[k];
                        nums[k] = nums[l];
                        nums[l] = temp;
                    }
                    foreach (int item in nums)
                        Console.WriteLine(item);
                    return;
                }
            }
            for (int i = 0,j = nums.Length-1;i< nums.Length/2; i++,j--)
            {
                int m = nums[i];
                nums[i] = nums[j];
                nums[j] = m;
            }
    }
}
原文地址:http://blog.csdn.net/qq_39643935/article/details/78198923
广告一下
热门教程
PHP7报A non well formed numeric value encountered 0
Linux系统下关闭mongodb的几种命令分享 0
mongodb删除数据、删除集合、删除数据库的命令 0
Git&Github极速入门与攻坚实战课程 0
python爬虫教程使用Django和scrapy实现 0
libnetsnmpmibs.so.31: cannot open shared object file 0
数据结构和算法视频教程 0
redis的hash结构怎么删除数据呢? 0
C++和LUA解析器的数据交互实战视频 0
mongodb errmsg" : "too many users are authenticated 0
C++基础入门视频教程 0
用30个小时精通C++视频教程可能吗? 0
C++分布式多线程游戏服务器开发视频教程socket tcp boost库 0
C++培训教程就业班教程 0
layui的util工具格式时间戳为字符串 0
C++实战教程之远程桌面远程控制实战 1
网络安全培训视频教程 0
LINUX_C++软件工程师视频教程高级项目实战 0
C++高级数据结构与算法视频教程 0
跨域问题很头疼?通过配置nginx轻松解决ajax跨域问题 0
相关文章
【译】JavaScript数据结构(3):单向链表与双向链表 16
10个JavaScript难点 16
【译】苹果拒绝支持PWA,有损Web的未来 16
iView 一周年了,同时发布了 2.0 正式版,但这只是开始... 16
nodejs+mongodb构建一个简单登录注册功能 16
【译】JavaScript数据结构(4):树 16
组件化开发与黑箱 16
TypeScript - 不止稳,而且快 16
webpack3+anujs+ReactCSSTransitionGroup 16
原生js实现图片放大镜效果 16
WEB缓存探究第二弹——实战 16
纯笔记:vfork 的一些使用场景(顺便讲一下 fork 的原理) 16
Android APP 内部捐赠实现(支付宝&amp;微信) 16
WKWebView 的一些小总结 16
模型评价(一) AUC大法 16
开始使用GraphQL 16
Webpack模块化原理简析 16
gulp使用问题记录 16
使用Angular4动画为页面添彩 16
Python27 Matplotlib (win64 python2.7) 安装及简单使用 16