Re: [閒聊] 每日leetcode

看板Marginalman作者 (早瀬ユウカの体操服 )時間6月前 (2025/05/13 13:39), 編輯推噓1(101)
留言2則, 2人參與, 6月前最新討論串1427/1548 (看更多)
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

05/13 16:29, 6月前 , 1F
好痛苦
05/13 16:29, 1F

05/13 21:15, 6月前 , 2F
大師
05/13 21:15, 2F
文章代碼(AID): #1e8jikg4 (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1e8jikg4 (Marginalman)