【leetcode】Reorganize String
题目如下:
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same.
If possible, output any possible result. If not possible, return the empty string. Example 1:
Input: S = "aab"
Output: "aba"
Example 2: Input: S = "aaab"
Output: ""
Note:
S will consist of lowercase letters and have length in range [1, 500].
解题思路:刚看到这个题目,我觉得有点无从下手。但是仔细考虑之后,我觉得这个题目满足一个经典的算法场景——把一个数组分成两个子数组,使得两个子数组和最接近。例如,输入的字符串S="aabc",可以被实例化成字段d = {a:2,b:1,c:1},那么三个字符出现的次数就构成了[2,1,1],把数组分成和最接近的两个子数组[2]和[1,1]。最后是生成输出的字符串,只要在两个数组中依次各取一个字符拼接即可。注意:如何两个子数组和差值大于2的话,那么是不可能组成题目要求的字符串的。
代码如下:
//动态规划思想分割子数组
var diff = function(map){
var len = map.length
var sum = 0
for(var i =0;i<len;i++){
sum += map[i].v
}
var dp = []
for(var i =0;i<=len;i++){
var list = []
for(var j = 0;j <= sum/2;j ++){
list.push(0)
}
dp.push(list)
}
for(var i =1;i<=len;i++) {
for(var j = 1;j <= sum/2;j ++) {
if(j>=map[i-1].v){
dp[i][j] = Math.max(dp[i-1][j],dp[i-1][j-map[i-1].v]+map[i-1].v);
}
else {
dp[i][j] = dp[i - 1][j];
}
}
}
//子数组和的差值大于等于2,直接返回空
if (sum - 2*dp[len][parseInt(sum / 2)] >=2){
return ""
}
var t = parseInt(sum/2)
var s1 = ""
var s2 = ""
//确定每个子数组的分配的元素
for(var i=len;i>0;i--){
if(dp[i][t] > dp[i-1][t]){ // 找到第一个接近 sum/2 的,然后与 它上面的比较,如果大于,则代表当前 i 被选中
t -= map[i-1].v;
var tmp = map[i-1].v
while (tmp-- > 0){
s1 += map[i-1].k
}
}
else{
var tmp = map[i-1].v
while (tmp-- > 0){
s2 += map[i-1].k
}
}
}
//生成返回值,依次从每个子数组中取一个元素,注意和较大的子数组排在前面
var s = ""
for(var i = 0;i<Math.min(s1.length,s2.length);i++){
if(s1.length > s2.length){
s += s1[i]
s += s2[i]
}
else{
s += s2[i]
s += s1[i]
} }
if(s1.length != s2.length){
s += (s1.length > s2.length ? s1[s1.length-1] : s2[s2.length-1])
}
return s
} var reorganizeString = function(S) {
var dic = new Array(26)
for(var i=0;i<S.length;i++){
var a = S[i].charCodeAt() - 'a'.charCodeAt()
if(dic[a] == undefined){
dic[a] = 1
}
else{
dic[a] ++
}
}
var d2 = []
for(var i=0;i<dic.length;i++){
if (dic[i] == undefined){
continue
}
var ascii = String.fromCharCode(i + 'a'.charCodeAt())
d2.push({'k':ascii,'v':dic[i]})
} d2.sort(function(a,b){
return b.v - a.v
})
return diff(d2) };
【leetcode】Reorganize String的更多相关文章
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
- 【LeetCode】984. String Without AAA or BBB 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串构造 日期 题目地址:https://leet ...
- 【leetcode】Scramble String
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- 【leetcode】 Interleaving String (hard)
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- 【leetcode】 Scramble String (hard)★
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 【leetcode】Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 【LeetCode】8. String to Integer (atoi) 字符串转整数
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 【leetcode】8. String to Integer (atoi)
题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...
随机推荐
- VGA显示正圆
接着上次的随笔,既然VGA时序已经实现了,那么就显示点东西看看吧. 想显示个圆,但是无从下手,参考了这篇文章:https://user.qzone.qq.com/1241003385/blog/154 ...
- json,异步加载,时间线
JSON是一种传输数据的格式 JSON.stringify(obj); obj--string JSON.parse(str); string-->obj
- 爬虫五之Selenium
Selenium 自动化测试工具,支持多种浏览器: 爬虫中主要用来解决JavaScript渲染问题. 用法详解 基本使用 声明浏览器对象 from selenium import webdriver ...
- js if(!!!e) {} 判断条件中的三个感叹号什么意思
两个感叹号的意思就是,将变量转换为其对应的布尔值. !!e就是e对应的布尔值,true或者false. !!!e==!(!!e)==!true/!false=false/true;
- linux top 查看CPU命令
top 命令主要用于查看进程的相关信息,同时它也会提供查看系统平均负载,cpu 信息和内存信息 实时监控系统资源使用情况 [root@localhost ~]$ top // 动态查看进程使用资源的情 ...
- c++ release和debug语句分离
#ifdef _DEBUG a=1; #else a=2; #endif
- iOS模拟器发生了崩溃,去哪找Crash Log
iOS模拟器发生了崩溃,可以在如下地方找到崩溃日志: ~/Library/Logs/DiagnosticReports/
- 牛逼哄洪的 Java 8 Stream,性能也牛逼么?
那么,Stream API的性能到底如何呢,代码整洁的背后是否意味着性能的损耗呢?本文对Stream API的性能一探究竟. 为保证测试结果真实可信,我们将JVM运行在 -server模式下,测试数据 ...
- numpy中的快速的元素级数组函数
numpy中的快速的元素级数组函数 一元(unary)ufunc 对于数组中的每一个元素,都将元素代入函数,将得到的结果放回到原来的位置 >>> import numpy as np ...
- ubuntu 个人常用命令
重启命令 : 1.reboot 2.shutdown -r now 立刻重启 3.shutdown -r 10 过10分钟自动重启 4.shutdown -r 20:35 ...