leetcode个人题解——#31 Next Permutation
写这题时脑子比较混乱,重写了一遍wiki大佬的解法。
算法:
According to Wikipedia, a man named Narayana Pandita presented the following simple algorithm to solve this problem in the 14th century.
- Find the largest index
ksuch thatnums[k] < nums[k + 1]. If no such index exists, just reversenumsand done. - Find the largest index
l > ksuch thatnums[k] < nums[l]. - Swap
nums[k]andnums[l]. - Reverse the sub-array
nums[k + 1:]
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int max;
int min;
for(max = nums.size() - ; max >= ; max--)
if(nums[max] < nums[max+])
break;
if(max < ){
reverse(nums.begin(),nums.end());
return;
}
else
for(min = nums.size() - ; min > ; min--)
if(nums[min] > nums[max])
break;
swap(nums[min], nums[max]);
reverse(nums.begin() + max + , nums.end());
}
};
leetcode个人题解——#31 Next Permutation的更多相关文章
- <LeetCode OJ> 31. Next Permutation
31. Next Permutation Total Accepted: 54346 Total Submissions: 212155 Difficulty: Medium Implement ne ...
- [Leetcode][Python]31: Next Permutation
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 31: Next Permutationhttps://oj.leetcode ...
- [array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- LeetCode - 31. Next Permutation
31. Next Permutation Problem's Link ---------------------------------------------------------------- ...
- leetcode 31. Next Permutation (下一个排列,模拟,二分查找)
题目链接 31. Next Permutation 题意 给定一段排列,输出其升序相邻的下一段排列.比如[1,3,2]的下一段排列为[2,1,3]. 注意排列呈环形,即[3,2,1]的下一段排列为[1 ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- Leetcode 简略题解 - 共567题
Leetcode 简略题解 - 共567题 写在开头:我作为一个老实人,一向非常反感骗赞.收智商税两种行为.前几天看到不止两三位用户说自己辛苦写了干货,结果收藏数是点赞数的三倍有余,感觉自己的 ...
随机推荐
- Python装饰器高级用法
在Python中,装饰器一般用来修饰函数,实现公共功能,达到代码复用的目的.在函数定义前加上@xxxx,然后函数就注入了某些行为,很神奇!然而,这只是语法糖而已. 场景 假设,有一些工作函数,用来对数 ...
- JDBC—执行sql语句的通用方法
/* * 执行 sql的方法集 * delete,insert into ,update */ public static void update(String sql){ Connection co ...
- java核心技术-多线程之基本使用
多线程程序好处就是可以提高cpu使用率和系统的性能.这里举个例子,民以食为天,咱们以餐馆为例(后面基本上都用餐馆作为对象),后面如果没有特殊说明均采用本节相关术语,围绕餐馆我们可以抽象出如下几个角色以 ...
- Model验证简单易懂
public bool UserSex { get; set; } //定义名字 [Display(Name = "年龄")] [Range(0, 150, ErrorMessag ...
- Ionic3项目实践记录
Ionic3首次项目实践记录 标签(空格分隔): Angular Ionic Ionic3踩坑 1. 路由懒加载(lazy load) 如果设置了懒加载,就必须全部懒加载(包括TabsPage),否则 ...
- chrome浏览器页面获取绑定返回顶部动画事件插件
在chrome浏览器下页面加载: var top = $("body").scrollTop() ; console.log(top) ...
- Jquery实现表单动态加减、ajax表单提交
一直在搞Java后台开发,记得刚工作那一两年时间搞过前后端交互开发,用的东西也是五花八门,当时觉得做页面展示给别人看,是很有成就的事情,不过现在感觉自己跟纯前端开发比起来弱爆了,不过如果你的目标是作为 ...
- windows系统,MongoDB开启用户验证登录的正确姿势
MongoDB默认安装并没有开启用户名密码登录,这样太不安全了,百度出来的开启验证登录的文章,对初次使用MongoDB的小白太不友好了,总结下经验,自己写一份指引. 1,我的安装路径是C:\Progr ...
- jdbc之存储过程的调用和调用方法
调用存储过程 调用存储过程的sql语句 {call 过程名称(参数列表)} conn = DbUtils.getConnection(); sql = "{call p_order_appr ...
- OpenWrt-Git依赖报错
前言 在Ubuntu中搭建openwrt编译环境时,安装完了需要的软件之后,我们执行命令检查依赖是否满足, make defconfig 有时会出现以下警告: ihid@ubuntu:~/chaos_ ...