You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0800-0899/0806.Number of Lines To Write String/README_EN.md
+56-33
Original file line number
Diff line number
Diff line change
@@ -51,95 +51,118 @@ There are a total of 2 lines, and the last line is 4 pixels wide.</pre>
51
51
52
52
## Solutions
53
53
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)$.
0 commit comments