zoukankan      html  css  js  c++  java
  • Text Justification [LeetCode]

    Problem Description:http://oj.leetcode.com/problems/text-justification/

    Note: Just be careful about boundary when implementing it.

     1 class Solution {
     2 public:
     3     string genNormalLine(vector<string> &words, int L, int start, int end){
     4         int word_count = end - start + 1;
     5         string line;
     6         if(word_count == 1){
     7             line.append(words[start]);
     8             line.append(L-line.size(), ' ');
     9         }else{
    10             int slot_num = word_count - 1;
    11             int words_size = 0;
    12             for(int i = start; i <= end; i++)
    13                 words_size += words[i].size();
    14             
    15             int space_num = L - words_size;
    16             int reminder = space_num % slot_num;
    17             int quotient = space_num / slot_num;
    18             line.append(words[start]);
    19             for(int i = start + 1; i <= end; i++) {
    20                 if(reminder > 0 ){
    21                     line.append(quotient + 1, ' ');
    22                     reminder --;
    23                 } else {
    24                     line.append(quotient, ' ');
    25                 }
    26                 
    27                 line.append(words[i]);
    28             }
    29         }
    30         
    31         return line;
    32     }
    33     
    34     vector<string> fullJustify(vector<string> &words, int L) {
    35         // Note: The Solution object is instantiated only once and is reused by each test case.
    36         vector<string> formatted_lines;
    37         if(words.size() == 0)
    38             return formatted_lines;
    39         for(int i = 0; i < words.size();){
    40             int length = 0;
    41             
    42             int j = i;
    43             for(; j < words.size(); j ++){
    44                 if( j == i)
    45                     length += words[j].size();
    46                 else 
    47                     length += (1+ words[j].size());
    48                 
    49                 if(length > L)
    50                     break;
    51             }
    52             
    53             //put words in a line
    54             if(j == words.size()) {
    55                 //last line
    56                 string line;
    57                 line.append(words[i]);
    58                 for(int k = i + 1; k < words.size(); k++){
    59                     line.push_back(' ');
    60                     line.append(words[k]);
    61                 }
    62                 if(line.size() < L){
    63                     line.append(L-line.size(), ' ');
    64                 }
    65                 
    66                 formatted_lines.push_back(line);
    67             }else {
    68                 //normal line
    69                 formatted_lines.push_back(genNormalLine(words,L, i, j - 1));
    70             }
    71             
    72             //next start index
    73             i = j;
    74         }
    75         
    76         return formatted_lines;
    77     }
    78 };
  • 相关阅读:
    视差滚动(Parallax Scrolling)插件补充
    10个最佳的触控手式的JavaScript框架(转)
    50个必备的实用jQuery代码段(转)
    PhoneGap开发不可或缺的五件装备
    优化移动网站的9大窍门(转)
    WSADATA
    htons
    INADDR_ANY
    SOCKADDR_IN
    mysql_query()与mysql_real_query()
  • 原文地址:https://www.cnblogs.com/guyufei/p/3371814.html
Copyright © 2011-2022 走看看