Sentence Screen Fitting -LintCode

204 篇文章 0 订阅

Given a rows x cols screen and a sentence represented by a list of non-empty words, find how many times the given sentence can be fitted on the screen.

 注意事项

 1. A word cannot be split into two lines. 
 2. The order of words in the sentence must remain unchanged. 
 3. Two consecutive words in a line must be separated by a single space. 
 4. Total words in the sentence won't exceed 100. 
 5. Length of each word is greater than 0 and won't exceed 10. \
 6. 1 ≤ rows, cols ≤ 20,000.

您在真实的面试中是否遇到过这个题? Yes
样例
Given rows = 2, cols = 8, sentence = [“hello”, “world”], retrun 1.

Explanation:
hello---
world---

The character '-' signifies an empty space on the screen.

Given rows = 3, cols = 6, sentence = [“a”, “bcd”, “e”], return 2.

Explanation:
a-bcd- 
e-a---
bcd-e-

The character '-' signifies an empty space on the screen.

Given rows = 4, cols = 5, sentence = [“I”, “had”, “apple”, “pie”], return 1.

Explanation:
I-had
apple
pie-I
had--

The character '-' signifies an empty space on the screen.

思路
举个简单例子rows = 3, cols = 6,当前位置j=2
当在位置j可以存放字符串sentence[k]且还有位置存放下一个数组(可能) ,j的值变成j + sentence[k].size() + 1,若此时遍历完数组则从头开始遍历,否则继续向后遍历:
如a-b—或者a-bb–
当在位置j可以存放字符串sentence[k]但是并不剩余位置,直接到下一层,同时考虑数组的遍历情况:
如a-bcd- 或a-bcde
当在位置j存放不起字符串sentence[k],到下一层。
如a-bcdef超出界限

#ifndef C889_H
#define C889_H
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Solution {
public:
    /**
    * @param sentence: a list of string
    * @param rows: an integer
    * @param cols: an integer
    * @return: return an integer, denote times the given sentence can be fitted on the screen
    */
    int wordsTyping(vector<string> &sentence, int rows, int cols) {
        // Write your code here
        if (sentence.empty())
            return 0;
        int res = 0;
        int len = sentence.size();
        int i = 0, j = 0;
        int k = 0;
        //当在位置j可以存放字符串sentence[k]且还有位置存放下一个数组(可能)
        //j的值变成j + sentence[k].size() + 1,若此时遍历完数组则从头开始遍历,否则继续向后遍历
        //当在位置j可以存放字符串sentence[k]但是并不剩余位置
        //直接到下一层,同时考虑数组的遍历情况
        //当在位置j存放不起字符串sentence[k],到下一层
        while (i < rows)
        {
            if (j + sentence[k].size() < cols - 1)
            {
                j += sentence[k].size() + 1;
                if (k == len - 1)
                {
                    res++;
                    k = 0;
                }
                else
                    ++k;
            }
            else if (j + sentence[k].size() == cols || j + sentence[k].size() == cols - 1)
            {
                i++;
                j = 0;
                if (k == len - 1)
                {
                    res++;
                    k = 0;
                }
                else
                    ++k;
            }
            else
            {
                ++i;
                j = 0;
            }
        }
        return res;
    }
};
#endif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值