【leetcode】Text Justification

本文介绍了一个文本对齐算法,该算法能够将给定的一组单词按照指定长度进行格式化,并确保每一行都达到完全左右对齐的效果。文章详细解释了算法的工作原理,包括如何在单词间分配空白符以保持对齐,同时提供了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 spaces ' ' when necessary so that each line has exactlyL characters.

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.

 

Corner Cases:
  • A line other than the last line might contain only one word. What should you do in this case?
    In this case, that line should be left-justified.
 
 

1.每两个单词之间至少一个空格

2.相邻空格数目之差不超过1
3.在左边的空格数目一定不小于右边空格的数目
4.如果一行只有一个单词,则左对齐,后面插空格
5.对于最后一行,左对齐,两个单词之间必须只空一个空格
 
对于非最后一行,假设所有单词的总长度为totalWordsLen,则必须插入的空格书为spaceNum=L-totalWordsLen
设共有n+1个单词,则有n个间隔
 
则第i个间隔处的空格数space[i]可以通过下面计算
int n1=spaceNum/n;
int n2=spaceNum%n;
vector<int> space(n,n1);
for(int i=0;i<n2;i++)  space[i]+=1;
 
 1 class Solution {
 2 public:
 3     vector<string> fullJustify(vector<string> &words, int L) {
 4        
 5         int n=words.size();
 6        
 7         vector<string> result;
 8        
 9         int count=0;
10         int totalWordsLen=0;
11         int startIndex=0;
12        
13         string tmpStr;
14        
15         for(int i=0;i<n-1;i++)
16         {
17             count++;
18             totalWordsLen+=words[i].size();
19             int len=totalWordsLen+count-1;
20            
21             if(len+words[i+1].size()+1>L)
22             {
23                 tmpStr=formLine(startIndex,i,L,totalWordsLen,words);
24                 result.push_back(tmpStr);
25                
26                 count=0;
27                 totalWordsLen=0;
28                 startIndex=i+1;
29             }
30         }
31        
32         tmpStr=formLastLine(startIndex,L,words);
33         result.push_back(tmpStr);
34        
35         return result;
36     }
37     string formLine(int start,int end,int &L,int totalWordsLen,vector<string> &words)
38     {
39         int n=end-start;
40         int spaceNum=L-totalWordsLen;
41         if(n==0) return words[start]+string(spaceNum,' ');
42        
43         int n1=spaceNum/n;
44         int n2=spaceNum%n;
45        
46         vector<int> space(n,n1);
47         for(int i=0;i<n2;i++)  space[i]+=1;
48  
49         string tmpStr="";
50         for(int i=start;i<end;i++)
51         {
52             tmpStr+=(words[i]+string(space[i-start],' '));
53         }
54        
55         tmpStr+=words[end];
56         return tmpStr;
57        
58     }
59    
60     string formLastLine(int start,int &L,vector<string> &words)
61     {
62         string tmpStr=words[start];
63         for(int i=start+1;i<words.size();i++)
64         {
65             tmpStr+=' '+words[i];
66         }
67         while(tmpStr.length()<L)
68         {
69             tmpStr+=' ';
70         }
71         return tmpStr;
72     }
73 };
74  

 

 
 

转载于:https://www.cnblogs.com/reachteam/p/4225059.html

内容概要:本文探讨了在MATLAB/SimuLink环境中进行三相STATCOM(静态同步补偿器)无功补偿的技术方法及其仿真过程。首先介绍了STATCOM作为无功功率补偿装置的工作原理,即通过调节交流电压的幅值和相位来实现对无功功率的有效管理。接着详细描述了在MATLAB/SimuLink平台下构建三相STATCOM仿真模型的具体步骤,包括创建新模型、添加电源和负载、搭建主电路、加入控制模块以及完成整个电路的连接。然后阐述了如何通过对STATCOM输出电压和电流的精确调控达到无功补偿的目的,并展示了具体的仿真结果分析方法,如读取仿真数据、提取关键参数、绘制无功功率变化曲线等。最后指出,这种技术可以显著提升电力系统的稳定性与电能质量,展望了STATCOM在未来的发展潜力。 适合人群:电气工程专业学生、从事电力系统相关工作的技术人员、希望深入了解无功补偿技术的研究人员。 使用场景及目标:适用于想要掌握MATLAB/SimuLink软件操作技能的人群,特别是那些专注于电力电子领域的从业者;旨在帮助他们学会建立复杂的电力系统仿真模型,以便更好地理解STATCOM的工作机制,进而优化实际项目中的无功补偿方案。 其他说明:文中提供的实例代码可以帮助读者直观地了解如何从零开始构建一个完整的三相STATCOM仿真环境,并通过图形化的方式展示无功补偿的效果,便于进一步的学习与研究。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值