leetcode_767 重构字符串

leetcode_767 重构字符串

题目

给定一个字符串S,检查是否能重新排布其中的字母,使得两相邻的字符不同。

若可行,输出任意可行的结果。若不可行,返回空字符串。

示例 1:

输入: S = “aab”
输出: “aba”
示例 2:

输入: S = “aaab”
输出: “”

思路

一:借助PriorityQueue
代码:

class Solution {
    public String reorganizeString(String S) {
        if(S.length()<2||S==null) return S;
        char[] chars=S.toCharArray();
        int[] nums=new int[26];
        int maxcount=0;
       //记录字符串中每个字母出现的次数
       for(char x:chars){
            nums[x-'a']++;
            maxcount=Math.max(maxcount,nums[x-'a']);
       }
       //如果最大的字母数大于数组的一半,那么他肯定就不能重组
       if(maxcount>(S.length()+1)/2) return "";
         PriorityQueue<Character> queue = new PriorityQueue<Character>(new Comparator<Character>() {
            public int compare(Character letter1, Character letter2) {
                return nums[letter2 - 'a'] - nums[letter1 - 'a'];
            }
        });
        //对26个字母进行遍历但是不对数组chars进行遍历是为了queue中有重复的字符
        for(int i=0;i<26;i++){
            if(nums[i]>0)
                queue.offer((char)(i+'a'));
        }
        StringBuffer res=new StringBuffer();
        while(queue.size()>1){
            char a=queue.poll();
            char b=queue.poll();
            res.append(a);
            res.append(b);

            nums[a-'a']--;
            nums[b-'a']--;

            if(nums[a-'a']>0)
                queue.offer(a);
            if(nums[b-'a']>0)
                queue.offer(b);          
        }
        if (queue.size() > 0) {
            res.append(queue.poll());
        }
    return res.toString();

    }
}

二:不借助PriotyQueue(),就是每一次都找最大值,然后每一次都加入最大值的数字和其余任意一个数字
代码:

class Solution {
    public String reorganizeString(String S) {
        if(S.length()<2||S==null) return S;
        char[] chars=S.toCharArray();
        int[] nums=new int[26];
        int maxcount=0;
       //记录字符串中每个字母出现的次数
       for(char x:chars){
            nums[x-'a']++;
            maxcount=Math.max(maxcount,nums[x-'a']);
       }
       if(maxcount>(S.length()+1)/2) return "";
       StringBuffer res=new StringBuffer();
       while(res.length()!=S.length()){
           int max=0;
           //找出当前的具有最大字母数的字母
           for(int i=0;i<26;i++){
               if(nums[i]>nums[max])
                    max=i;
           }
           //将这个字母加入
           res.append((char)(max+'a'));
           nums[max]--;
           //再放入一个不同的字母
           for(char x:chars){
               if(nums[x-'a']>0&&x!=(char)(max+'a')){
                res.append(x);
                nums[x-'a']--;
                break;
               }
           }
       }
       return res.toString();
    }
}

总结

1.PriorityQueue可以按照自己所定义的东西进行排序

PriorityQueue queue = new PriorityQueue(new Comparator() {
public int compare(Character letter1, Character letter2) {
return nums[letter2 - ‘a’] - nums[letter1 - ‘a’];
}
});

peek() 返回队头元素但是不出队
poll() 出队头元素
offer()入队元素
isEmpty()判断是否为空

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值