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. 数据结构:DHUOJ 单链表ADT模板应用算法设计:长整数加法运算(使用单链表存储计算结果)

    单链表ADT模板应用算法设计:长整数加法运算(使用单链表存储计算结果) 时间限制: 1S类别: DS:线性表->线性表应用 题目描述: 输入范例: -5345646757684654765867 ...

  2. 从零开始画自己的DAG作业依赖图(四)--节点连线优化版

    概述 上个版本简单的连线在一些复杂场景,尤其层级比较多,连线跨层级比较多的情况下,会出现线条会穿过矩形的情况,这一讲就是在这个基础上,去优化这个连线. 场景分析 在下面几种情况下,简单版本的画法已经没 ...

  3. CSS入门指南-4:页面布局

    这是<CSS设计指南>的读书笔记,用于加深学习效果. display 属性 display是 CSS 中最重要的用于控制布局的属性.每个元素都有一个默认的 display 值.对于大多数元 ...

  4. java中如何打印规定图案? 举例说明

    9.4 print out the following pattern(打印图案). * *** ***** ******* ***** *** * 提示: 1)本题上面的图案和下面的图案是一样的.所 ...

  5. 初始化properties

    package com.letech.common; import java.io.IOException; import java.util.Properties; public class Con ...

  6. JavaScript 中 append()、prepend()、after()、before() 的区别

    内容 append().prepend().after().before() 的区别 jQuery 操作 DOM 之添加节点 方法名 作用 $(selector).append() 指定元素内部添加, ...

  7. JavaScript实现带正则表达式的表单校验(校验成功后跳转)

    运行结果: 源代码: 1 <!DOCTYPE html> 2 <html lang="zh"> 3 <head> 4 <meta char ...

  8. DTO数据传输对象详解

    文章目录 一.DTO是什么? 二.DTO解决的问题 三.代码演示 一.DTO是什么? DTO (数据传输对象) 数据传输对象(DTO),是一种设计模式之间传输数据的软件应用系统.数据传输目标往往是数据 ...

  9. API管理工具介绍

    ​ 时间都去哪里了 敏捷迭代和团队协作,前后端分离的工作模式几乎是每个互联网公司的常规工作模式. 前后端分离,各自开发的优点很多,其中一项是它只需要提供一个统一的API接口,即可被web,iOS,An ...

  10. 地铁系统ajax人性化操作

    根据选择的城市不同,显示的地铁系统和线路图也不同