[Swift]LeetCode806. 写字符串需要的行数 | Number of Lines To Write String

We are to write the letters of a given string S, from left to right into lines. Each line has maximum width 100 units, and if writing a letter would cause the width of the line to exceed 100 units, it is written on the next line. We are given an array widths, an array where widths[0] is the width of ‘a‘, widths[1] is the width of ‘b‘, ..., and widths[25] is the width of ‘z‘.

Now answer two questions: how many lines have at least one character from S, and what is the width used by the last such line? Return your answer as an integer list of length 2.

Example :
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:
All letters have the same length of 10. To write all 26 letters,
we need two full lines and one line with 60 units.
Example :
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:
All letters except ‘a‘ have the same length of 10, and
"bbbcccdddaa" will cover 9 * 10 + 2 * 4 = 98 units.
For the last ‘a‘, it is written on the second line because
there is only 2 units left in the first line.
So the answer is 2 lines, plus 4 units in the second line. 

Note:

  • The length of S will be in the range [1, 1000].
  • S will only contain lowercase letters.
  • widths is an array of length 26.
  • widths[i] will be in the range of [2, 10].


我们要把给定的字符串 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]


Runtime: 8 ms

Memory Usage: 20 MB

 1 class Solution {
 2     func numberOfLines(_ widths: [Int], _ S: String) -> [Int] {
 3         var dic:[Character:Int] = Dictionary.init()
 4         let characters = Array.init("abcdefghijklmnopqrstuvwxyz")
 5         for (i,value) in widths.enumerated() {
 6             dic[characters[i]] = value
 7         }
 8         var result = 0
 9         var hang = 1
10         for c in S {
11             let a = result + dic[c]!
12             if a <= 100 {
13                 result += dic[c]!
14             } else {
15                 result = dic[c]!
16                 hang += 1
17             }
18         }
19         return [hang,result]
20     }
21 }


8ms

 1 class Solution {
 2     func numberOfLines(_ widths: [Int], _ S: String) -> [Int] {
 3         let chars = Array(S)
 4         var sum = 0
 5         var l = 0
 6         var last = 0
 7         for c in chars {
 8             let diff = widths[Int(c.unicodeScalars.first!.value - Character("a").unicodeScalars.first!.value)]
 9             sum += diff
10             if sum > 100 {
11                 l += 1
12                 last = diff
13                 sum = diff
14             } else if sum == 100 {
15                 l += 1
16                 last = diff
17                 sum = 0
18             } else {
19              last = sum
20             }
21         }
22         return [l+1,last]
23     }
24 }


12ms

 1 class Solution {
 2     func numberOfLines(_ widths: [Int], _ S: String) -> [Int] {
 3         var currentWidth = 0
 4         var lines = 1
 5         let a = "a".unicodeScalars.first!.value
 6         for letter in S.unicodeScalars {
 7             let width = widths[Int(letter.value - a)]
 8             if currentWidth + width > 100 {
 9                 lines += 1
10                 currentWidth = 0
11             }
12             currentWidth += width
13         }
14         return [lines, currentWidth]
15     }
16 }


32 ms

 1 class Solution {
 2     func numberOfLines(_ widths: [Int], _ S: String) -> [Int] {
 3         var cnt:Int = 1
 4         var cur:Int = 0
 5         for c in S
 6         {
 7             var t:Int = widths[c.ascii - 97]
 8             if cur + t > 100
 9             {
10                 cnt += 1
11             }
12             cur = (cur + t > 100) ? t : cur + t
13         }
14         return [cnt, cur]
15     }
16 }
17
18 //Character扩展
19 extension Character
20 {
21   //Character转ASCII整数值(定义小写为整数值)
22    var ascii: Int {
23        get {
24            return Int(self.unicodeScalars.first?.value ?? 0)
25        }
26     }
27 }

原文地址:https://www.cnblogs.com/strengthen/p/10548894.html

时间: 2024-07-31 12:35:10

[Swift]LeetCode806. 写字符串需要的行数 | Number of Lines To Write String的相关文章

CI中获取读操作的结果集行数+获取写操作的影响行数

