Leetcode 68. Text Justification
来源:互联网 发布:深圳雅思培训机构 知乎 编辑:程序博客网 时间:2024/05/21 04:43
vector<string> fullJustify(vector<string>& words, int maxWidth) { vector<string> rs; int L = maxWidth; //得预先判断后一个单词的长度对,数组行长度的影响,j=i+1 for(int i=0 ; i<words.size(); ) //i固定在每行开头的单词,只有执行最后一步才换行i才加一 { int j = i+1; //判断普通行的多个单词的插入情况,每加一个单词都在 原行长度len = words[i].length() 上加上单词长度 int len = words[i].length(); for(;j<words.size() && len+words[j].length()<L;j++){ //更新len长度:+空格+单词长度 len += 1+words[j].length(); } //判断最后一个单词的情况:可以直接在最后一行加入它 if(j==words.size()) { string s_cur(words[i]); for(i+=1;i<j;i++) s_cur +=" "+words[i]; //加上之前的所有单词后,再判断新单词加入后长度和L的关系 //int len_end = s_cur.length()+1+words[j].size(); //if(len_end > L){//加上后溢出的话,则补上空格不加单词 while(s_cur.length()<L) s_cur.push_back(' '); rs.push_back(s_cur); //string s_end(words[j]); //rs.push_back(s_end); return rs; } //执行for循环后,改行的j-i=1即改行只有一个单词时,补全空格 if(j-i==1){ rs.push_back(words[i++]); rs.back().append(L-rs.back().length(), ' ');//补全每行长度 减去 当前行长度的空格个数 continue; } //普通行最后的空格插入,多少个单词空隙?(j-i-1)需要插入多少空格(L-len),每个空隙插入多少空格(L-len)/(j-i-1)? int a = (L-len) / (j-i-1) + 1;//多少个单词间空隙,空隙多少个空格 int b = (L-len) % (j-i-1); //第一个空隙补足空格数 string s(words[i]); for (i++; i < j; i++, b--) { s.append(a,' '); if (b>0) s.push_back(' '); s.append(words[i]); } rs.push_back(s); }return rs; }
0 0
- LeetCode 68. Text Justification
- LeetCode --- 68. Text Justification
- [Leetcode] 68. Text Justification
- [leetcode] 68.Text Justification
- [leetcode] 68. Text Justification
- LeetCode 68. Text Justification
- Leetcode 68. Text Justification
- LeetCode 68. Text Justification
- leetcode 68. Text Justification
- [LeetCode] 68. Text Justification
- LeetCode-68.Text Justification
- leetcode 68. Text Justification
- leetcode-68. Text Justification
- Leetcode 68. Text Justification
- [LeetCode] 68. Text Justification
- 【LeetCode】68. Text Justification
- Leetcode 68. Text Justification
- Leetcode 68. Text Justification
- pycharm安装第三方库
- 二叉树的遍历
- Java 常用设计模式-适配器模式adapter
- 分享移动端车牌识别OCR识别流程
- Vue.js前后端同构方案之准备篇——代码优化
- Leetcode 68. Text Justification
- Iframe页面传值
- 1、嵌入式开发工具链
- 使用maven创建spring-boot项目
- android百度地图开发——android studio配置sdk
- 10/5/2017记录 ubuntu+python+sublime3+插件
- hypervisor介绍
- Android中SPI的使用
- 屏幕适配