767. Reorganize String
Posted gopanama
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }
以上是关于767. Reorganize String的主要内容,如果未能解决你的问题,请参考以下文章
sql server中index的REBUILD和REORGANIZE
Use Word 2010's Navigation Pane to quickly reorganize documents