📣 独立日限时特惠: VIP通行证立减$68
查看: 2230| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

LC 68 Text justification

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
请问一下有没有人只到这一行code讲的是什么啊?

主要是 第一个for loop 的 left = right
和第二个 for loop 的 大脑友好写法是什么样子的啊?
···
for(int left = 0; left < N; left = right){
            // Each word comes with one space;
            // Except the first word, so start with -1.
            int len = -1;
            for(right = left; right < N && len + words[right].length() + 1 <= maxWidth; right ++){
                len += words[right].length() + 1;
            }
···
https://mnmunknown.gitbooks.io/algorithm-notes/content/628,_linkedin_mian_jing_ti.html

上一篇:请教find时间复杂度
下一篇:Bloomberg 的一道旧面试题
推荐
newgod2500 2017-2-21 12:57:35 | 只看该作者
全局:
有个C#的代码,希望楼主可以参考下

  1.    public IList<string> FullJustify(string[] words, int maxWidth) {
  2.         var res = new List<string>();
  3.         

  4.         for(int i = 0, currIndex; i< words.Length; i = currIndex)
  5.         //i: index of words
  6.         //currIndex: the current index of words in the line
  7.         //lineLen: Current total length of words in the line
  8.         {
  9.             int lineLen = -1; //we need to skip the space for last word hence start len is -1.
  10.             for(currIndex = i; currIndex < words.Length && lineLen + words[currIndex].Length + 1 <= maxWidth; currIndex++ )
  11.                lineLen = lineLen + words[currIndex].Length + 1; //default one space between words
  12.             
  13.             StringBuilder curStr = new StringBuilder(words[i]);
  14.             int space = 1, extra = 0;
  15.             //default value for evenlyDistributeSpaces-> deal with last line case
  16.             
  17.             //not 1 char, not last line
  18.             //currIndex - i - 1 :
  19.             //currIndex is already pointing to nex index
  20.             // -1 mean n there are n-1 gap between n words
  21.             if(currIndex != i+1 && currIndex != words.Length)
  22.             {
  23.                 space = (maxWidth - lineLen) / (currIndex - i - 1) + 1; // +1 mean for the default one space between words
  24.                 extra = (maxWidth - lineLen) % (currIndex - i - 1);
  25.             }
  26.             
  27.             
  28.             
  29.             //not 1 char, including last line
  30.             for(int j = i + 1; j< currIndex; j++) //j: index of word in the current line
  31.               {
  32.                   for(int s = space; s>0;s--) curStr.Append(" "); //Add the "even" space
  33.                   if(extra-- > 0) curStr.Append(" ");
  34.                   curStr.Append(words[j]);
  35.               }
  36.               
  37.             //reach the last line
  38.             int strLen = maxWidth - curStr.Length;
  39.             while(strLen-- > 0) curStr.Append(" ");
  40.             res.Add(curStr.ToString());
  41.             
  42.         }
  43.         
  44.         return res;
  45.     }
复制代码

评分

参与人数 1大米 +3 收起 理由
arteeezy + 3 回答的很好!

查看全部评分

回复

使用道具 举报

🔗
newgod2500 2017-2-21 12:56:32 | 只看该作者
全局:
粗略看了下...是不是博主写错了.你高亮一下left和right, right只有第二个子for loop 才有increment, 而下面的所有right都是使用常量0...博主不是漏了brackets..就是 某个地方写错了.欢迎指正
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表