767. Reorganize String

 1 class Solution {
 2     public String reorganizeString(String S) {
 3         if(S.length() == 0) return "";
 4         int[] arr = new int[26];
 5         int max = 0;
 6         for(int i = 0; i < S.length(); i++){
 7             arr[S.charAt(i) - ‘a‘]++;
 8         }
 9         int index = 0;
10         int sum = 0;
11         for(int i = 0; i < 26; i++){
12             sum += arr[i];
13             if(max < arr[i]){
14                 max = arr[i];
15                 index = i;
16             }
17         }
18         sum = sum - arr[index];
19         arr[index] = 0;
20         if(sum < max-1){
21             return "";
22         }
23         String[] res = new String[max];
24         for(int i = 0; i < max; i++){
25             res[i] = "" + (char)(‘a‘ + index);
26             for(int j = 0; j < 26; j++){
27                 if(arr[j] != 0){
28                     res[i] = res[i] + (char)(‘a‘ + j);
29                     arr[j]--;
30                     break;
31                 }
32
33             }
34         }
35         for(int i = 0; i < max; i++){
36             for(int j = 0; j < 26; j++){
37                 if(arr[j] != 0){
38                     res[i] = res[i] + (char)(‘a‘ + j);
39                     arr[j]--;
40                 }
41
42             }
43         }
44
45         String str = "";
46         for(int i = 0; i < max; i++){
47             str += res[i];
48         }
49         return str;
50
51     }
52 }

原文地址:https://www.cnblogs.com/goPanama/p/9882390.html

时间: 2024-08-04 09:36:41

767. Reorganize String的相关文章

[LC] 767. Reorganize String

Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same. If possible, output any possible result.  If not possible, return the empty string. Example 1: Input: S = "aab" Out

LeetCode - Reorganize String

Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same. If possible, output any possible result. If not possible, return the empty string. Example 1: Input: S = "aab" Outp

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

【LeetCode】堆 heap(共31题)

[23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无序数组中最小/大的K个数) 给了一个无序数组,可能有重复数字,找到第 k 个最大的元素并且返回这个元素值. 题解:直接用直接用个堆保存数组中最大的 K 个数.时间复杂度是 O(NlogK). 1 //时间复杂度是 O(NlogK), 用堆辅助. 2 class Solution { 3 public: 4 int findKthLargest(vector<int>

【LeetCode】排序 sort(共20题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [56]Merge Intervals [57]Insert Interval [75]Sort Colors [147]Insertion Sort List [148]Sort List [164]Maximum Gap [179]Largest Number [242]Valid Anagram [252]Meeting Rooms (2018年11月22日,为

【LeetCode】贪心 greedy(共38题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [44]Wildcard Matching [45]Jump Game II (2018年11月28日,算法群衍生题) 题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步. 题解: [55]Jump Game (2018年11月27日,算法群) 给了一个数组nums,nums[i] = k 代表站在第 i 个位置的情况下, 我最多能往前走 k 个单

EcmaScript源码

1 /** 2 @type {Number} 3 @const 4 */ 5 NaN = 0; 6 /** 7 @type {Number} 8 */ 9 Infinity = 0; 10 undefined = 0; 11 /** 12 @param {*} x 13 @return {Object} 14 */ 15 eval = function(x) {}; 16 /** 17 @param {*} s 18 @param {Number} [radix] 19 @return {Num

[Swust OJ 767]--将军回家(Dijkstra算法)

题目链接:http://acm.swust.edu.cn/problem/767/ Time limit(ms): 1000 Memory limit(kb): 65535 Description 在涪江河的两边共有n个城市,其中位于一边的城市属于1类城市,另外一边的属于2类城市,(特别的:城市1属于1类,城市2属于2类).现在知道一些道路的情况,比如知道城市1到城市5之间有一条长度为100的路.将军要从城市1回到城市2的家,他就开始设计回家的线路.回家时由于驾照的关系,只能越过一次涪江河.现在

hive报错: Specified key was too long; max key length is 767 bytes

DataNucleus.Datastore (Log4JLogger.java:error(115)) - An exception was thrown while adding/validating class(es) : Specified key was too long; max key length is 767 bytescom.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Specified key was too