原题链接在这里: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的更多相关文章

  1. LeetCode:60. Permutation Sequence,n全排列的第k个子列

    LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...

  2. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  3. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  4. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  5. LeetCode Palindrome Permutation II

    原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...

  6. LeetCode Palindrome Permutation

    原题链接在这里:https://leetcode.com/problems/palindrome-permutation/ 题目: Given a string, determine if a per ...

  7. 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 ...

  8. [LeetCode] Find Permutation 找全排列

    By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decre ...

  9. [leetcode]Next Permutation @ Python

    原题地址:https://oj.leetcode.com/problems/next-permutation/ 题意: Implement next permutation, which rearra ...

随机推荐

  1. Android自定义view-CircleSeekbar

    自定义view练手,效果图如下:实现功能 可设置圆环颜色和线宽及触摸后的颜色和线宽    可设置圆环内圈显示的文本内容及字体大小.颜色    可设置触摸点的图片    可设置触摸的有效范围 源码git ...

  2. spark sql 窗口函数over partition by

    1.窗口函数需要使用hiveContext,故引入如下包 libraryDependencies += "org.apache.spark" %% "spark-core ...

  3. springmvc的@Validated/@Valid注解使用和BindingResult bindingResult

    关于@Valid和Validated的比较 @Valid是使用hibernate validation的时候使用 @Validated 是只用spring  Validator 校验机制使用 一:@V ...

  4. 转载:10+ handy and reusable jQuery code snippets

    源地址:http://www.catswhocode.com/blog/10-handy-and-reusable-jquery-code-snippets Smooth scrolling to t ...

  5. Spring事务源码分析总结

    Spring事务是我们日常工作中经常使用的一项技术,Spring提供了编程.注解.aop切面三种方式供我们使用Spring事务,其中编程式事务因为对代码入侵较大所以不被推荐使用,注解和aop切面的方式 ...

  6. 二十五 Python分布式爬虫打造搜索引擎Scrapy精讲—Requests请求和Response响应介绍

    Requests请求 Requests请求就是我们在爬虫文件写的Requests()方法,也就是提交一个请求地址,Requests请求是我们自定义的 Requests()方法提交一个请求 参数: ur ...

  7. 论Sava(),SaveOrUpdate(),Merge()区别

    一.Save(): 用于将一个临时对象转变为持久化对象,也就是将一个新的业务实体保存到数据库中:相当于jdbc的insert. <假如两个实体之间有关系(例如employee表和address表 ...

  8. 简明 Nginx Location Url 配置笔记

    基本配置 为了探究nginx的url配置规则,当然需要安装nginx.我使用了vagrant创建了一个虚拟环境的ubuntu,通过apt-get安装nginx.这样就不会污染mac的软件环境.通过vr ...

  9. MYSQL freedata 外联接

    主要是解决,不同生产系统里面,有不同的数据库. SQL 又不能夸系统查询表. 只能在一个系统里,可以跨不同的数据库查表. 所以会用映射 .FREEDATA 这种方式,这样A 系统 里的表更新之后,就可 ...

  10. 十五、dbms_space(分析段增长和空间的需求)

    1.概述 作用:用于分析段增长和空间的需求. 2.包的组成 1).unused_space作用:用于返回对象(表.索引.簇)的未用空间语法:dbms_space.unused_space(segmen ...