zoukankan      html  css  js  c++  java
  • LeetCode OJ——Text Justification

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

    编译代码要看warnings!它提供了可能出问题的情况,比如类型转换上unsigned int < int的比较,要用计算机的思维。这道题终究还是没有AC,差不多就这样吧。

    要养成好的代码习惯,可以一边写代码,一边debug。注意变量命名之类的,常规问题。

    Good good study!

      1 #include <iostream>
      2 #include <vector>
      3 #include <string>
      4 using namespace std;
      5 
      6 class Solution {
      7 public:
      8     vector< string> fullJustify(vector <string> & words, int L) {
      9         // IMPORTANT: Please reset any member data you declared, as
     10         // the same Solution instance will be reused for each test case.
     11         int alreadylen = 0;
     12         int beginindex = 0,endindex = 0;
     13         int averagespace = 0,firstspace = 0;
     14         vector<string > result;
     15 
     16         string onepiece("");
     17         for(unsigned int index = 0;index<words.size();index++)
     18         {
     19             beginindex  = endindex = index;
     20             alreadylen = 0;
     21 
     22             while((index<words.size())&&( static_cast<int>(words[index].size()) <= (L-alreadylen)))
     23             {
     24                 int wordsindexsize = words[index].size();
     25                 alreadylen += words[index].size()+1;
     26                 index++;
     27             }
     28 
     29             endindex = index;
     30 
     31             if(beginindex == endindex-1)
     32             {
     33                 onepiece.clear();
     34                 onepiece = words[beginindex];
     35                 if(L -words[beginindex].size()>0)
     36                     onepiece.append( L-words[beginindex].size(),' ');                                                           
     37             }
     38             else if(index != words.size()+1 && index!=words.size())
     39             {
     40                 onepiece.clear();
     41                 averagespace = (L-alreadylen+1)/(endindex-beginindex-1);
     42                 firstspace = averagespace + (L -alreadylen+1)%(endindex-beginindex-1);
     43 
     44                 for(int tt = beginindex;tt<endindex;tt++)
     45                 {
     46                     onepiece +=words[tt];
     47                     if(tt==beginindex)
     48                         onepiece.append(firstspace+1,' ' );
     49                     else if(tt!= endindex -1)
     50                         onepiece.append(averagespace+1,' ' );
     51                 }
     52             }
     53             else
     54             {
     55                 onepiece.clear();
     56                 int alreadylength = 0;
     57                 for(int tt = beginindex;tt<endindex;tt++)
     58                 {
     59                     
     60                     onepiece += words[tt];
     61                     alreadylength += words[tt].size();
     62                     if(tt!=words.size()-1)
     63                     {
     64                         onepiece.append(1,' ' );
     65                         alreadylength++;
     66                     }
     67                     else
     68                         onepiece.append(L-alreadylength,' ' );
     69 
     70                 }
     71 
     72             }
     73             index--;
     74             result.push_back(onepiece);
     75         }
     76         return result; 
     77     }
     78     };
     79 
     80 
     81     int main()
     82     {
     83         Solution *mySolution = new Solution();
     84 
     85         //words: [This, "is", "an", "example", "of", "text", "justification."]
     86         //L: 16.
     87         vector<string > words;
     88  
     89         /*words.push_back( "this");
     90         words.push_back( "is");
     91         words.push_back( "an");
     92         words.push_back( "example");
     93         words.push_back( "of"); 
     94         words.push_back( "text"); 
     95         words.push_back( "justification."); */
     96         words.push_back( "what");
     97         words.push_back( "must");
     98         words.push_back( "be");
     99         words.push_back( "shall");
    100         words.push_back( "be."); 
    101         vector<string > output;
    102         output = mySolution->fullJustify(words,12);
    103         for(unsigned int i = 0;i<output.size();i++)
    104             cout<<output[i]<<endl;
    105 
    106         return 0;
    107     }
  • 相关阅读:
    docker简单介绍----Dockerfile命令
    docker简单介绍----docker仓库的应用
    nginx实现https的配置文件
    docker简单介绍---网络端口管理
    mysql查看实时连接数
    linux 批量测试域名返回码脚本
    docker简单介绍----镜像和容器管理
    香冢
    GitHub 在使用命令行 git push 时报错:The requested URL returned error: 403
    守规矩应该被骂吗?
  • 原文地址:https://www.cnblogs.com/qingcheng/p/3401145.html
Copyright © 2011-2022 走看看