zoukankan      html  css  js  c++  java
  • [LeetCode] 1945. Sum of Digits of String After Convert

    You are given a string s consisting of lowercase English letters, and an integer k.

    First, convert s into an integer by replacing each letter with its position in the alphabet (i.e., replace 'a' with 1'b' with 2, ..., 'z' with 26). Then, transform the integer by replacing it with the sum of its digits. Repeat the transform operation k times in total.

    For example, if s = "zbax" and k = 2, then the resulting integer would be 8 by the following operations:

    • Convert: "zbax" ➝ "(26)(2)(1)(24)" ➝ "262124" ➝ 262124
    • Transform #1: 262124 ➝ 2 + 6 + 2 + 1 + 2 + 4 ➝ 17
    • Transform #2: 17 ➝ 1 + 7 ➝ 8

    Return the resulting integer after performing the operations described above.

    Example 1:

    Input: s = "iiii", k = 1
    Output: 36
    Explanation: The operations are as follows:
    - Convert: "iiii" ➝ "(9)(9)(9)(9)" ➝ "9999" ➝ 9999
    - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36
    Thus the resulting integer is 36.
    

    Example 2:

    Input: s = "leetcode", k = 2
    Output: 6
    Explanation: The operations are as follows:
    - Convert: "leetcode" ➝ "(12)(5)(5)(20)(3)(15)(4)(5)" ➝ "12552031545" ➝ 12552031545
    - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33
    - Transform #2: 33 ➝ 3 + 3 ➝ 6
    Thus the resulting integer is 6.
    

    Example 3:

    Input: s = "zbax", k = 2
    Output: 8

    Constraints:

    • 1 <= s.length <= 100
    • 1 <= k <= 10
    • s consists of lowercase English letters.

    字符串转化后的各位数字之和。

    给你一个由小写字母组成的字符串 s ,以及一个整数 k 。

    首先,用字母在字母表中的位置替换该字母,将 s 转化 为一个整数(也就是,'a' 用 1 替换,'b' 用 2 替换,... 'z' 用 26 替换)。接着,将整数 转换 为其 各位数字之和 。共重复 转换 操作 k 次 。

    例如,如果 s = "zbax" 且 k = 2 ,那么执行下述步骤后得到的结果是整数 8 :

    转化:"zbax" ➝ "(26)(2)(1)(24)" ➝ "262124" ➝ 262124
    转换 #1:262124 ➝ 2 + 6 + 2 + 1 + 2 + 4 ➝ 17
    转换 #2:17 ➝ 1 + 7 ➝ 8
    返回执行上述操作后得到的结果整数。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/sum-of-digits-of-string-after-convert
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    这是一道字符串的模拟题,直接根据题意实现即可。因为步骤较多,这道题还是值得一做的。

    时间O(n)

    空间O(n)

    Java实现

     1 class Solution {
     2     public int getLucky(String s, int k) {
     3         StringBuilder sb = new StringBuilder();
     4         for (char c : s.toCharArray()) {
     5             int val = c - 'a' + 1;
     6             sb.append(val);
     7         }
     8 
     9         String numString = sb.toString();
    10         int kk = k;
    11         int sum = 0;
    12         while (kk != 0) {
    13             sum = 0;
    14             for (char c : numString.toCharArray()) {
    15                 int n = c - '0';
    16                 sum += n;
    17             }
    18             kk--;
    19             numString = String.valueOf(sum);
    20         }
    21         return sum;
    22     }
    23 }

    LeetCode 题目总结

  • 相关阅读:
    vscode Git提交
    安装 nrm
    vue+element ui项目总结点(六)table编辑当前行、删除当前行、新增、合计操作
    Filebrowser安装教程
    vue数据处理:把数组处理成适用于tree组件的数据
    WC2016模拟Divisor
    对拍模板
    题解(5031. 【NOI2017模拟3.27】B)(数论,组合数学)
    Codeforces #657 Div2C choosing flowers
    关于一个大菜鸡的记录
  • 原文地址:https://www.cnblogs.com/cnoodle/p/15082886.html
Copyright © 2011-2022 走看看