LeetCode Find Permutation
原题链接在这里:https://leetcode.com/problems/find-permutation/description/
题目:
By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decreasing relationship between two numbers, 'I' represents an increasing relationship between two numbers. And our secret signature was constructed by a special integer array, which contains uniquely all the different number from 1 to n (n is the length of the secret signature plus 1). For example, the secret signature "DI" can be constructed by array [2,1,3] or [3,1,2], but won't be constructed by array [3,2,4] or [2,1,3,4], which are both illegal constructing special string that can't represent the "DI" secret signature.
On the other hand, now your job is to find the lexicographically smallest permutation of [1, 2, ... n] could refer to the given secret signature in the input.
Example 1:
Input: "I"
Output: [1,2]
Explanation: [1,2] is the only legal initial spectial string can construct secret signature "I", where the number 1 and 2 construct an increasing relationship.
Example 2:
Input: "DI"
Output: [2,1,3]
Explanation: Both [2,1,3] and [3,1,2] can construct the secret signature "DI",
but since we want to find the one with the smallest lexicographical permutation, you need to output [2,1,3]
Note:
- The input string will only contain the character 'D' and 'I'.
- The length of input string is a positive integer and will not exceed 10,000
题解:
先生成sorted array. 若出现连续的D时就把连续D开始和结尾对应的这段reverse.
Time Complexity: O(n). n = s.length().
Space: O(1). regardless res.
AC Java:
class Solution {
public int[] findPermutation(String s) {
int len = s.length();
int [] res = new int[len+1];
for(int i = 0; i<len+1; i++){
res[i] = i+1;
} for(int i = 0; i<len; i++){
if(s.charAt(i) == 'D'){
int mark = i;
while(i<len && s.charAt(i)=='D'){
i++;
}
reverse(res, mark, i);
}
} return res;
} private void reverse(int [] res, int i, int j){
while(i<j){
swap(res, i++, j--);
}
} private void swap(int [] res, int i, int j){
int temp = res[i];
res[i] = res[j];
res[j] = temp;
}
}
LeetCode Find Permutation的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode Palindrome Permutation II
原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...
- LeetCode Palindrome Permutation
原题链接在这里:https://leetcode.com/problems/palindrome-permutation/ 题目: Given a string, determine if a per ...
- Java for LeetCode 060 Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Find Permutation 找全排列
By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decre ...
- [leetcode]Next Permutation @ Python
原题地址:https://oj.leetcode.com/problems/next-permutation/ 题意: Implement next permutation, which rearra ...
随机推荐
- 【转】TCP那些事(上,下)
TCP是一个巨复杂的协议,因为他要解决很多问题,而这些问题又带出了很多子问题和阴暗面.所以学习TCP本身是个比较痛苦的过程,但对于学习的过程却能让人有很多收获.关于TCP这个协议的细节,我还是推荐你去 ...
- 在Eclipse中快速添加main方法
方法一: 在创建类时自动添加,勾选“public static void main(String[] args)” 方法二: 输入main之后按"alt+/"组合键,选择如图所 ...
- php-fpm: 某项目网站频繁出现503问题解决( WARNING: [pool www] server reached pm.max_children setting (50), consider raising it)
服务是nginx+php-fpm配置, 在运行过一段时间后,会经常出现: WARNING: [pool www] server reached pm.max_children setting (50) ...
- 转:MyEclipse安装Eclipse Memory Analyzer插件,并进行错误文件分析流程
转自 http://www.cnblogs.com/nb44c/p/5218880.html 1.先安装MAT插件 Memory Analyzer 插件下载地址:http://www.eclipse. ...
- HDU 5651 组合+逆元
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5651 题目意思我看了半天没读懂,一直以为是回文子串又没看见substring的单词最后看博客才知道是用给 ...
- LeetCode OJ:Number of 1 Bits(比特1的位数)
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- 正则表达式 preg_match 匹配中文
preg_match 匹配中文出错 2010年01月06日 星期三 14:55 错误提示: Warning: preg_match() [function.preg-match]: Compilati ...
- 安装spring报错:Cannot complete the install because of a conflicting dependency.
问题: 在Eclipse里安装Spring插件,help->install new software用端点安装,说是出现软件依赖错误报错如下: Cannot complete the insta ...
- Linux:col命令详解
col 经常用于将说明文件转存为纯文本以方便阅读 语法 col(选项) 选项 -b:过滤掉所有的控制字符,包括RLF和HRLF: -f:滤掉RLF字符,但允许将HRLF字符呈现出来: -x:以多个空格 ...
- vue--踩坑
1.通过computed计算属性,计算过的值,假如传递给子组件,在子组件中修改是不起作用的.