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. Python学习--21天Python基础学习之旅(Day01、Day02)

    21天的python基础学习,使用<Python从入门到实践>,并且需要手敲书中的code,以下为整个学习过程的记录. Day01: 安装python时要选择复选框 Add Python ...

  2. Linux网络配置:Nat和桥接模式详解

    Linux网络配置:Nat和桥接模式详解 一.我们首先说一下VMware的几个虚拟设备: Centos虚拟网络编辑器中的虚拟交换机: VMnet0:用于虚拟桥接网络下的虚拟交换机: VMnet1:用于 ...

  3. putty 远程登录ubuntu的方法

    首先,要确定linux的ssh服务已经开启了: 首先要确定开启了ssh-server: 安装:sudo apt-get install openssh-server             安装:su ...

  4. 【Android开发】Coding + git命令行基本使用

    上传代码 进入本地仓库的目录. cd ... 查看仓库链接 : git remote -v 如果没有,则添加url链接 : git remote add testName https://git.co ...

  5. 世界各国 MCC 和 MNC 列表

    http://www.cnblogs.com/inteliot/archive/2012/08/22/2651666.html常见MCC:代码(MCC)    ISO 3166-1    国家202 ...

  6. ES6(Promise)等一个函数执行完后再执行另一个函数

    function text1(){ return new Promise((resolve, reject) => { setTimeout(function () { resolve(cons ...

  7. 前端如何通过js判断浏览器的类型(忽略版本)web html css javascript

    每个页面浏览器会实例出一个window对象,在window对象下有一个属性navigator,navigator本身是一个对象,navigator对象上有一个属性userAgent里面包含了当前浏览器 ...

  8. Vue UI 可视化项目管理界面

    除了直接使用npm的命令进行安装脚手架的安装以外,我们还可以使用Vue提供的GUI方法vue ui来进行项目的构建以及安装 win+R powershell 打开终端 在一个干净的目录下输入命令 vu ...

  9. 控制反转 IOC 理论推导

    控制反转 IOC 理论推导 按照我们传统的开发,我们会先去 dao 层创建一个接口,在接口中定义方法. public interface UserDao { void getUser(); } 然后再 ...

  10. 一致性Hash的原理与实现

    应用场景 在了解一致性Hash之前,我们先了解一下一致性Hash适用于什么场景,能解决什么问题?这里先放一下我自己认为适用的场景.一致性Hash适用于服务器动态扩展且需要负载均衡的场景 试想以下场景, ...