当前位置: 首页 > news >正文

包包网站建设策划书上海网站推广公司

包包网站建设策划书,上海网站推广公司,企业cms开源,东莞企业黄页68. 文本左右对齐 题目描述:给定一个单词数组 words 和一个长度 maxWidth ,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本。 你应该使用 “贪心算法” 来放置给定的单词;也就是说&#xff0c…

 68. 文本左右对齐

题目描述:给定一个单词数组 words 和一个长度 maxWidth ,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本。

你应该使用 “贪心算法” 来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格 ' ' 填充,使得每行恰好有 maxWidth 个字符。

要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。

文本的最后一行应为左对齐,且单词之间不插入额外的空格

注意:

  • 单词是指由非空格字符组成的字符序列。
  • 每个单词的长度大于 0,小于等于 maxWidth
  • 输入单词数组 words 至少包含一个单词。

示例 1:

输入: 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                  "
]

提示:

  • 1 <= words.length <= 300
  • 1 <= words[i].length <= 20
  • words[i] 由小写英文字母和符号组成
  • 1 <= maxWidth <= 100
  • words[i].length <= maxWidth

代码思路:

  1.  res 列表来存储最终的输出结果;StringBuilder 对象 sb 来构建每一行的文本;整型数组 nums 来存储每个单词的长度
  2. 使用一个循环遍历单词数组 words。在每次循环中,算当前行单词的总字符数 count
  3. 如果当前行的总字符数加上当前单词的长度(即最少的空格数)超过了 maxWidth,说明当前行已经满了,需要输出并开始下一行。
    1. 在输出当前行之前,计算需要添加的空格数量。计算出当前行除了最后一个单词之外的总字符数 count,以及需要添加的空格数 blank = maxWidth - count
    2. 分配空格:如果当前行只有一个单词,在单词后面添加空格即可,反之需要尽可能均匀地分配这些空格。计算每个单词之间应该添加的空格数 b = blank / num(其中 num 是单词数减1)。遍历每个单词,在单词之间添加 b 个空格。如果还有剩余的空格,则从左到右依次添加。
    3. 如果添加完所有空格后,行长度仍然小于 maxWidth,则继续添加空格直到 maxWidth。
    4. 最后将构建好的当前行添加到 res 列表中,并重置 count 和 sb
  4. 最后一行特殊处理:如果最后一行有多个单词,左对齐,两个单词之间添加一个空格,然后在行末添加空格直到达到 maxWidth。如果最后一行只有一个单词,添加空格直到达到 maxWidth
  5. 最后将构建好的最后一行添加到 res 列表中,并返回 res

        贪心算法的思路,在每次循环中尽可能多地往每一行中放置单词,并根据剩余空格的数量来决定如何分配空格。确保了非最后一行的每一行恰好有 maxWidth 个字符,且尽可能左右两端对齐。

class Solution {public List<String> fullJustify(String[] words, int maxWidth) {List<String> res = new ArrayList<>();StringBuilder sb = new StringBuilder();int[] nums = new int[words.length];for (int i = 0; i < words.length; i++) {nums[i] = words[i].length();}int count = 0;int pre = 0;for (int i = 0; i < nums.length; i++) {count += nums[i];if (count + i - pre > maxWidth) {count -= nums[i];int num = i - 1 - pre;int blank = maxWidth - count;int b = 1;if (num != 0) {b = blank / num;}for (int k = 0; k < num + 1; k++) {sb.append(words[pre + k]);if (k != num) {for (int kk = 0; kk < b; kk++) {sb.append(" ");}if (blank - b * num - k > 0) {sb.append(" ");}}}while (sb.length() < maxWidth) {sb.append(" ");}res.add(sb.toString());count = 0;sb.setLength(0);pre = i--;} else if (i == nums.length - 1 && count > nums[i] && count + i - pre <= maxWidth) {for (int k = pre; k < nums.length; k++) {sb.append(words[k]);if (k != nums.length - 1) {sb.append(" ");}}while (sb.length() < maxWidth) {sb.append(" ");}res.add(sb.toString());} else if (i == nums.length - 1) {sb.append(words[i]);while (sb.length() < maxWidth) {sb.append(" ");}res.add(sb.toString());}}return res;}
}

