Re: [閒聊] 每日leetcode

看板Marginalman作者 (是oin的說)時間1年前 (2024/08/06 11:28), 編輯推噓2(201)
留言3則, 3人參與, 1年前最新討論串648/1548 (看更多)
※ 引述 《JerryChungYC (JerryChung)》 之銘言: :   : https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-ii :   : 3016. Minimum Number of Pushes to Type Word II :   : 給一個字串 word :   : 電話按鍵映射小寫英文字母集合 透過按的次數獲得字母 : 如 2 映射 ['a', 'b', 'c'] 按 1 次得到 a 按 2 次得到 b 按 3 次得到 c : 現在將按鍵 2~9 重新對應到不同字母集合 每個鍵可以映射到任意數量的字母 : 找出輸入字串所需的最少按鍵次數 : 注意 1, *, #, 0 不映射到任何字母 :   : Example 1: : Input: word = "abcde" : Output: 5 : Explanation: "a" ~ "e" 分別對應 key 2 ~ 6 : 合計 1 + 1 + 1 + 1 + 1 = 5 :   機掰 這題以前周賽出過 我寫過 思路: 記數量 用的多的就拿去前面放 用的少的後面一點 笑死 我以前寫超醜 class Solution { public: int minimumPushes(string word) { int letter[300] = {}; int most1[300] = {}; int most2[300] = {}; int most3[300] = {}; int most4[300] = {}; int len = word.size(); for(int i = 0 ; i < len ; i++) { letter[word[i]] ++; } int m = 0; int mi = 0; for(int i = 0 ; i < 8 ; i ++) { m=0; mi=0; for(int j = 0 ; j < 300 ; j ++) { if(letter[j] > m) { m = letter[j]; mi = j; } } letter[mi] = 0; most1[mi] = m; } for(int i = 0 ; i < 8 ; i ++) { m = 0; mi=0; for(int j = 0 ; j < 300 ; j ++) { if(letter[j] > m) { m = letter[j]; mi = j; } } letter[mi] = 0; most2[mi] = m; } for(int i = 0 ; i < 8 ; i ++) { m=0; mi=0; for(int j = 0 ; j < 300 ; j ++) { if(letter[j] > m) { m = letter[j]; mi = j; } } letter[mi] = 0; most3[mi] = m; } for(int i = 0 ; i < 8 ; i ++) { m=0; mi=0; for(int j = 0 ; j < 300 ; j ++) { if(letter[j] > m) { m = letter[j]; mi = j; } } letter[mi] = 0; most4[mi] = m; } int ans = 0; for(int i = 1 ; i < 300 ; i ++) { // if(most1[i]!=0)printf("%d ",most1[i]); ans += most1[i]; } // printf("\n"); for(int i = 1 ; i < 300 ; i ++) { // if(most2[i]!=0)printf("%d ",most2[i]); ans += most2[i] * 2; } // printf("\n"); for(int i = 1 ; i < 300 ; i ++) { // if(most3[i]!=0)printf("%d ",most3[i]); ans += most3[i] * 3; } // printf("\n"); for(int i = 1 ; i < 300 ; i ++) { // if(most4[i]!=0)printf("%d ",most4[i]); ans += most4[i] * 4; } // printf("\n"); // printf("========== \n"); return ans; } }; -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.230.129.159 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1722914924.A.FE8.html

08/06 11:30, 1年前 , 1F
我現在也寫超醜 我去死
08/06 11:30, 1F

08/06 11:30, 1年前 , 2F
你有什麼用
08/06 11:30, 2F

08/06 11:35, 1年前 , 3F
你現在還是寫的很醜,放心
08/06 11:35, 3F
文章代碼(AID): #1ciPXi_e (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1ciPXi_e (Marginalman)