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 ...
随机推荐
- jdbc的快速入门(需要mysql-connector-java-5.1.39-bin.jar包)
package Lianxi;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;i ...
- 微信小程序中涉及虚拟产品购买,ios暂不支持的相关整理意见
这个东西呢也不能怪微信小程序,属实苹果搞的事情,那怎么小程序相关内容去通过审核呢? 首先我们要搞清楚哪些属于虚拟商品:如某某书币,某某会员,或者是提前编辑好的网课,文档等都属虚拟商品购买,简言之就是没 ...
- Python这些位运算的妙用,绝对让你大开眼界
位运算的性能大家想必是清楚的,效率绝对高.相信爱好源码的同学,在学习阅读源码的过程中会发现不少源码使用了位运算.但是为啥在实际编程过程中应用少呢?想必最大的原因,是较为难懂.不过,在面试的过程中,在手 ...
- python学习笔记(三)——函数
函数定义 def 函数名(形参 . . . ) 函数体 1. 函数参数 返回值:可以有一个或多个 形参:支持默认形参.关键字形参.可变参数形参等 1.1 必须参数 调用时传入的参数必须与定义时相同. ...
- H5扇形
使用H5 canvas绘制的可交互扇形 requestAnimationFrame() 现有动画实现方式的不足 setTimeout和setInterval都不十分精确.为它们传入的第二个参数,实际上 ...
- oracle 序列的使用
序列: 是oacle提供的用于产生一系列唯一数字的数据库对象. 自动提供自增的唯一的数值. 共享的数据 主要用于提供主键值 将序列装入内存可以提高访问效率 1.创建序列: 1. 要有创建序列的权限 ...
- 微信小程序版博客——开发汇总总结(附源码)
花了点时间陆陆续续,拼拼凑凑将我的小程序版博客搭建完了,这里做个简单的分享和总结. 整体效果 对于博客来说功能页面不是很多,且有些限制于后端服务(基于ghost博客提供的服务),相关样式可以参考截图或 ...
- mysql基本操作2
##DDL控制表结构,不支持事务##DML控制表数据,支持事务 DQL专门做查询 ##TCL 管理事务##DCL 管理数据库权限 ##ORDER BY 子句-根据指定列对结果集 ...
- java基础-多线程互斥锁
多线程(JDK1.5的新特性互斥锁)* 1.同步 * 使用ReentrantLock类的lock()和unlock()方法进行同步* 2.通信 * 使用ReentrantLock类的newCondit ...
- JAVASE While 计算累加实例
package com.huang.boke.flowPath;public class While { public static void main(String[] args) { int i= ...