Leetcode: Find Permutation(Unsolve lock problem)
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
还没研究:
For example, given IDIIDD we start with sorted sequence 1234567
Then for each k continuous D starting at index i we need to reverse [i, i+k] portion of the sorted sequence.
IDIIDD
1234567 // sorted
1324765 // answer
public class Solution {
public int[] findPermutation(String s) {
int n = s.length(), arr[] = new int[n + 1];
for (int i = 0; i <= n; i++) arr[i] = i + 1; // sorted
for (int h = 0; h < n; h++) {
if (s.charAt(h) == 'D') {
int l = h;
while (h < n && s.charAt(h) == 'D') h++;
reverse(arr, l, h);
}
}
return arr;
}
void reverse(int[] arr, int l, int h) {
while (l < h) {
arr[l] ^= arr[h];
arr[h] ^= arr[l];
arr[l] ^= arr[h];
l++; h--;
}
}
}
Leetcode: Find Permutation(Unsolve lock problem)的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- Leetcode: The Maze III(Unsolved Lock Problem)
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
- [LeetCode] 752. Open the Lock 开锁
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', ...
- [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 ...
随机推荐
- Windows 7 下使用 pandoc 转换文档格式
工作中我们经常需要面对各种各样的文档格式,文档格式转换也就在所难免.通常有些文档编辑工具会提供自带的格式转换功能,但可转换格式比较有限.pandoc 正好可以解决这个问题,几乎你能见到的所有文档格式都 ...
- 那些年我们跳过的 IE坑
一, IE input X 去掉文本框的叉叉和密码输入框的眼睛图标 解: 从IE 10开始,type=”text” 的 input 在用户输入内容后,会自动产生一个小叉叉(X),方便用户点击清 ...
- Tornado-Secure cookie and Session
这一节涉及的内容有: 1.客户端和服务端操作cookie的方法 2.secure cookie的机制 3.基本/自定义session 文件结构 三个启动文件由下往上对应的分别是三种服务端:使用secu ...
- prometheus + grafana + node_exporter + alertmanager 的安装部署与邮件报警 (一)
大家一定要先看详细的理论教程,再开始搭建,这样报错后才容易找到突破口 参考文档 https://www.cnblogs.com/afterdawn/p/9020129.html https://www ...
- 什么是Hash?Hash有哪些特性?
Hash 把任意长度的输入通过散列算法变换成固定长度的输出 Hash的特性: 输入域无穷,输出域有限.例如:有无穷多个(在工程中可以具体到多少个,例如1000)输入参数经过hash函数映射后得到有限的 ...
- swust oj 983
利用二叉树中序及后序遍历确定该二叉树的先序序列 1000(ms) 10000(kb) 2606 / 4908 已知二叉树的中序和先序遍历可以唯一确定后序遍历.已知中序和后序遍历可以唯一确定先序遍历,但 ...
- 安卓动态分析工具【Android】3D布局分析工具
https://blog.csdn.net/fancylovejava/article/details/45787729 https://blog.csdn.net/dunqiangjiaodemog ...
- 用JS编写个人所得税计算器
编写 “个人所得税计算器”函数 计算个税的方法: 3500 以下免征 3500 ~ 5000 部分 缴纳 3% 5000 ~ 9000 部分 缴纳 10% 9000 以上部分 缴纳 20% 代码如下: ...
- 初识Restful架构
1.对Rest(Restful)的理解 理解RESTful架构 怎样用通俗的语言解释REST,以及RESTful 维基百科:Representational state transfer 2.Rest ...
- nmcli命令使用以及网卡绑定bond
一.nmcli命令 1.什么是nmcli 以前我配置网卡的时候都要打vim /etc/sysconfig/network-scripts/ifcfg-eth0这么一长串命令,有很多配置名字还记不住就需 ...