返回介绍

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

发布于 2024-06-17 01:03:34 字数 4539 浏览 0 评论 0 收藏 0

806. Number of Lines To Write String

中文文档

Description

You are given a string s of lowercase English letters and an array widths denoting how many pixels wide each lowercase English letter is. Specifically, widths[0] is the width of 'a', widths[1] is the width of 'b', and so on.

You are trying to write s across several lines, where each line is no longer than 100 pixels. Starting at the beginning of s, write as many letters on the first line such that the total width does not exceed 100 pixels. Then, from where you stopped in s, continue writing as many letters as you can on the second line. Continue this process until you have written all of s.

Return _an array _result_ of length 2 where:_

  • result[0]_ is the total number of lines._
  • result[1]_ is the width of the last line in pixels._

 

Example 1:

Input: 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], s = "abcdefghijklmnopqrstuvwxyz"
Output: [3,60]
Explanation: You can write s as follows:
abcdefghij  // 100 pixels wide
klmnopqrst  // 100 pixels wide
uvwxyz    // 60 pixels wide
There are a total of 3 lines, and the last line is 60 pixels wide.

Example 2:

Input: 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], s = "bbbcccdddaaa"
Output: [2,4]
Explanation: You can write s as follows:
bbbcccdddaa  // 98 pixels wide
a      // 4 pixels wide
There are a total of 2 lines, and the last line is 4 pixels wide.

 

Constraints:

  • widths.length == 26
  • 2 <= widths[i] <= 10
  • 1 <= s.length <= 1000
  • s contains only lowercase English letters.

Solutions

Solution 1

class Solution:
  def numberOfLines(self, widths: List[int], s: str) -> List[int]:
    last, row = 0, 1
    for c in s:
      w = widths[ord(c) - ord('a')]
      if last + w <= 100:
        last += w
      else:
        row += 1
        last = w
    return [row, last]
class Solution {
  private static final int MAX_WIDTH = 100;

  public int[] numberOfLines(int[] widths, String s) {
    int last = 0, row = 1;
    for (char c : s.toCharArray()) {
      int w = widths[c - 'a'];
      if (last + w <= MAX_WIDTH) {
        last += w;
      } else {
        ++row;
        last = w;
      }
    }
    return new int[] {row, last};
  }
}
class Solution {
public:
  const int MAX_WIDTH = 100;

  vector<int> numberOfLines(vector<int>& widths, string s) {
    int last = 0, row = 1;
    for (char c : s) {
      int w = widths[c - 'a'];
      if (last + w <= MAX_WIDTH)
        last += w;
      else {
        ++row;
        last = w;
      }
    }
    return {row, last};
  }
};
func numberOfLines(widths []int, s string) []int {
  last, row := 0, 1
  for _, c := range s {
    w := widths[c-'a']
    if last+w <= 100 {
      last += w
    } else {
      row++
      last = w
    }
  }
  return []int{row, last}
}
impl Solution {
  pub fn number_of_lines(widths: Vec<i32>, s: String) -> Vec<i32> {
    let mut count = 1;
    let mut sum = 0;
    for c in s.as_bytes() {
      let width = widths[(c - b'a') as usize];
      if sum + width > 100 {
        sum = 0;
        count += 1;
      }
      sum += width;
    }
    vec![count, sum]
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文