zoukankan      html  css  js  c++  java
  • [leetcode] 68. 文本左右对齐(国区第240位AC的~)

    68. 文本左右对齐

    国区第240位AC的~我还以为坑很多呢,一次过,嘿嘿,开心

    其实很简单,注意题意:使用“贪心算法”来放置给定的单词;也就是说,尽可能多地往每行中放置单词。

    也就是说,一行里放置多少word,是一次确定的,后面的行word怎么分配,跟前面的行没有关系。

    注意有个隐藏条件:两个单词间至少有一个空格

    那么我们就可以把问题分解了:

    第一步:找出一行需要放置哪些word(“贪心算法”来放置给定的单词,尽可能多地往每行中放置单词。)
    第二步:word怎么放,也就是空格如何分配(平均分配就好了,具体看代码把,写的很清楚)

    此题的示例给的很良心,把需要注意的地方都给出来了,你能过示例基本这题就过了

    class Solution {
        public List<String> fullJustify(String[] words, int maxWidth) {
            int index = 0;
            List<String> ans = new ArrayList<>();
            while (true) {
                int curWordCnt = 0;
                int curAllWordLength = 0;
                List<String> tmp = new ArrayList<>();
                // 第一步,先挑出哪些word需要填充
                while (index < words.length && curAllWordLength + curWordCnt + words[index].length() <= maxWidth) {
                    tmp.add(words[index]);
                    curWordCnt++;
                    curAllWordLength += words[index].length();
                    index++;
                }
                StringBuilder s = new StringBuilder();
                s.append(tmp.get(0));
                if (index == words.length) {
                    // 如果是最后一行,特殊处理
                    for (int i = 1; i < tmp.size(); i++) {
                        s.append(" ").append(tmp.get(i));
                    }
                    while (s.length() < maxWidth) {
                        s.append(" ");
                    }
                    ans.add(s.toString());
                    break;
                }
                // 第二步,word怎么排?主要是空格的分配
                int spaceLength = maxWidth - curAllWordLength;
                int averSpaceLength = 0;
                if (curWordCnt > 1) {
                    averSpaceLength = spaceLength / (curWordCnt - 1);
                }
                int overflowSpaceLength = spaceLength - averSpaceLength * (curWordCnt - 1);
                for (int i = 1; i < tmp.size(); i++) {
                    for (int j = 0; j < averSpaceLength; j++) {
                        s.append(" ");
                    }
                    if (overflowSpaceLength > 0) {
                        overflowSpaceLength--;
                        s.append(" ");
                    }
                    s.append(tmp.get(i));
                }
                // 如果只有一个单词,特殊处理,后面全部填充空格
                if (tmp.size() == 1) {
                    while (s.length() < maxWidth) {
                        s.append(" ");
                    }
                }
                ans.add(s.toString());
    
            }
    
            return ans;
        }
    }
    
  • 相关阅读:
    常用博客Metaweblog Api地址
    如何在Mac下配置Github和Bitbucket的SSH
    Java内部类持有外部类的引用详细分析与解决方案
    java 静态变量生命周期(类生命周期)
    比较List和ArrayList的性能及ArrayList和LinkedList优缺点
    List和ArrayList的区别
    hashmap可以用null为键值
    iOS各种调试技巧豪华套餐
    Split()[1]中的[1]是什么意思
    windows安装TortoiseGit详细使用教程
  • 原文地址:https://www.cnblogs.com/acbingo/p/9368542.html
Copyright © 2011-2022 走看看