日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LeetCode 68. 文本左右对齐(字符串逻辑题)

發布時間:2024/7/5 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode 68. 文本左右对齐(字符串逻辑题) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 題目

給定一個單詞數組和一個長度 maxWidth,重新排版單詞,使其成為每行恰好有 maxWidth 個字符,且左右兩端對齊的文本。

你應該使用“貪心算法”來放置給定的單詞;也就是說,盡可能多地往每行中放置單詞。必要時可用空格 ’ ’ 填充,使得每行恰好有 maxWidth 個字符。

要求盡可能均勻分配單詞間的空格數量。如果某一行單詞間的空格不能均勻分配,則左側放置的空格數要多于右側的空格數。

文本的最后一行應為左對齊,且單詞之間不插入額外的空格。

說明:
單詞是指由非空格字符組成的字符序列。
每個單詞的長度大于 0,小于等于 maxWidth。
輸入單詞數組 words 至少包含一個單詞。

示例: 輸入: words = ["This", "is", "an", "example", "of", "text", "justification."] maxWidth = 16 輸出: ["This is an","example of text","justification. " ]示例 2: 輸入: words = ["What","must","be","acknowledgment","shall","be"] maxWidth = 16 輸出: ["What must be","acknowledgment ","shall be " ] 解釋: 注意最后一行的格式應為 "shall be " 而不是 "shall be",因為最后一行應為左對齊,而不是左右兩端對齊。 第二行同樣為左對齊,這是因為這行只包含一個單詞。示例 3: 輸入: words = ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"] maxWidth = 20 輸出: ["Science is what we","understand well","enough to explain to","a computer. Art is","everything else we","do " ]

來源:力扣(LeetCode) 鏈接:https://leetcode-cn.com/problems/text-justification
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。

2. 解題

class Solution { // C++ public:vector<string> fullJustify(vector<string>& words, int maxWidth) {vector<string> ans;string line;int i, width = 0, wc;for(i = 0; i < words.size(); ++i){if(line.empty()){ //為空,直接加入單詞line = words[i];width = words[i].size();wc = 1;//單詞個數}else{if(width+1+words[i].size() <= maxWidth){ //還能加入line += " "+words[i];width += 1+words[i].size();wc++;}else//超了,放不下i{process(wc,line,maxWidth,width);//處理單詞ans.push_back(line);//該行存入答案line = "";width = wc = 0;i--;}}}line += string(maxWidth-width,' ');//最后一行左對齊,后面補空格ans.push_back(line);return ans;}void process(int wc, string& line, int maxWidth, int width){if(wc == 1)//只有一個單詞,直接后面補空格{line += string(maxWidth-width,' ');return;}int space = maxWidth - width;//需要的空格數int n = space/(wc-1);//平均插入個數int pos = wc-1;//可以插入的位置個數for(int i = line.size()-1; i >= 0; --i){if(line[i] == ' '){ //找到空格了line.insert(i,n,' ');//插入平均的個數space -= n;//空格數更新pos--;//位置數更新if(pos > 0 && space%pos == 0)//位置還有,且能被整除n = space/pos;//變成整除的(左邊空格大于右邊條件)}}} };

0 ms 7 MB

class Solution:# py3def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:ans = []line = ""width = 0wc = 0def process(wc,line,width):if wc==1:line += ' '*(maxWidth-width)return linespace = maxWidth-widthn = space//(wc-1)pos = wc-1line = list(line)size = len(line)for i in range(size-1,-1,-1):if line[i]==' ':line.insert(i, ' '*n)space -= npos -= 1if pos > 0 and space%pos==0:n = space//posline = "".join(line)return linei = 0while i < len(words):if len(line)==0:line = words[i]width = len(words[i])wc = 1else:if width+1+len(words[i]) <= maxWidth:line += " "+words[i]width += 1+len(words[i])wc += 1else:temp = process(wc,line,width)ans.append(temp)line = ""width, wc = 0, 0i -= 1i += 1line += ' '*(maxWidth-width)ans.append(line)return ans

44 ms 13.5 MB

總結

以上是生活随笔為你收集整理的LeetCode 68. 文本左右对齐(字符串逻辑题)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。