Question

767. Reorganize String

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的更多相关文章

  1. [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 就是给你一个字符串,能不 ...

  2. 767. Reorganize String

    Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...

  3. [LeetCode] 767. Reorganize String 重构字符串

    Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...

  4. 【LeetCode】767. Reorganize String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/reorganiz ...

  5. LeetCode - 767. Reorganize String

    Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...

  6. [LC] 767. Reorganize String

    Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...

  7. [LeetCode] Reorganize String 重构字符串

    Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...

  8. LeetCode - Reorganize String

    Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...

  9. 【leetcode】Reorganize String

    题目如下: Given a string S, check if the letters can be rearranged so that two characters that are adjac ...

随机推荐

  1. HTML5 Audio & Video 属性解析

    一.HTML 音频/视频 方法 play() play() 方法开始播放当前的音频或视频. var myVideo=document.getElementById("video1" ...

  2. Java/C++实现模板方法模式---数据库操作

    对数据库的操作一般包括连接.打开.使用.关闭等步骤,在数据库操作模板类中我们定义了connDB().openDB().useDB().closeDB()四个方法分别对应这四个步骤.对于不同类型的数据库 ...

  3. Java/C++实现观察者模式--股票价格

    当股票的价格上涨或下降5%时,会通知持有该股票的股民,当股民听到价格上涨的消息时会买股票,当价格下降时会大哭一场. 类图: Java代码: public class Investor implemen ...

  4. CCS基础样式表

    一.css样式表 1.样式表分类 1.内联式 <p >This is an apple</p> 2.内嵌样式表 作为一个独立的区域 内嵌在网页里面,必须写在head标签里面 & ...

  5. MFC---文档与视图结构

    文档与视图结构 文档.视图的关系,是一对多的映射,一个文档可以对应多个视图,而一个视图只能对应一个文档.例如,一个.html文件,可以用记事本打开,也可以用浏览器打开,这里的.html文件就是文档,记 ...

  6. Ubuntu中hyperledger-fabric2.3.0环境搭建

    系统环境 hyperledger-fabric在Ubuntu安装过程,fabric版本为2.3.0 首先安装相关软件 1.安装docker 直接参考下面这篇文档安装好docker-ce即可 Ubunt ...

  7. mysql查询 if判断、case语句的使用等

    一. 查询的数字转换为中文返回前端 1. 如果是0或1表状态等,可用: IF(字段 = 0, '否', '是') AS xxx 2. 如果是多个值,比如1,2,3可用: ELT(字段, '计划治理', ...

  8. Java学习day24

    今天学习了IP地址.端口以及TCP/UDP通信协议 网络连接与过去的信件类似,需要知道对方的地址才能寄出去,在计算机网络中,我们的地址就是IP以及端口号 IP能用来唯一定位一台联网的计算机 本机的IP ...

  9. OpenHarmony 3.1 Beta 版本关键特性解析——ArkUI canvas组件

    (以下内容来自开发者分享,不代表 OpenHarmony 项目群工作委员会观点) 江英杰 华为技术有限公司 canvas 是 ArkUI 开发框架里的画布组件,常用于自定义绘制图形.因为其轻量.灵活. ...

  10. Codeforces Round #671 (Div. 2) B. Stairs 难度1200

    题目链接: Problem - 1419B - Codeforces 题目 题意 给x个格子,你可以用这x个格子去拼成楼梯 好的楼梯的要求如下: 1. 第n列有n个格子 2. 这个楼梯的所有格子可以被 ...