• LeetCode 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"
    Output: "aba"
    

    Example 2:

    Input: S = "aaab"
    Output: ""
    

    Note:

    • S will consist of lowercase letters and have length in range [1, 500].

    本题首先要想明白的一点:什么情况会不成立即返回空。

    通过观察可以看出:出现次数最多的字母个数 - 1 > 其他所有字母出现次数和

    此时必有两个出现次数最多的字母连在一起。

    反之,必能形成一个隔一个的情形。

    我的代码比较长,就意识到不是最简单的做法,不过很好理解。

    解释其中必要参数和函数:

    int [] table = new int [26]; // 确定每个字母出现次数
    
    int [] judge = new int [26]; // 确定是否能形成一个隔一个的情形
    
    int count = 0; // 统计除出现次数最多的字母其他字母出现次数和
    int place = 0; // 这个参数比较重要,用于确定下一个字母应该插入在字符串的哪个位置
    public static int find_max(int[] a){} // 返回出现次数最多的字母对应的数组位置

    之后的思想就是往res中插入字母,先把出现次数最多的字母放上去,然后向其中插入其他字母,当place到最后一个位置时回到初始位置。

    代码如下:

    class Solution {
        
        public static int find_max(int[] a){
    		int flag = 0;
    		for(int i = 0; i < a.length; i++){
    			if(flag <= a[i]) flag = a[i];
    		}
    		int j = 0;
    		for(j=0;j<a.length;j++){
    			if(flag == a[j]) break;
    		}
    		return j;
    	}
        public String reorganizeString(String a) {
    		char[] ch = a.toCharArray();
    		int [] table = new int[26];
    		int [] judge = new int[26];
    		String res = "";
    		int num = 0;
    		
    		for (int i = 0; i < a.length(); i++) {
    			table[ch[i] - 'a']++;
    			judge[ch[i] - 'a']++;
    		}
    		
    		Arrays.sort(judge);
    		int count = 0;
    		for(int i = judge.length-2; i >= 0; i--){
    			count += judge[i];
    		}
    		//System.out.println(count);
    		if (judge[judge.length-1] - 1 > count) {
    			//System.out.println(false);
                return res;
    		}
    		//for(int  i=0;i<table.length;i++) System.out.println(table[i]);
    		for (int i = 0; i < table.length; i++) {
    			if(table[i] != 0) num++;
    		}
    		int place = 1;
    		for(int i = 0; i < num; i++){
    			int x = find_max(table);
    			//System.out.println(x);
    //			if(place > res.length() - 1) {
    //				System.out.println("run");
    //				place = 1;
    //			}
    			if(res.length() == 0){
    				while(table[x] != 0){
    					char q = (char) (x + 'a');
    					res += q;
    					table[x]--;
    				}
    			}else{
    				while(table[x] != 0){
    					if(place > res.length()) place = 1;
    					//System.out.println(place);
    					if(place <= res.length() ){
    						StringBuilder sb = new StringBuilder(res);
    						sb.insert(place, (char)(x + 'a'));
    						res = sb.toString();
    						table[x] --;
    						place = place + 2;
    						//System.out.println(res);
    						//System.out.println("此时place:"+place+"而res长度:"+res.length());
    					}
    				}
    				
    			}
    			//System.out.println(res);
    		}
            return res;
    		//System.out.println(res);
        }
    }
    

    声明:该方法只是作者自己的写法,并不是最好的做法,如有更好的方法望在评论区给出。

     

      

      

  • 相关阅读:
    replace函数的使用(替换单个和全局)
    数据库记录删除方式
    UDP接收百万级数据的解决方案
    早睡早起+微信朋友圈控制在10人以内…
    java Date型时间比较大小
    教会你时间管理的奥秘,提升工作效率。
    大多数人都输给了这个字——等
    二分法查找和普通查找
    王者荣耀里拿个王者有啥了不起,有胆就来挑战一下ApsaraCache源码
    给予Java初学者的学习路线建议
  • 原文地址:https://www.cnblogs.com/jiangxiaobin1996/p/8653773.html
Copyright © 2020-2023  润新知