Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >LeetCode 68. Text Justification

LeetCode 68. Text Justification

作者头像
ShenduCC
发布于 2019-09-23 07:01:12
发布于 2019-09-23 07:01:12
32000
代码可运行
举报
文章被收录于专栏:算法修养算法修养
运行总次数:0
代码可运行

题目

没有意思的字符串模拟题

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution {
public:
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        
        vector<string> latest;
        vector<vector<int>> result;
        vector<int> ans;
        int num=0;
        for(int i=0;i<words.size();i++)
        {
            if(num==0){
                num+=words[i].length();
                
            }
            else
                num+=words[i].length()+1;
            
            if(num==maxWidth)
            {
                ans.push_back(i);
                result.push_back(ans);
                ans.clear();
                num=0;
            }
            else if(num>maxWidth)
            {
                result.push_back(ans);
                ans.clear();
                ans.push_back(i);
                
                num=0;
                num+=words[i].length();
            }
            else
            {
                ans.push_back(i);
            }
        }
        
        if(ans.size()!=0)
        {
            result.push_back(ans);
        }
        
        string str="";
        for(int i=0;i<result.size();i++)
        {
            if(i==result.size()-1)
            {
                str="";
                str+=words[result[i][0]];
                for(int j=1;j<result[i].size();j++)
                {
                    str+=" ";
                    str+=words[result[i][j]];
                }
                int l=str.length();
                for(int k=0;k<maxWidth-l;k++)
                {
                    str+=" ";
                }
                latest.push_back(str);
                break;
            }
            str="";
            int sum=0;
            int len=result[i].size();
            for(int j=0;j<result[i].size();j++)
            {
                sum+=words[result[i][j]].length();
            }
            
            int s = maxWidth-sum;
            
            int av=0;
            int sb=0;
            if(len!=1){
             av = s/(len-1);
             sb = s%(len-1);
            }
            
            str+=words[result[i][0]];

            for(int j=1;j<result[i].size();j++)
            {
                for(int k=0;k<av;k++)
                {
                    str+=" ";
                }
                if(sb>0)
                {
                    str+=" ";
                    sb--;
                }
                str+=words[result[i][j]];
            }
            if(len==1)
            {
                int l=str.length();
                for(int k=0;k<maxWidth-l;k++)
                {
                    str+=" ";
                }
            }
            
            latest.push_back(str);
            
        }
        
        return latest;
        
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-09-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Leetcode 68 Text Justification
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
triplebee
2018/01/12
6610
LeetCode 0068 - Text Justification
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.
Reck Zhang
2021/08/11
2130
leetcode: 68. Text Justification [✗]
Problem # 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 l
JNingWei
2018/09/27
4320
LeetCode 68. 文本左右对齐(字符串逻辑题)
给定一个单词数组和一个长度 maxWidth,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本。
Michael阿明
2020/07/13
8090
LeetCode 241. Different Ways to Add Parentheses
题目 递归,分治,暴力跑就可以了 class Solution { public: vector<int> diffWaysToCompute(string input) { vector<int> ans; if(input=="") return ans; int tag=0; for(int i=0;i<input.length();i++) { i
ShenduCC
2020/04/08
3050
LeetCode Contest 166
但是在用c++的快排的时候,被坑了,我一直的习惯是写自定义比较函数,没有写运算符重载,不知道为什么一直RE,浪费了挺多时间
ShenduCC
2019/12/11
2990
LeetCode 95. Unique Binary Search Trees II
题目 DFS, class Solution { public: int vis[100005]; int m; vector<TreeNode*> generateTrees(int n) { vector<TreeNode*> ans; if(n==0) return ans; return fun(1,n); } vector<TreeNode*> fun(int l,int r)
ShenduCC
2020/01/15
2420
LeetCode 30 Substring with Concatenation of All Words
题目 食之无味的题目 class Solution { public: map<string,int> m; map<int,string> m2; map<string,int> m3; int b[100005]; int c[100005]; vector<int> findSubstring(string s, vector<string>& words) { vector<int> ans;
ShenduCC
2019/07/18
2980
Leetcode 54. Spiral Matrix
版权声明:博客文章都是作者辛苦整理的,转载请注明出处,谢谢! https://blog.csdn.net/Quincuntial/article/details/81004791
Tyan
2019/05/25
4170
Leetcode 30 Substring with Concatenation of All Words 无序map的应用细节
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters. For example, given:
triplebee
2018/01/12
4880
[Leetcode][python]Text Justification/文本左右对齐
来自:https://shenjie1993.gitbooks.io/leetcode-python/068%20Text%20Justification.html 把一个集合的单词按照每行L个字符存放,不足的在单词间添加空格,每行要两端对齐(即两端都要是单词),如果空格不能均匀分布在所有间隔中,那么左边的空格要多于右边的空格,最后一行靠左对齐,每个单词间一个空格。
蛮三刀酱
2019/03/26
1.9K0
LeetCode weekly contest 278 (amazon pay)
第三题 O(n)的计算hash值。利用取模运算法则,从后往前先计算k个字符的hash 值, 然后开始向左移动,每次移动都要先减去右边最后一个值,然后再乘以P,最后加上左边的
ShenduCC
2022/03/10
3060
【leetcode】Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:
阳光岛主
2019/02/19
4340
LeetCode 周赛 184
判断字符串是不是子串,效率高的方式应该是字典树,按照字典序排序后,建树,再建的过程中就可以得到答案。 但是这是比赛中,又是第一题,所以直接用contains了
ShenduCC
2020/04/13
3450
LeetCode 22. 括号生成(回溯/DP)
给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。
Michael阿明
2020/07/13
5160
LeetCode 22. 括号生成(回溯/DP)
Text Justification — LeetCode
原标题链接: http://oj.leetcode.com/problems/text-justification/ 这道题属于纯粹的字符串操作。要把一串单词安排成多行限定长度的字符串。主要难点在于空格的安排,首先每一个单词之间必须有空格隔开。而当当前行放不下很多其它的单词而且字符又不能填满长度L时。我们要把空格均匀的填充在单词之间。假设剩余的空格量刚好是间隔倍数那么就均匀分配就可以。否则还必须把多的一个空格放到前面的间隔里面。实现中我们维护一个count计数记录当前长度。超过之后我们计算共同的空格量以及多出一个的空格量,然后将当行字符串构造出来。最后一个细节就是最后一行不须要均匀分配空格。句尾留空就能够。所以要单独处理一下。时间上我们须要扫描单词一遍,然后在找到行尾的时候在扫描一遍当前行的单词,只是整体每一个单词不会被訪问超过两遍,所以整体时间复杂度是O(n)。而空间复杂度则是结果的大小(跟单词数量和长度有关,不能准确定义,假设知道最后行数r。则是O(r*L))。代码例如以下:
全栈程序员站长
2022/07/06
4260
LeetCode 107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
ShenduCC
2018/07/24
2800
等值线算法「建议收藏」
大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。 Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
全栈程序员站长
2022/09/20
8880
等值线算法「建议收藏」
LeetCode 164. Maximum Gap (排序)
题解:首先,当然我们可以用快排,排完序之后,遍历一遍数组,就能得到答案了。但是快速排序的效率是O(n* logn),不是题目要求的线性效率,也就是O(n)的效率。
ShenduCC
2020/02/14
3840
LeetCode(Weekly Contest 183)题解
0. 前言 中文版地址:https://leetcode-cn.com/contest/weekly-contest-183/ 英文版地址:https://leetcode.com/contest/weekly-contest-183/ 1. 题解 1.1 5380. 数组中的字符串匹配(1408. String Matching in an Array) 中文版题目描述:https://leetcode-cn.com/problems/string-matching-in-an-array/submis
西凉风雷
2022/11/23
2290
相关推荐
Leetcode 68 Text Justification
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验