原题链接在这里: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. python调用虹软2.0第二版

    第一版踩了无数的坑,终于第二版把坑全添了,这次更新可以正常获取人脸数,角度,代码可读性更高,继续更新中 第三版已发出 https://www.cnblogs.com/wxt51/p/10125460. ...

  2. mock数据

    作为前端经常需要模拟后台数据,我们称之为mock.通常的方式为自己搭建一个服务器,返回我们想要的数据. 在这里我们使用node.js来实现 http://www.cnblogs.com/bsn-hua ...

  3. python爬虫之下载京东页面图片

    import requests from bs4 import BeautifulSoup import time import re t = 0 #用于给图片命名 for i in range(10 ...

  4. Mysql(基础篇)

    linux下的mysql操作 1.# 打开 MySQL 服务 sudo service mysql start 2.#使用 root 用户登录,密码为空 mysql -u root 3.创建数据库 C ...

  5. 安装 python 数据分析插件 pandas

    一上午试验了各种方法,发现利用pycharm是最快的.可以抛弃版本,命令和兼容问题的烦恼.纯粹傻瓜式 方法是 pycharm, 直接在settings里面,搜索pandas,添加即可,他会把所有之前需 ...

  6. DataTable 操作

    public void CreateTable() { //创建表 DataTable dt = new DataTable(); //1.添加列 dt.Columns.Add("Name& ...

  7. SSM的Maven项目搭建过程

    POM文件 父项目管理jar包,pom <modelVersion>4.0.0</modelVersion> <groupId>cn.e3mall</grou ...

  8. day38 爬虫之Scrapy + Flask框架

    s1617day3 内容回顾: Scrapy - 创建project - 创建爬虫 - 编写 - 类 - start_urls = ['http://www.xxx.com'] - def parse ...

  9. 弄懂flex布局

    目前在不考虑IE以及低端安卓机(4.3-)的兼容下,已经可以放心使用flex进行布局了.什么是flex布局以及它的好处,这里就不再赘述. 在这篇文章里,想说说flex布局的属性语法及其细节.那么网上也 ...

  10. jquery基础 笔记三

    一. 操作"DOM属性" 在jQuery中没有包装操作"DOM属性"的函数, 因为使用javascript获取和设置"DOM属性"都很简单. ...