Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b194714

Browse files
Merge pull request SharingSource#462 from SharingSource/ac_oier
✨feat: Add 806
2 parents b83a7df + 40cf234 commit b194714

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
| [794. 有效的井字游戏](https://leetcode-cn.com/problems/valid-tic-tac-toe-state/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/valid-tic-tac-toe-state/solution/gong-shui-san-xie-fen-qing-kuang-tao-lun-pikn/) | 中等 | 🤩🤩🤩🤩 |
8888
| [796. 旋转字符串](https://leetcode-cn.com/problems/rotate-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/rotate-string/solution/by-ac_oier-bnkx/) | 简单 | 🤩🤩🤩 |
8989
| [804. 唯一摩尔斯密码词](https://leetcode-cn.com/problems/unique-morse-code-words/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/unique-morse-code-words/solution/by-ac_oier-a9hv/) | 简单 | 🤩🤩🤩 |
90+
| [806. 写字符串需要的行数](https://leetcode-cn.com/problems/number-of-lines-to-write-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-lines-to-write-string/solution/by-ac_oier-5hg2/) | 简单 | 🤩🤩🤩🤩 |
9091
| [846. 一手顺子](https://leetcode-cn.com/problems/hand-of-straights/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/hand-of-straights/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-4hxw/) | 中等 | 🤩🤩🤩 |
9192
| [859. 亲密字符串](https://leetcode-cn.com/problems/buddy-strings/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/buddy-strings/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-q056/) | 简单 | 🤩🤩🤩🤩🤩 |
9293
| [867. 转置矩阵](https://leetcode-cn.com/problems/transpose-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/transpose-matrix/solution/yi-you-wei-jin-huo-xu-ni-neng-kan-kan-zh-m53m/) | 简单 | 🤩🤩🤩🤩 |
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[806. 写字符串需要的行数](https://leetcode-cn.com/problems/number-of-lines-to-write-string/solution/by-ac_oier-5hg2/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
我们要把给定的字符串 `S` 从左到右写到每一行上,每一行的最大宽度为 100ドル$ 个单位,如果我们在写某个字母的时候会使这行超过了 100ドル$ 个单位,那么我们应该把这个字母写到下一行。
10+
11+
我们给定了一个数组 `widths`,这个数组 $widths[0]$ 代表 `'a'` 需要的单位, $widths[1]$ 代表 `'b'` 需要的单位,..., $widths[25]$ 代表 `'z'` 需要的单位。
12+
13+
现在回答两个问题:至少多少行能放下 `S`,以及最后一行使用的宽度是多少个单位?
14+
15+
将你的答案作为长度为 2ドル$ 的整数列表返回。
16+
17+
示例 1:
18+
```
19+
输入:
20+
widths = [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
21+
S = "abcdefghijklmnopqrstuvwxyz"
22+
23+
输出: [3, 60]
24+
25+
解释:
26+
所有的字符拥有相同的占用单位10。所以书写所有的26个字母,
27+
我们需要2个整行和占用60个单位的一行。
28+
```
29+
示例 2:
30+
```
31+
输入:
32+
widths = [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
33+
S = "bbbcccdddaaa"
34+
35+
输出: [2, 4]
36+
37+
解释:
38+
除去字母'a'所有的字符都是相同的单位10,并且字符串 "bbbcccdddaa" 将会覆盖 9 * 10 + 2 * 4 = 98 个单位.
39+
最后一个字母 'a' 将会被写到第二行,因为第一行只剩下2个单位了。
40+
所以,这个答案是2行,第二行有4个单位宽度。
41+
```
42+
43+
注:
44+
* 字符串 `S` 的长度在 $[1, 1000]$ 的范围。
45+
* `S` 只包含小写字母。
46+
* `widths` 是长度为 26ドル$ 的数组。
47+
* $widths[i]$ 值的范围在 $[2, 10]$。
48+
49+
---
50+
51+
### 模拟
52+
53+
根据题意进行模拟即可。
54+
55+
使用变量 `a` 代指当前有多少行是满的,使用变量 `b` 代指当前填充光标所在的位置。
56+
57+
代码:
58+
```Java
59+
class Solution {
60+
public int[] numberOfLines(int[] widths, String s) {
61+
int a = 0, b = 0;
62+
for (char c : s.toCharArray()) {
63+
int t = widths[c - 'a'];
64+
if (b + t > 100 && ++a >= 0) b = t;
65+
else b += t;
66+
}
67+
if (b != 0) a++;
68+
return new int[]{a, b};
69+
}
70+
}
71+
```
72+
* 时间复杂度:$O(n)$
73+
* 空间复杂度:不使用 `toCharArray` 为 $O(1),ドル否则为 $O(n)$
74+
75+
---
76+
77+
### 最后
78+
79+
这是我们「刷穿 LeetCode」系列文章的第 `No.804` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
80+
81+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
82+
83+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
84+
85+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
86+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /