Sentence Screen Fitting
Given a rows x cols screen and a sentence represented by a list of words, find how many times the given sentence can be fitted on the screen.
Note:
- A word cannot be split into two lines.
- The order of words in the sentence must remain unchanged.
- Two consecutive words in a line must be separated by a single space.
- Total words in the sentence won't exceed 100.
- Length of each word won't exceed 10.
- 1 ≤ rows, cols ≤ 20,000.
Example 1:
Input:
rows = 2, cols = 8, sentence = ["hello", "world"] Output:
1 Explanation:
hello---
world--- The character '-' signifies an empty space on the screen.
Example 2:
Input:
rows = 3, cols = 6, sentence = ["a", "bcd", "e"] Output:
2 Explanation:
a-bcd-
e-a---
bcd-e- The character '-' signifies an empty space on the screen.
Example 3:
Input:
rows = 4, cols = 5, sentence = ["I", "had", "apple", "pie"] Output:
1 Explanation:
I-had
apple
pie-I
had-- The character '-' signifies an empty space on the screen.
分析:
统计加空格的句子总长度,然后遍历每一行,初始化colsRemaining为cols,然后还需要一个变量idx,来记录当前单词的位置,如果colsRemaining大于0,就进行while循环,如果当前单词的长度小于等于colsRemaining,说明可以放下该单词,那么就减去该单词的长度就是剩余的空间,然后如果此时colsRemaining仍然大于0,则减去空格的长度1,然后idx自增1,如果idx此时超过单词个数的范围了,说明一整句可以放下,那么就有可能出现宽度远大于句子长度的情况,所以我们加上之前放好的一句之外,还要加上colsRemaining/len的个数,然后colsRemaining%len是剩余的位置,此时idx重置为0.
class Solution {
int wordsTyping(String[] sentence, int rows, int cols) {
String all = "";
for (String word : sentence) {
all += (word + " ");
}
int res = , idx = , n = sentence.length, len = all.length();
for (int i = ; i < rows; ++i) {
int colsRemaining = cols;
while (colsRemaining > ) {
if (sentence[idx].length() <= colsRemaining) {
colsRemaining -= sentence[idx].length();
if (colsRemaining > ) {
colsRemaining -= ;
}
if (++idx >= n) {
res += ( + colsRemaining / len);
colsRemaining %= len;
idx = ;
}
} else {
break;
}
}
}
return res;
}
}
Sentence Screen Fitting的更多相关文章
- [LeetCode] Sentence Screen Fitting 调整屏幕上的句子
Given a rows x cols screen and a sentence represented by a list of words, find how many times the gi ...
- Leetcode: Sentence Screen Fitting
Given a rows x cols screen and a sentence represented by a list of words, find how many times the gi ...
- 418. Sentence Screen Fitting
首先想到的是直接做,然后TLE. public class Solution { public int wordsTyping(String[] sentence, int rows, int col ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)
All LeetCode Questions List 题目汇总 Sorted by frequency of problems that appear in real interviews. Las ...
- LeetCode All in One 题目讲解汇总(转...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...
- 断电不断网——Linux的screen
title: 断电不断网--Linux的screen author:青南 date: 2015-01-01 20:20:23 categories: [Linux] tags: [linux,scre ...
随机推荐
- Web大文件分片上传
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 先说下要求: PC端全平台支持,要求支持Windows,Mac,Linux 支持所 ...
- 在一个非默认的位置包含头文件stdafx.h
如果stdafx.h和你当前的工程不在一个文件夹下,当你在代码中第一行写下#include "stdafx.h"时,VC编译器会根据编译规则(相关的规则请查看MSDN)来区别std ...
- 数论之同余性质 线性同余方程&拔山盖世BSGS&中国剩余定理
先记录一下一些概念和定理 同余:给定整数a,b,c,若用c不停的去除a和b最终所得余数一样,则称a和b对模c同余,记做a≡b (mod c),同余满足自反性,对称性,传递性 定理1: 若a≡b (mo ...
- [CSP-S模拟测试]:A(单调栈维护凸包+二分答案)
题目传送门(内部题150) 输入格式 第一行两个整数$N,Q$. 接下来的$N$行,每行两个整数$a_i,b_i$. 接下来的$Q$行,每行一个整数$x$. 输出格式 对于每个询问,输出一行一个整数表 ...
- dcokee 安装 nginx
1,docker pull openresty/openresty 2, mkdir /opt/local/openresty 等等文件夹 3, docker run --name=" ...
- 胜利点 final发布
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2019fall/homework/10062 1.视频地址:https://www.bilibili.com/ ...
- python中list不能做索引
先看python中内置的list不能作为字典的key. 可将list或者ndarray转化为tuple再做索引. list不能进行hash: import numpy as np a1 = np.ar ...
- AS中集成bug管理系统
这里大家可以选择各种Bug管理工具,几乎包括了市面上常用的各种Bug跟踪管理工具. 由于如:点击JIRA,填入公司JIRA服务器的地址,填入Server.Username和密码即可,点击Test,弹出 ...
- 互斥锁lock、信号量semaphore、事件Event、
1.互斥锁lock 应用在多进程中互斥所lock:互斥锁是进程间的get_ticket互相排斥进程之间,谁先枪占到资源,谁就先上锁,等到解锁之后,下一个进程在继续使用.# 语法: 上锁: lock.a ...
- Selenium 2自动化测试实战38(整合自动发邮件功能)
整合自动发邮件功能 解决了前面的问题后,现在就可以将自动发邮件功能集成到自动化测试项目中了.下面重新编辑runtest.py文件 #runtest.py #coding:utf-8 from HTML ...