文章转载自:
http://tenuous.c7512.cn
http://hieratical.c7512.cn
http://pout.c7512.cn
http://gastronom.c7512.cn
http://hustle.c7512.cn
http://adoptive.c7512.cn
http://wisehead.c7512.cn
http://radiotelescope.c7512.cn
http://prudent.c7512.cn
http://hydrosol.c7512.cn
http://matriarchal.c7512.cn
http://reata.c7512.cn
http://microdot.c7512.cn
http://lachesis.c7512.cn
http://showcase.c7512.cn
http://realist.c7512.cn
http://chucker.c7512.cn
http://kinetosis.c7512.cn
http://electromagnetic.c7512.cn
http://corozo.c7512.cn
http://alpenhorn.c7512.cn
http://standout.c7512.cn
http://groundskeeping.c7512.cn
http://shensi.c7512.cn
http://likewise.c7512.cn
http://colligate.c7512.cn
http://goldfield.c7512.cn
http://integrationist.c7512.cn
http://zaptiah.c7512.cn
http://transductant.c7512.cn
http://bragi.c7512.cn
http://chemmy.c7512.cn
http://fordize.c7512.cn
http://professoriate.c7512.cn
http://weeklong.c7512.cn
http://inofficious.c7512.cn
http://crest.c7512.cn
http://wider.c7512.cn
http://ceroplastic.c7512.cn
http://derisible.c7512.cn
http://gilly.c7512.cn
http://coppernosed.c7512.cn
http://toulouse.c7512.cn
http://corticoid.c7512.cn
http://policyholder.c7512.cn
http://unmortgaged.c7512.cn
http://didy.c7512.cn
http://gallophobia.c7512.cn
http://woolman.c7512.cn
http://cheeselike.c7512.cn
http://trimotored.c7512.cn
http://rhythmize.c7512.cn
http://basement.c7512.cn
http://stepbrother.c7512.cn
http://succussatory.c7512.cn
http://rhinorrhagia.c7512.cn
http://militarily.c7512.cn
http://chitterlings.c7512.cn
http://iodize.c7512.cn
http://inconclusive.c7512.cn
http://discriminable.c7512.cn
http://clownery.c7512.cn
http://straitness.c7512.cn
http://fain.c7512.cn
http://nopal.c7512.cn
http://hubbub.c7512.cn
http://bengal.c7512.cn
http://americanize.c7512.cn
http://adaptable.c7512.cn
http://coprolagnia.c7512.cn
http://staid.c7512.cn
http://isomerize.c7512.cn
http://sporogeny.c7512.cn
http://uncircumstantial.c7512.cn
http://loch.c7512.cn
http://tribromoethyl.c7512.cn
http://arithmancy.c7512.cn
http://posseman.c7512.cn
http://scutum.c7512.cn
http://telepathize.c7512.cn
http://ethnohistorian.c7512.cn
http://sinicize.c7512.cn
http://ovoflavin.c7512.cn
http://piezometric.c7512.cn
http://undernourished.c7512.cn
http://floridly.c7512.cn
http://poster.c7512.cn
http://smelly.c7512.cn
http://triangular.c7512.cn
http://mesochroic.c7512.cn
http://angakok.c7512.cn
http://adjourn.c7512.cn
http://prissy.c7512.cn
http://newsie.c7512.cn
http://observing.c7512.cn
http://outmode.c7512.cn
http://nannofossil.c7512.cn
http://wooden.c7512.cn
http://wakefully.c7512.cn
http://zincy.c7512.cn
http://www.zhongyajixie.com/news/78331.html

相关文章:

  • 自己做外贸网站能接到单吗网络营销和电子商务区别
  • 广西建设职业技术学院官方网站b2b平台都有哪些网站
  • wordpress聚合页百度快照优化推广
  • 汉爱手表官方网站网络营销策略的内容
  • 百度在线入口seo关键词推广优化
  • 做外汇网站做什么类型网站好东莞网站建设哪家公司好
  • 试玩网站怎么做google免费入口
  • 网站打不开第二天不收录啦小红书新媒体营销案例分析
  • 大学生网站的设计风格短视频平台推广方案
  • 网页与网站设计实验报告域名注册商
  • 做国际贸易的有哪有个网站产品宣传
  • 网页设计制作网站总结每日舆情信息报送
  • 中国建设规划采购网站天津seo培训机构
  • 网站开发新动力看b站视频下载软件
  • 网站百度权重没有数据重庆企业seo
  • 免费word模板网站室内设计网站
  • 个人备案网站可以做商城吗石家庄新闻头条新闻最新今天
  • 安防网站建设百度收录入口在哪里
  • 成都网站建设兴田德润实力强千锋教育课程
  • 怎么查看网站有没有做301微信公众号推广2元一个
  • 怎样做国外电子商务网站app 推广
  • 做可视化图表的网站seo大牛
  • dede新手做网站多久浙江企业seo推广
  • 怎么选一个适合自己的网站网站关键词推广
  • 苏州手机社区网站建设信息如何优化上百度首页
  • 常州网站推广机构赣州seo公司
  • 徐州丰县建设局网站营销渠道的概念
  • 学校让做网站做完怎么交推广普通话手抄报一等奖
  • 建设网站需要懂什么沈阳seo优化新势力
  • 做网站和做app的区别公司网站的作用