本质:读操作,用mysql_num_rows函数,写操作用mysql_affected_rows函数 mysql_num_rows() 返回结果集中行的数目.此命令仅对 SELECT 语句有效.要取得被 INSERT,UPDATE 或者 DELETE 查询所影响到的行的数目,用 mysql_affected_rows(). CI中的方法: 读操作,获取行数: $query->num_rows() 该函数将会返回当前请求的行数.在本例子中, $query 表示当前 SQL 所产生的请求结果对象:

leetcode 434. 字符串中的单词数(Number of Segments in a String)

目录 题目描述: 示例: 解法: 题目描述: 统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符. 请注意,你可以假定字符串里不包括任何不可打印的字符. 示例: 输入: "Hello, my name is John" 输出: 5 解法: class Solution { public: int countSegments(string s) { int res = 0; int sz = s.size(); int i = 0, j = 0; while(i < sz

使用java读取文件夹中文件的行数

使用java统计某文件夹下所有文件的行数 经理突然交代一个任务:要求统计某个文件夹下所有文件的行数.在网上查了一个多小时没有解决.后来心里不爽就决定自己写一个java类用来统计文件的行数,于是花了两个小时将代码写出(可见我的java功底还是挺烂的).虽然有很多有待改进的地方,依然有纪念意义. 本java类的核心是通过BufferedReader类的readLine()方法,间接的统计行数:通过递归遍历文件. 这个类只是写来完成任务的.结果不是很严谨,许多情况并没考虑到:比如判断想读取某一类文件怎

【转载】程序员的成长和代码行数的关系

在2011年John D. Cook写了一篇博客,其中提到: 我的朋友Clift Norris发现了一个基本常数,我称之为Norris常数,一个未经培训的程序员在他或她遇到瓶颈之前能写出的平均代码量.Clift估计这个值是1500行.超过这个数以后,代码会变得如此混乱,以至于本人都无法轻而易举的进行调试和修改. 我还不了解足够多的初级程序员来验证这一结果,不过我自己认识到,程序员生涯的下一个瓶颈将发生在20,000行.我把Norris常数改成2,000那样正好变成十倍. 在我离开大学之后的第一份

删除log文件末尾中指定的行数

/// <summary>        /// 删除log文件末尾中指定的行数        /// </summary>        /// <param name="file">文件路径</param>        /// <param name="line">删除的行数</param>        public static void deleteLogToLine(string

代码行数统计

/** * Copyright ? 2015 All rights reserved. */ package cn.yufu.system.tools; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList;

shell 脚本---每隔几个小时查看日志文件中包含某些字符串的行数

在linux生产环境下,有如下这样的一个平常运维需要的工作. 每隔一段时间,比如两个小时,就要对每秒都要产生日志的日志文件(这里假设为testfile.out,其绝对路径为/home/panlm/shellpra/testfile.out)进行一个操作,这个操作是将日志中包含某些字符串的行给单独打印出来,并重新放在一个文件(这里的文件假设为out.log)中.这些需要匹配的字符串可以按这种方式表示"0x216000ab"其中ab为01到18的连续整数. 实现这样一个要求的做法主要有两步

c - 统计字符串&quot;字母,空格,数字,其他字符&quot;的个数和行数.

1 #include <stdio.h> 2 #include <ctype.h> 3 4 using namespace std; 5 6 /* 7 题目:输入一行字符,分别统计出其中英文字母.空格.数字和其它字符的个数. 8 */ 9 10 void 11 count() { 12 //统计个数. 13 int letters = 0; 14 int spaces = 0; 15 int digit = 0; 16 int others = 0; 17 char curChar

一个简单的代码计算行数demo编写

最近手头的项目基本上已经完结,历经了5个月的开发和迭代,各种的需求调整,想对自己的代码量进行一个客观的计算,于是抽了点时间写下了这个小demo,朋友们有需要的可以看看,很简单. 基本的思想就是:根目录->递归的遍历所有文件夹>计算具体某一文件的代码行数 具体流程如下: #pragma mark---------------------->递归文件夹下文件 - (void)recordFilePathWithPath:(NSString *)path{ /*        文件管理器