Skip to content

Commit ca86e35

Browse files
authored
feat: add solutions to lc problem: No.806 (doocs#2508)
No.0806.Number of Lines To Write String
1 parent f02695d commit ca86e35

File tree

8 files changed

+155
-97
lines changed

8 files changed

+155
-97
lines changed

solution/0800-0899/0806.Number of Lines To Write String/README.md

+55-32
Original file line numberDiff line numberDiff line change
@@ -50,93 +50,116 @@ S = "bbbcccdddaaa"
5050

5151
### 方法一:模拟
5252

53+
我们定义两个变量 `lines``last`,分别表示行数和最后一行的宽度,初始时 `lines = 1``last = 0`
54+
55+
遍历字符串 $s$,对于每个字符 $c$,计算其宽度 $w$,如果 $last + w \leq 100$,则将 $w$ 加到 `last` 上,否则行数 `lines` 加一,并且 `last` 重置为 $w$。
56+
57+
最后返回 `lines``last` 构成的数组。
58+
59+
时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$。
60+
5361
<!-- tabs:start -->
5462

5563
```python
5664
class Solution:
5765
def numberOfLines(self, widths: List[int], s: str) -> List[int]:
58-
last, row = 0, 1
59-
for c in s:
60-
w = widths[ord(c) - ord('a')]
66+
lines, last = 1, 0
67+
for w in map(lambda c: widths[ord(c) - ord("a")], s):
6168
if last + w <= 100:
6269
last += w
6370
else:
64-
row += 1
71+
lines += 1
6572
last = w
66-
return [row, last]
73+
return [lines, last]
6774
```
6875

6976
```java
7077
class Solution {
71-
private static final int MAX_WIDTH = 100;
72-
7378
public int[] numberOfLines(int[] widths, String s) {
74-
int last = 0, row = 1;
75-
for (char c : s.toCharArray()) {
76-
int w = widths[c - 'a'];
77-
if (last + w <= MAX_WIDTH) {
79+
int lines = 1, last = 0;
80+
for (int i = 0; i < s.length(); ++i) {
81+
int w = widths[s.charAt(i) - 'a'];
82+
if (last + w <= 100) {
7883
last += w;
7984
} else {
80-
++row;
85+
++lines;
8186
last = w;
8287
}
8388
}
84-
return new int[] {row, last};
89+
return new int[] {lines, last};
8590
}
8691
}
8792
```
8893

8994
```cpp
9095
class Solution {
9196
public:
92-
const int MAX_WIDTH = 100;
93-
9497
vector<int> numberOfLines(vector<int>& widths, string s) {
95-
int last = 0, row = 1;
98+
int lines = 1, last = 0;
9699
for (char c : s) {
97100
int w = widths[c - 'a'];
98-
if (last + w <= MAX_WIDTH)
101+
if (last + w <= 100) {
99102
last += w;
100-
else {
101-
++row;
103+
} else {
104+
++lines;
102105
last = w;
103106
}
104107
}
105-
return {row, last};
108+
return {lines, last};
106109
}
107110
};
108111
```
109112
110113
```go
111114
func numberOfLines(widths []int, s string) []int {
112-
last, row := 0, 1
115+
lines, last := 1, 0
113116
for _, c := range s {
114117
w := widths[c-'a']
115118
if last+w <= 100 {
116119
last += w
117120
} else {
118-
row++
121+
lines++
119122
last = w
120123
}
121124
}
122-
return []int{row, last}
125+
return []int{lines, last}
126+
}
127+
```
128+
129+
```ts
130+
function numberOfLines(widths: number[], s: string): number[] {
131+
let [lines, last] = [1, 0];
132+
for (const c of s) {
133+
const w = widths[c.charCodeAt(0) - 'a'.charCodeAt(0)];
134+
if (last + w <= 100) {
135+
last += w;
136+
} else {
137+
++lines;
138+
last = w;
139+
}
140+
}
141+
return [lines, last];
123142
}
124143
```
125144

126145
```rust
127146
impl Solution {
128147
pub fn number_of_lines(widths: Vec<i32>, s: String) -> Vec<i32> {
129-
let mut count = 1;
130-
let mut sum = 0;
131-
for c in s.as_bytes() {
132-
let width = widths[(c - b'a') as usize];
133-
if sum + width > 100 {
134-
sum = 0;
135-
count += 1;
148+
let mut lines = 1;
149+
let mut last = 0;
150+
151+
for c in s.chars() {
152+
let idx = ((c as u8) - b'a') as usize;
153+
let w = widths[idx];
154+
if last + w <= 100 {
155+
last += w;
156+
} else {
157+
lines += 1;
158+
last = w;
136159
}
137-
sum += width;
138160
}
139-
vec![count, sum]
161+
162+
vec![lines, last]
140163
}
141164
}
142165
```

solution/0800-0899/0806.Number of Lines To Write String/README_EN.md

+56-33
Original file line numberDiff line numberDiff line change
@@ -51,95 +51,118 @@ There are a total of 2 lines, and the last line is 4 pixels wide.</pre>
5151

5252
## Solutions
5353

54-
### Solution 1
54+
### Solution 1: Simulation
55+
56+
We define two variables `lines` and `last`, representing the number of lines and the width of the last line, respectively. Initially, `lines = 1` and `last = 0`.
57+
58+
We iterate through the string $s$. For each character $c$, we calculate its width $w$. If $last + w \leq 100$, we add $w$ to `last`. Otherwise, we increment `lines` by one and reset `last` to $w$.
59+
60+
Finally, we return an array consisting of `lines` and `last`.
61+
62+
The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$.
5563

5664
<!-- tabs:start -->
5765

5866
```python
5967
class Solution:
6068
def numberOfLines(self, widths: List[int], s: str) -> List[int]:
61-
last, row = 0, 1
62-
for c in s:
63-
w = widths[ord(c) - ord('a')]
69+
lines, last = 1, 0
70+
for w in map(lambda c: widths[ord(c) - ord("a")], s):
6471
if last + w <= 100:
6572
last += w
6673
else:
67-
row += 1
74+
lines += 1
6875
last = w
69-
return [row, last]
76+
return [lines, last]
7077
```
7178

7279
```java
7380
class Solution {
74-
private static final int MAX_WIDTH = 100;
75-
7681
public int[] numberOfLines(int[] widths, String s) {
77-
int last = 0, row = 1;
78-
for (char c : s.toCharArray()) {
79-
int w = widths[c - 'a'];
80-
if (last + w <= MAX_WIDTH) {
82+
int lines = 1, last = 0;
83+
for (int i = 0; i < s.length(); ++i) {
84+
int w = widths[s.charAt(i) - 'a'];
85+
if (last + w <= 100) {
8186
last += w;
8287
} else {
83-
++row;
88+
++lines;
8489
last = w;
8590
}
8691
}
87-
return new int[] {row, last};
92+
return new int[] {lines, last};
8893
}
8994
}
9095
```
9196

9297
```cpp
9398
class Solution {
9499
public:
95-
const int MAX_WIDTH = 100;
96-
97100
vector<int> numberOfLines(vector<int>& widths, string s) {
98-
int last = 0, row = 1;
101+
int lines = 1, last = 0;
99102
for (char c : s) {
100103
int w = widths[c - 'a'];
101-
if (last + w <= MAX_WIDTH)
104+
if (last + w <= 100) {
102105
last += w;
103-
else {
104-
++row;
106+
} else {
107+
++lines;
105108
last = w;
106109
}
107110
}
108-
return {row, last};
111+
return {lines, last};
109112
}
110113
};
111114
```
112115
113116
```go
114117
func numberOfLines(widths []int, s string) []int {
115-
last, row := 0, 1
118+
lines, last := 1, 0
116119
for _, c := range s {
117120
w := widths[c-'a']
118121
if last+w <= 100 {
119122
last += w
120123
} else {
121-
row++
124+
lines++
122125
last = w
123126
}
124127
}
125-
return []int{row, last}
128+
return []int{lines, last}
129+
}
130+
```
131+
132+
```ts
133+
function numberOfLines(widths: number[], s: string): number[] {
134+
let [lines, last] = [1, 0];
135+
for (const c of s) {
136+
const w = widths[c.charCodeAt(0) - 'a'.charCodeAt(0)];
137+
if (last + w <= 100) {
138+
last += w;
139+
} else {
140+
++lines;
141+
last = w;
142+
}
143+
}
144+
return [lines, last];
126145
}
127146
```
128147

129148
```rust
130149
impl Solution {
131150
pub fn number_of_lines(widths: Vec<i32>, s: String) -> Vec<i32> {
132-
let mut count = 1;
133-
let mut sum = 0;
134-
for c in s.as_bytes() {
135-
let width = widths[(c - b'a') as usize];
136-
if sum + width > 100 {
137-
sum = 0;
138-
count += 1;
151+
let mut lines = 1;
152+
let mut last = 0;
153+
154+
for c in s.chars() {
155+
let idx = ((c as u8) - b'a') as usize;
156+
let w = widths[idx];
157+
if last + w <= 100 {
158+
last += w;
159+
} else {
160+
lines += 1;
161+
last = w;
139162
}
140-
sum += width;
141163
}
142-
vec![count, sum]
164+
165+
vec![lines, last]
143166
}
144167
}
145168
```
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
class Solution {
22
public:
3-
const int MAX_WIDTH = 100;
4-
53
vector<int> numberOfLines(vector<int>& widths, string s) {
6-
int last = 0, row = 1;
4+
int lines = 1, last = 0;
75
for (char c : s) {
86
int w = widths[c - 'a'];
9-
if (last + w <= MAX_WIDTH)
7+
if (last + w <= 100) {
108
last += w;
11-
else {
12-
++row;
9+
} else {
10+
++lines;
1311
last = w;
1412
}
1513
}
16-
return {row, last};
14+
return {lines, last};
1715
}
1816
};
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
func numberOfLines(widths []int, s string) []int {
2-
last, row := 0, 1
2+
lines, last := 1, 0
33
for _, c := range s {
44
w := widths[c-'a']
55
if last+w <= 100 {
66
last += w
77
} else {
8-
row++
8+
lines++
99
last = w
1010
}
1111
}
12-
return []int{row, last}
12+
return []int{lines, last}
1313
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
class Solution {
2-
private static final int MAX_WIDTH = 100;
3-
42
public int[] numberOfLines(int[] widths, String s) {
5-
int last = 0, row = 1;
6-
for (char c : s.toCharArray()) {
7-
int w = widths[c - 'a'];
8-
if (last + w <= MAX_WIDTH) {
3+
int lines = 1, last = 0;
4+
for (int i = 0; i < s.length(); ++i) {
5+
int w = widths[s.charAt(i) - 'a'];
6+
if (last + w <= 100) {
97
last += w;
108
} else {
11-
++row;
9+
++lines;
1210
last = w;
1311
}
1412
}
15-
return new int[] {row, last};
13+
return new int[] {lines, last};
1614
}
1715
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
class Solution:
22
def numberOfLines(self, widths: List[int], s: str) -> List[int]:
3-
last, row = 0, 1
4-
for c in s:
5-
w = widths[ord(c) - ord('a')]
3+
lines, last = 1, 0
4+
for w in map(lambda c: widths[ord(c) - ord("a")], s):
65
if last + w <= 100:
76
last += w
87
else:
9-
row += 1
8+
lines += 1
109
last = w
11-
return [row, last]
10+
return [lines, last]

0 commit comments

Comments
 (0)