Re: [閒聊] 每日LeetCode已回收

看板Marginalman作者 (四葉天下第一)時間3年前 (2022/10/28 15:43), 3年前編輯推噓1(102)
留言3則, 3人參與, 3年前最新討論串71/719 (看更多)
最近寫leetcode每日比以前好一些 至少想的到解法了 但是runtime 和 memory 目前都在墊底部分 不知道接著刷leetcode能不能改善 --------------------------------------------------- 今天的用C++寫 class Solution { public: vector<vector<string>> groupAnagrams(vector<string>& strs) { map<map<char, int>, vector<string>> m; for (string str : strs) { map<char, int> c_map; for (char c : str) { c_map[c] ++; } m[c_map].push_back(str); } vector<vector<string>> vec; for (auto& s : m) { vec.push_back(s.second); } return vec; } }; 把strs中的每個string裡面的c存到c_map裡統計數量 再丟到m這個map裡面index 後面用vector存放string 最後把map的值丟到vector裡return Runtime 131ms Beats 14.60% Memory 37.5MB beats 5.3% ---- class Solution { public: vector<vector<string>> groupAnagrams(vector<string>& strs) { map<string, vector<string>> m; for (string str : strs) { string s = str; sort(s.begin(), s.end()); m[s].push_back(str); } vector<vector<string>> vec; for (auto& s : m) { vec.push_back(s.second); } return vec; } }; 修改用sort的string 換掉map Runtime 89ms Beats 32.21% Memory 19.8MB beats 70.47% ---- -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.228.246.249 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1666943039.A.041.html

10/28 15:46, 3年前 , 1F
大師
10/28 15:46, 1F
※ 編輯: Pash97143 (125.228.246.249 臺灣), 10/28/2022 15:54:20 ※ 編輯: Pash97143 (125.228.246.249 臺灣), 10/28/2022 16:03:37

10/28 17:06, 3年前 , 2F
這是c 還是cpp
10/28 17:06, 2F

10/28 22:19, 3年前 , 3F
cpp vector map這些是cpp才有吧?
10/28 22:19, 3F
文章代碼(AID): #1ZMuW_11 (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1ZMuW_11 (Marginalman)