Re: [閒聊] 每日leetcode
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
討論串 (同標題文章)
完整討論串 (本文為第 1427 之 1548 篇):