Skip to content

Commit f5c6019

Browse files
committed
feat: add solutions to lc problem: No.0806
No.0806.Number of Lines To Write String
1 parent ef5d5cc commit f5c6019

File tree

6 files changed

+200
-2
lines changed

6 files changed

+200
-2
lines changed

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

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,92 @@ S = "bbbcccdddaaa"
4848

4949
<!-- 这里可写通用的实现逻辑 -->
5050

51+
**方法一:模拟**
52+
5153
<!-- tabs:start -->
5254

5355
### **Python3**
5456

5557
<!-- 这里可写当前语言的特殊实现逻辑 -->
5658

5759
```python
58-
60+
class Solution:
61+
def numberOfLines(self, widths: List[int], s: str) -> List[int]:
62+
last, row = 0, 1
63+
for c in s:
64+
w = widths[ord(c) - ord('a')]
65+
if last + w <= 100:
66+
last += w
67+
else:
68+
row += 1
69+
last = w
70+
return [row, last]
5971
```
6072

6173
### **Java**
6274

6375
<!-- 这里可写当前语言的特殊实现逻辑 -->
6476

6577
```java
78+
class Solution {
79+
private static final int MAX_WIDTH = 100;
80+
81+
public int[] numberOfLines(int[] widths, String s) {
82+
int last = 0, row = 1;
83+
for (char c : s.toCharArray()) {
84+
int w = widths[c - 'a'];
85+
if (last + w <= MAX_WIDTH) {
86+
last += w;
87+
} else {
88+
++row;
89+
last = w;
90+
}
91+
}
92+
return new int[]{row, last};
93+
}
94+
}
95+
```
96+
97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
const int MAX_WIDTH = 100;
103+
104+
vector<int> numberOfLines(vector<int>& widths, string s) {
105+
int last = 0, row = 1;
106+
for (char c : s)
107+
{
108+
int w = widths[c - 'a'];
109+
if (last + w <= MAX_WIDTH) last += w;
110+
else
111+
{
112+
++row;
113+
last = w;
114+
}
115+
}
116+
return {row, last};
117+
}
118+
};
119+
```
66120
121+
### **Go**
122+
123+
```go
124+
func numberOfLines(widths []int, s string) []int {
125+
last, row := 0, 1
126+
for _, c := range s {
127+
w := widths[c-'a']
128+
if last+w <= 100 {
129+
last += w
130+
} else {
131+
row++
132+
last = w
133+
}
134+
}
135+
return []int{row, last}
136+
}
67137
```
68138

69139
### **...**

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

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,81 @@ There are a total of 2 lines, and the last line is 4 pixels wide.</pre>
5454
### **Python3**
5555

5656
```python
57-
57+
class Solution:
58+
def numberOfLines(self, widths: List[int], s: str) -> List[int]:
59+
last, row = 0, 1
60+
for c in s:
61+
w = widths[ord(c) - ord('a')]
62+
if last + w <= 100:
63+
last += w
64+
else:
65+
row += 1
66+
last = w
67+
return [row, last]
5868
```
5969

6070
### **Java**
6171

6272
```java
73+
class Solution {
74+
private static final int MAX_WIDTH = 100;
75+
76+
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) {
81+
last += w;
82+
} else {
83+
++row;
84+
last = w;
85+
}
86+
}
87+
return new int[]{row, last};
88+
}
89+
}
90+
```
91+
92+
### **C++**
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
const int MAX_WIDTH = 100;
98+
99+
vector<int> numberOfLines(vector<int>& widths, string s) {
100+
int last = 0, row = 1;
101+
for (char c : s)
102+
{
103+
int w = widths[c - 'a'];
104+
if (last + w <= MAX_WIDTH) last += w;
105+
else
106+
{
107+
++row;
108+
last = w;
109+
}
110+
}
111+
return {row, last};
112+
}
113+
};
114+
```
63115
116+
### **Go**
117+
118+
```go
119+
func numberOfLines(widths []int, s string) []int {
120+
last, row := 0, 1
121+
for _, c := range s {
122+
w := widths[c-'a']
123+
if last+w <= 100 {
124+
last += w
125+
} else {
126+
row++
127+
last = w
128+
}
129+
}
130+
return []int{row, last}
131+
}
64132
```
65133

66134
### **...**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
const int MAX_WIDTH = 100;
4+
5+
vector<int> numberOfLines(vector<int>& widths, string s) {
6+
int last = 0, row = 1;
7+
for (char c : s)
8+
{
9+
int w = widths[c - 'a'];
10+
if (last + w <= MAX_WIDTH) last += w;
11+
else
12+
{
13+
++row;
14+
last = w;
15+
}
16+
}
17+
return {row, last};
18+
}
19+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func numberOfLines(widths []int, s string) []int {
2+
last, row := 0, 1
3+
for _, c := range s {
4+
w := widths[c-'a']
5+
if last+w <= 100 {
6+
last += w
7+
} else {
8+
row++
9+
last = w
10+
}
11+
}
12+
return []int{row, last}
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
private static final int MAX_WIDTH = 100;
3+
4+
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) {
9+
last += w;
10+
} else {
11+
++row;
12+
last = w;
13+
}
14+
}
15+
return new int[]{row, last};
16+
}
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
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')]
6+
if last + w <= 100:
7+
last += w
8+
else:
9+
row += 1
10+
last = w
11+
return [row, last]

0 commit comments

Comments
 (0)