精華區beta Marginalman 關於我們 聯絡資訊
https://leetcode.com/problems/total-characters-in-string-after-transformations-i 3335. Total Characters in String After Transformations I 給你一個字串s和一個數字t表示回合,每回合 a->b b->c ..... y->z z->ab,求出最後 字串的長度。 思路: 照題目敘述做轉換並做t次就好,因為數字很大要取MOD。 Java Code: ------------------------------------------------- class Solution { static int MOD = (int)1e9 + 7; public int lengthAfterTransformations(String s, int t) { int[] cnt = new int[26]; for (char ch : s.toCharArray()) { cnt[ch - 'a']++; } while (t-- > 0) { int[] tmp = new int[26]; for (int i = 1; i < 26; i++) { tmp[i] = cnt[i - 1]; } tmp[0] = cnt[25]; tmp[1] = (tmp[1] + cnt[25]) % MOD; cnt = tmp; } int res = 0; for (int num : cnt) { res = (res + num) % MOD; } return res; } } ------------------------------------------------- -- https://i.imgur.com/5xKbxoh.jpeg -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.159.104.111 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1747114798.A.A84.html
yam276: 好痛苦 05/13 16:29
leafff: 大師 05/13 21:15