767. Reorganize String - LeetCode
Question

Solution
题目大意:
给一个字符串,将字符按如下规则排序,相邻两个字符一同,如果相同返回空串否则返回排序后的串。
思路:
首先找出字符数量最大的字符,如果数量大于S长度的一半,返回空字符串,否则将该字符从索引0开始间隔着放(0,2,4,...),放到头再从索引1开始以2为步长放
Java实现:
public String reorganizeString(String S) {
int[] letters = new int[26];
for (char c : S.toCharArray()) {
letters[c - 'a']++;
}
int maxIdx = 0;
int max = letters[0];
for (int i = 1; i < letters.length; i++) {
if (max < letters[i]) {
max = letters[i];
maxIdx = i;
}
}
if (max > Math.ceil(S.length() / 2.0)) {
return "";
}
int tmpIdx = 0;
char[] retArr = new char[S.length()];
for (int j = 0; j < max; j++) {
retArr[tmpIdx] = (char) (maxIdx + 'a');
tmpIdx += 2;
}
for (int i = 0; i < letters.length; i++) {
if (letters[i] > 0 && i != maxIdx) {
for (int j = 0; j < letters[i]; j++) {
if (tmpIdx >= S.length()) tmpIdx = 1;
retArr[tmpIdx] = (char) (i + 'a');
tmpIdx += 2;
}
}
}
return String.valueOf(retArr);
}
767. Reorganize String - LeetCode的更多相关文章
- [leetcode]Weekly Contest 68 (767. Reorganize String&&769. Max Chunks To Make Sorted&&768. Max Chunks To Make Sorted II)
766. Toeplitz Matrix 第一题不说,贼麻瓜,好久没以比赛的状态写题,这个题浪费了快40分钟,我真是...... 767. Reorganize String 就是给你一个字符串,能不 ...
- 767. Reorganize String
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- [LeetCode] 767. Reorganize String 重构字符串
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- 【LeetCode】767. Reorganize String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/reorganiz ...
- LeetCode - 767. Reorganize String
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- [LC] 767. Reorganize String
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- [LeetCode] Reorganize String 重构字符串
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- LeetCode - Reorganize String
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- 【leetcode】Reorganize String
题目如下: Given a string S, check if the letters can be rearranged so that two characters that are adjac ...
随机推荐
- (stm32f103学习总结)—stm32外部中断
一.外部中断介绍 1.1 EXTI简介 EXTI简介 STM32F10x外部中断/事件控制器(EXTI)包含多达 20 个用于产生事 件/中断请求的边沿检测器.EXTI的每根输入线都可单独进行配置,以 ...
- WebGL2系列之顶点数组对象
使用了顶点缓冲技术后,绘制效率有了较大的提升.但是还有一点不尽如人意,那就是顶点的位置坐标.法向量.纹理坐标等不同方面的数据每次使用时需要单独指定,重复了一些不必要的工作.WebGL2提供了一种专门用 ...
- java中hashCode和equals什么关系,hashCode到底怎么用的
Object类的hashCode的用法:(新手一定要忽略本节,否则会很惨) 马 克-to-win:hashCode方法主要是Sun编写的一些数据结构比如Hashtable的hash算法中用到.因为ha ...
- 正则、字符类Pattern、Matcher类
字符类 * [abc] a.b 或 c(简单类) * [^abc] 任何字符,除了 a.b 或 c(否定) * [a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围) * [0-9 ...
- es5语法下,javascript如何判断函数是new还是()调用
es5语法没有支持类class,但是可以通关函数来申明一个类,如下: function Person(name){ this.name=name; } var john=new Person('joh ...
- 论文解读(Graph-MLP)《Graph-MLP: Node Classification without Message Passing in Graph》
论文信息 论文标题:Graph-MLP: Node Classification without Message Passing in Graph论文作者:Yang Hu, Haoxuan You, ...
- springboot集成spring security安全框架入门篇
一. :spring security的简介 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下 ...
- Mybatis-自定义类型处理器
类型转换器:mybatis中有一些常用的类型转换器,比如把Java中的short类型转换为mysql中的short类型:但是如果现在是Java中的Date类型,但是我想要存储到数据库中转换为Long类 ...
- Java基础语法02——流程控制
流程控制:顺序结构.分支结构(if-else.switch-case).循环结构(for.while.do-while)
- Mybatis项目无法初始化异常
该异常是Maven资源导出时出错,.xml文件或者.properties文件不能正常导出所致,最简单的办法就是在目标文件上复制粘贴一份.xml文件或者是.properties文件: 但是实际应用的过程 ...