zoukankan      html  css  js  c++  java
  • 重构字符串

    package Leetcode;

    import java.util.ArrayList;

    import java.util.Collections;
    import java.util.Comparator;
    import java.util.HashMap;

    import java.util.List;
    import java.util.Map;

    import java.util.Map.Entry;
    /**
     * 给定一个字符串S,检查是否能重新排布其中的字母,使得两相邻的字符不同。
    若可行,输出任意可行的结果。若不可行,返回空字符串。
     */
    public class reOrganize {
        public static void main(String[] args) {
            String S = "sfffp";
            String result = reorganizeString(S);
            int x = 0;

        }

        public static String reorganizeString(String S) {
            if (S == null || S.length() == 0) {
                return "";
            }
            Map<Character, Integer> map = new HashMap<>();
            for (int i = 0; i < S.length(); i++) {
                if (map.containsKey(S.charAt(i))) {
                    map.put(S.charAt(i), map.get(S.charAt(i))+1);
                } else {
                    map.put(S.charAt(i), 1);
                }
            }
            List<Entry<Character, Integer>> list = new ArrayList<Entry<Character, Integer>>(map.entrySet());

            Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() {

                @Override
                public int compare(Entry<Character, Integer> o1, Entry<Character, Integer> o2) {
                    if(o1.getValue()>o2.getValue()){
                        return -1;
                    }else if(o1.getValue()>o2.getValue()){
                        return 1;
                    }else{
                        return 0;
                    }
                }
            });
            int x=list.get(0).getValue();
            int sum=0;
            for(int i=1;i<list.size();i++){
                sum=sum+list.get(i).getValue();
            }
            if(sum+1<x){
                return "";
            }
            char []result=new char[S.length()];
            //新字符串构建方法:首先把最长的字母放到偶数位置上,
            //接着用其他字符填充剩余的偶数位置
            //超过字符串长度开始填充奇数位置
           int j=0;
           for(int i=0;i<x;i++){
               result[j]=list.get(0).getKey();
                j=j+2;
           }
           for(int i=1;i<list.size();i++){
               int l=list.get(i).getValue();
               char c=list.get(i).getKey();
               for(int k=0;k<l;k++){
                   if(j>=S.length()){
                        j=1;
                   }
                    result[j]=c;
                    j=j+2;
                }
           }
            
            return String.valueOf(result);
            
        }
        
    }
  • 相关阅读:
    Linux常用命令学习2---(文件搜索命令locate find、命令搜索命令whereis which、字符串搜索命令grep、帮助命令man)
    LeetCode Perfect Squares
    华为笔试 数字转中文拼音
    二位数组 顺时针打印矩阵
    LeetCode Interleaving String
    LeetCode Coins in a Line
    LeetCode Backpack
    LeetCode Unique Paths
    LeetCode Minimum Path Sum
    腾讯模拟笔试题
  • 原文地址:https://www.cnblogs.com/jieyi/p/14063592.html
Copyright © 2011-2022 走看看