149_leetcode_Text Justification
来源:互联网 发布:手机阻拦广告软件 编辑:程序博客网 时间:2024/05/16 12:32
Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.
You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' '
when necessary so that each line has exactly Lcharacters.
Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.
For the last line of text, it should be left justified and no extra space is inserted between words.
For example,
words: ["This", "is", "an", "example", "of", "text", "justification."]
L: 16
.
Return the formatted lines as:
[ "This is an", "example of text", "justification. "]
Note: Each word is guaranteed not to exceed L in length.
1:注意特殊情况;2:最后一行左对其,并且单词与单词之间只有一个空格;非最后一行,单词左对其和右对其,并且单词与单词之间的空格尽可能均匀, 如果只有一个单词的话,则是左对其;3:获得可以组成一行的单词,并对这一行单词进行相应的处理。
vector<string> fullJustify(vector<string> &words, int L) { vector<string> result; if(words.size() == 0) { return result; } int wordLength = 0; int count = -1; int size = (int)words.size(); vector<string> lineWords; for(int i = 0; i < size; i++) { if(count + 1 + words[i].length() > L) { result.push_back(normal_line(lineWords, L, wordLength)); lineWords.clear(); count = -1; wordLength = 0; } count = count + 1 + (int)words[i].length(); wordLength += words[i].length(); lineWords.push_back(words[i]); } result.push_back(end_line(lineWords, L, wordLength)); return result; } string normal_line(vector<string>& lineWords, int L, int wordLength) { string tmp = ""; int wordNumber = (int)lineWords.size(); if(wordNumber == 1) { tmp.append(lineWords[0]); for(int i = 0; i < L - wordLength; i++) { tmp.append(" "); } return tmp; } int even_space = (L - wordLength) / (wordNumber - 1); int left_space = (L - wordLength) % (wordNumber - 1); tmp.append(lineWords[0]); for(int i = 1; i < wordNumber; i++) { for(int j = 0; j < even_space; j++) { tmp.append(" "); } if(left_space > 0) { tmp.append(" "); left_space--; } tmp.append(lineWords[i]); } return tmp; } string end_line(vector<string>& lineWords, int L, int wordLength) { string tmp = ""; tmp.append(lineWords[0]); int size = (int)lineWords.size(); for(int i = 1; i < size; i++) { tmp.append(" "); tmp.append(lineWords[i]); } for(int i = 0; i < L - wordLength - size + 1; i++) { tmp.append(" "); } return tmp; }
- 149_leetcode_Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- ssh 免密码登陆
- android.app.SuperNotCalledException: Activity {com.lcxx.nur.view/com.lcxx.nur.view.doctorsadvice.Doc
- Vijos 1052 贾老二算算术
- 建设网站优质外链
- linux进程通信之共享内存
- 149_leetcode_Text Justification
- 多校第一场 1010(HDU4870) rating---DP
- 在线投票系统中刷票与反刷票的斗争
- 中科燕园GIS外包案例之10----水资源webgis系统
- 如何把PDF转WORD文字转换器在线转换
- SWT图片处理(大小缩放,透明,置灰,旋转,反色)
- windows 无线网络共享
- STORM与HADOOP的比较
- C#MD5加密方法