• 1592. Rearrange Spaces Between Words


    You are given a string text of words that are placed among some number of spaces. Each word consists of one or more lowercase English letters and are separated by at least one space. It's guaranteed that text contains at least one word.

    Rearrange the spaces so that there is an equal number of spaces between every pair of adjacent words and that number is maximized. If you cannot redistribute all the spaces equally, place the extra spaces at the end, meaning the returned string should be the same length as text.

    Return the string after rearranging the spaces.

    Example 1:

    Input: text = "  this   is  a sentence "
    Output: "this   is   a   sentence"
    Explanation: There are a total of 9 spaces and 4 words. We can evenly divide the 9 spaces between the words: 9 / (4-1) = 3 spaces.
    

    Example 2:

    Input: text = " practice   makes   perfect"
    Output: "practice   makes   perfect "
    Explanation: There are a total of 7 spaces and 3 words. 7 / (3-1) = 3 spaces plus 1 extra space. We place this extra space at the end of the string.
    

    Example 3:

    Input: text = "hello   world"
    Output: "hello   world"
    

    Example 4:

    Input: text = "  walks  udp package   into  bar a"
    Output: "walks  udp  package  into  bar  a "
    

    Example 5:

    Input: text = "a"
    Output: "a"
    

    Constraints:

    • 1 <= text.length <= 100
    • text consists of lowercase English letters and ' '.
    • text contains at least one word.
    class Solution {
        public String reorderSpaces(String text) {
            int count = 0;
            List<String> list = new ArrayList();
            StringBuilder cur = new StringBuilder();
            for(char c : text.toCharArray()) {
                if(c == ' ') {
                    count++;
                    if(cur.length() != 0)   list.add(cur.toString());
                    cur = new StringBuilder();
                }
                else {
                    cur.append(c);
                }
            }
            if(cur.length() != 0) list.add(cur.toString());
            int specin, speclast;
            if(list.size() == 1) {
                specin = 0;
                speclast = count;
            }
            else {
                specin = count / (list.size() - 1);
                speclast = count % (list.size() - 1);   
            }
            
            // System.out.println(count+" "+list.size()+" "+speclast);
            StringBuilder sb1 = new StringBuilder();
            for(int i = 0; i < specin; i++) sb1.append(" ");
            StringBuilder sb2 = new StringBuilder();
            for(int i = 0; i < speclast; i++) sb2.append(" ");
            StringBuilder sb3 = new StringBuilder();
            for(int i = 0; i < list.size() - 1; i++) sb3.append(list.get(i)).append(sb1.toString());
            return sb3.append(list.get(list.size() - 1)).append(sb2).toString();
        }
    }

    小细节挺多,值得个medium吧。。

    尤其是只有1个string,或者结尾没有空格等情况。其他就照着来,先统计中间加多少空格,然后是屁股加多少空格。

  • 相关阅读:
    JavaWeb的一些理解
    phpMysql安装遇到的两个问题
    正则表达式学习1
    《SOA思想、技术与系统集成应用详解》读书笔记四《SOA核心技术及应用》Apache Tuscany sca安装及与eclipse结合
    19本书,选5本,任两本不邻粗解
    J2ME中处理日期相关问题
    《JSP2.0 技术手册》读书笔记二HttpSession
    《SOA思想、技术与系统集成应用详解》读书笔记三SCA
    《SOA思想、技术与系统集成应用详解》读书笔记一
    《SOA思想、技术与系统集成应用详解》读书笔记二Java Web服务
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/13760568.html
Copyright © 2020-2023  润新知