G面经prepare: Reorder String to make duplicates not consecutive
字符串重新排列,让里面不能有相同字母在一起。比如aaabbb非法的,要让它变成ababab。给一种即可
Greedy:
跟FB面经Prepare task Schedule II很像,记录每个char出现次数,然后用最大堆,把剩下char里面出现次数多的优先Poll出来组建新的string
如果poll出来的char跟上一个相同,则用一个queue暂时存一下
我觉得时间复杂度:O(N) + O(KlogK) + O(NlogK) = O(NlogK) ,where K is the number of different character in the string
1 package ReorderString; 2 import java.util.*; 3 4 public class Solution { 5 class Element { 6 char val; 7 int appear; 8 public Element(char value) { 9 this.val = value; 10 this.appear = 1; 11 } 12 } 13 14 public String reorder(String str) { 15 Element[] summary = new Element[26]; 16 for (int i=0; i<str.length(); i++) { 17 char cur = str.charAt(i); 18 if (summary[(int)(cur-'a')] == null) { 19 summary[(int)(cur-'a')] = new Element(cur); 20 } 21 else { 22 summary[(int)(cur-'a')].appear++; 23 } 24 } 25 PriorityQueue<Element> queue = new PriorityQueue<Element>(11, new Comparator<Element>() { 26 public int compare(Element e1, Element e2) { 27 return e2.appear - e1.appear; 28 } 29 }); 30 31 for (Element each : summary) { 32 if (each != null) { 33 queue.offer(each); 34 } 35 } 36 Queue<Element> store = new LinkedList<Element>(); 37 StringBuffer res = new StringBuffer(); 38 while (!queue.isEmpty() || !store.isEmpty()) { 39 if (!queue.isEmpty()) { 40 Element cur = queue.poll(); 41 if (res.length()==0 || cur.val!=res.charAt(res.length()-1)) { 42 res.append(cur.val); 43 cur.appear--; 44 if (cur.appear > 0) store.offer(cur); 45 while (!store.isEmpty()) { 46 queue.offer(store.poll()); 47 } 48 } 49 else { //cur.val equals last char in res 50 store.offer(cur); 51 } 52 } 53 else { //store is not empty but queue is empty 54 res = new StringBuffer(); 55 res.append(-1); 56 return res.toString(); 57 } 58 } 59 return res.toString(); 60 } 61 62 63 /** 64 * @param args 65 */ 66 public static void main(String[] args) { 67 // TODO Auto-generated method stub 68 Solution sol = new Solution(); 69 String res = sol.reorder("aaabbba"); 70 System.out.println(res); 71 } 72 73 }
G面经prepare: Reorder String to make duplicates not consecutive相关推荐
- G面经prepare: Set Intersection Set Difference
求两个sorted数组的intersection e.g. [1,2,3,4,5],[2,4,6] 结果是[2,4] difference 类似merge, 分小于等于大于三种情况,然后时间O(m+n ...
- G面经prepare: Pattern Match
设定一个pattern 把 'internationalization' 变成 'i18n', 比如word是house,pattern可以是h3e, 3se, 5, 1o1s1等, 给pattern ...
- G面经prepare: Straight Partition of A Deck of Cards
Define "Straight" as 5 cards with consecutive numbers. Determine if the deck can be fully ...
- mysql prepare原理
注:2013年的老文章 Prepare的作用 Prepare SQL产生的原因.首先从mysql服务器执行sql的过程开始讲起,SQL执行过程包括以下阶段 词法分析->语法分析->语义分析 ...
- 学用 ASP.Net 之 字符串 (2): string.Format
一般应用与左右对齐: protected void Button1_Click(object sender, EventArgs e) {TextBox1.TextMode = TextBoxMode ...
- ACM提交,C++,G++,C,GCC的区别
今天做了一道水题,POJ-1004,水题一个,12个double类型的数求平均数 但是, 1 #include <iostream> 2 #include <cstdio> 3 ...
- JavaScript String 对象扩展方法
/** 在字符串末尾追加字符串 **/ String.prototype.append = function (str) {return this.concat(str); } /** 删除指定索引位 ...
- js中String的常用扩展
js中String的常用扩展,包括trim,检查中文,url,emal,电话号码,转类型,格式化代码等 //去掉字符串空间调用方式 字符串.trim() String.prototype.trim = ...
- 用g.raphael.js高速绘制饼图、柱状图、点状图、折线图(上)
首先介绍一下什么是g.raphael.这个又要说到什么是raphael.js.raphael是一个javascript库,可以用来跨浏览器绘制各种图形,只要是你想得到的图形都可以用raphael绘制出 ...
最新文章
- Linux服务器-使用mysql
- androidstudio常见问题
- ElementUI介绍以及安装
- 【竞赛相关】南大化院博士刘子腾:跨专业如何做数据竞赛浅谈
- LeetCode 633 平方数之和
- 微博授权失败 redirect_uri_mismatch
- 13个DNS 根服务器
- 使用alarm API实现灵活的延时操作
- Android ConstraintLayout 最新使用小结
- 安卓手机如何使用第三方主题,制作专属自己喜好的主题
- 微信PC版 消息汇总和云端控制的开发介绍
- 学习笔记1——制作数据集
- html 字体图标库,阿里字体图标库介绍及图标字体的使用方法
- 【毕设教程】python区块链实现 - proof of work工作量证明共识算法
- 一说出口就被误解的离职原因,这样补救!
- 量子计算与量子信息之量子力学引论(一)
- Linux管道符|命令使用
- 阳光明媚的一天~|~
- 大数据体系的4个热点,4个趋势和3个疑问
- oracle em13c install,如何使用Oracle EM13c管理数据库私有云
热门文章
- 《101 Windows Phone 7 Apps》读书笔记-Silly Eye
- mcgs组态软件中字体如果从左到右变化_昆仑通态专题(十一):MCGS嵌入式组态软件的脚本程序...
- 重新封装了一下NODE-MONGO 使其成为一个独立的服务.可以直接通过get/post来操作
- PowerShell-6.文件操作
- 【错误记录】Visual Studio 中编译 NDK 报错 ( no matching function for call to ‘cacheflush‘ cacheflush(); )
- 【IOC 控制反转】Android 事件依赖注入 ( 事件依赖注入具体的操作细节 | 获取 Activity 中的所有方法 | 获取方法上的注解 | 获取注解上的注解 | 通过注解属性获取事件信息 )
- 【错误记录】SeeMusic 内存错误 ( 内存占用率 100 % | 清除系统设置信息 )
- 【Flutter】Animation 动画 ( AnimatedBuilder 动画使用流程 | 创建动画控制器 | 创建动画 | 创建动画作用的组件 | 关联动画与组件 | 动画执行 )
- 【数据挖掘】数据挖掘总结 ( K-Means 聚类算法 | 二维数据的 K-Means 聚类 ) ★
- 【DBMS 数据库管理系统】数据仓库中 数据追加 ( 时标方法 | DELTA 文件法 | 前后映像文件法 | 日志文件法 )