From 40cf234ec28078316cb121edc2c4848a8f62931e Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Tue, 12 Apr 2022 10:22:08 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20Add=20806?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "Index/\346\250\241\346\213\237.md" | 1 + ...10\347\256\200\345\215\225\357\274\211.md" | 86 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 "LeetCode/801-810/806. \345\206\231\345\255\227\347\254\246\344\270\262\351\234\200\350\246\201\347\232\204\350\241\214\346\225\260\357\274\210\347\256\200\345\215\225\357\274\211.md" diff --git "a/Index/\346\250\241\346\213\237.md" "b/Index/\346\250\241\346\213\237.md" index 4cea1ff8..78f6f930 100644 --- "a/Index/\346\250\241\346\213\237.md" +++ "b/Index/\346\250\241\346\213\237.md" @@ -87,6 +87,7 @@ | [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/) | 中等 | 🤩🤩🤩🤩 | | [796. 旋转字符串](https://leetcode-cn.com/problems/rotate-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/rotate-string/solution/by-ac_oier-bnkx/) | 简单 | 🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩 | +| [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | diff --git "a/LeetCode/801-810/806. \345\206\231\345\255\227\347\254\246\344\270\262\351\234\200\350\246\201\347\232\204\350\241\214\346\225\260\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/801-810/806. \345\206\231\345\255\227\347\254\246\344\270\262\351\234\200\350\246\201\347\232\204\350\241\214\346\225\260\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..65cc9600 --- /dev/null +++ "b/LeetCode/801-810/806. \345\206\231\345\255\227\347\254\246\344\270\262\351\234\200\350\246\201\347\232\204\350\241\214\346\225\260\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -0,0 +1,86 @@ +### 题目描述 + +这是 LeetCode 上的 **[806. 写字符串需要的行数](https://leetcode-cn.com/problems/number-of-lines-to-write-string/solution/by-ac_oier-5hg2/)** ,难度为 **简单**。 + +Tag : 「模拟」 + + + +我们要把给定的字符串 `S` 从左到右写到每一行上,每一行的最大宽度为 $100$ 个单位,如果我们在写某个字母的时候会使这行超过了 $100$ 个单位,那么我们应该把这个字母写到下一行。 + +我们给定了一个数组 `widths`,这个数组 $widths[0]$ 代表 `'a'` 需要的单位, $widths[1]$ 代表 `'b'` 需要的单位,..., $widths[25]$ 代表 `'z'` 需要的单位。 + +现在回答两个问题:至少多少行能放下 `S`,以及最后一行使用的宽度是多少个单位? + +将你的答案作为长度为 $2$ 的整数列表返回。 + +示例 1: +``` +输入: +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" + +输出: [3, 60] + +解释: +所有的字符拥有相同的占用单位10。所以书写所有的26个字母, +我们需要2个整行和占用60个单位的一行。 +``` +示例 2: +``` +输入: +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" + +输出: [2, 4] + +解释: +除去字母'a'所有的字符都是相同的单位10,并且字符串 "bbbcccdddaa" 将会覆盖 9 * 10 + 2 * 4 = 98 个单位. +最后一个字母 'a' 将会被写到第二行,因为第一行只剩下2个单位了。 +所以,这个答案是2行,第二行有4个单位宽度。 +``` + +注: +* 字符串 `S` 的长度在 $[1, 1000]$ 的范围。 +* `S` 只包含小写字母。 +* `widths` 是长度为 $26$ 的数组。 +* $widths[i]$ 值的范围在 $[2, 10]$。 + +--- + +### 模拟 + +根据题意进行模拟即可。 + +使用变量 `a` 代指当前有多少行是满的,使用变量 `b` 代指当前填充光标所在的位置。 + +代码: +```Java +class Solution { + public int[] numberOfLines(int[] widths, String s) { + int a = 0, b = 0; + for (char c : s.toCharArray()) { + int t = widths[c - 'a']; + if (b + t > 100 && ++a >= 0) b = t; + else b += t; + } + if (b != 0) a++; + return new int[]{a, b}; + } +} +``` +* 时间复杂度:$O(n)$ +* 空间复杂度:不使用 `toCharArray` 为 $O(1)$,否则为 $O(n)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.804` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +