当前位置: 首页 > news >正文

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:

 1 class Solution {
 2     public int[] findPermutation(String s) {
 3         int len = s.length();
 4         int [] res = new int[len+1];
 5         for(int i = 0; i<len+1; i++){
 6             res[i] = i+1;
 7         }
 8         
 9         for(int i = 0; i<len; i++){
10             if(s.charAt(i) == 'D'){
11                 int mark = i;
12                 while(i<len && s.charAt(i)=='D'){
13                     i++;
14                 }
15                 reverse(res, mark, i);
16             }
17         }
18         
19         return res;
20     }
21     
22     private void reverse(int [] res, int i, int j){
23         while(i<j){
24             swap(res, i++, j--);
25         }
26     }
27     
28     private void swap(int [] res, int i, int j){
29         int temp = res[i];
30         res[i] = res[j];
31         res[j] = temp;
32     }
33 }

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/8300946.html

相关文章:

  • postgressql sql查询拼接多个字段为一个字段查询出来
  • 计算并发用户数的五种方法
  • 非刚性人脸跟踪 —— 面部特征检测器
  • 【编程珠玑】【第二章】编程求解组合问题
  • 设置zookeeper开机自启动
  • JS 获取浏览器和屏幕宽高信息
  • android 通用 Intent
  • 查询注意事项
  • 用Token令牌维护微服务之间的通信安全的实现
  • Angular学习(一)
  • jsp:jstl标签forTokens
  • Fiddler Web Debugger的下载和安装(图文详解)
  • Navicat for MySQL出现1030-Got error 28 from storage engine错误
  • 通过config文件配置动态导入模块
  • 第二次C语言实验报告
  • [deviceone开发]-do_Webview的基本示例
  • 「前端」从UglifyJSPlugin强制开启css压缩探究webpack插件运行机制
  • 07.Android之多媒体问题
  • express + mock 让前后台并行开发
  • Fundebug计费标准解释:事件数是如何定义的?
  • HashMap剖析之内部结构
  • javascript 哈希表
  • Javascript 原型链
  • JS+CSS实现数字滚动
  • nfs客户端进程变D,延伸linux的lock
  • 爱情 北京女病人
  • 翻译:Hystrix - How To Use
  • 前端每日实战:61# 视频演示如何用纯 CSS 创作一只咖啡壶
  • 让你的分享飞起来——极光推出社会化分享组件
  • 思否第一天
  • 微信端页面使用-webkit-box和绝对定位时,元素上移的问题
  • 消息队列系列二(IOT中消息队列的应用)
  • 用Visual Studio开发以太坊智能合约
  • (2022 CVPR) Unbiased Teacher v2
  • (C++20) consteval立即函数
  • (备忘)Java Map 遍历
  • (超详细)语音信号处理之特征提取
  • (带教程)商业版SEO关键词按天计费系统:关键词排名优化、代理服务、手机自适应及搭建教程
  • (二)JAVA使用POI操作excel
  • (附源码)python旅游推荐系统 毕业设计 250623
  • (附源码)springboot 基于HTML5的个人网页的网站设计与实现 毕业设计 031623
  • (附源码)ssm旅游企业财务管理系统 毕业设计 102100
  • (一)80c52学习之旅-起始篇
  • (转)memcache、redis缓存
  • ./configure,make,make install的作用(转)
  • .NET Core Web APi类库如何内嵌运行?
  • .NET core 自定义过滤器 Filter 实现webapi RestFul 统一接口数据返回格式
  • .NET Framework Client Profile - a Subset of the .NET Framework Redistribution
  • .Net mvc总结
  • .NET 分布式技术比较
  • .NET/C# 使用反射注册事件
  • .NET/C# 阻止屏幕关闭,阻止系统进入睡眠状态
  • .NET中的十进制浮点类型,徐汇区网站设计
  • @ComponentScan比较
  • @SuppressLint(NewApi)和@TargetApi()的区别