Re: [閒聊] 每日LeetCode

看板Marginalman作者 (博衣こより的貓)時間1年前 (2022/09/22 11:38), 1年前編輯推噓1(105)
留言6則, 3人參與, 1年前最新討論串7/719 (看更多)
※ 引述《Rushia (みけねこ的鼻屎)》之銘言: : 557. Reverse Words in a String III : 題目: : 給定一個字串,返回這個字串以"空白分隔"的反轉。 : Example: : Input: s = "God Ding" : Output: "doG gniD" 思路: 1. 用getline切 2. 用reverse轉 3. 放進output 4. 後面還有就加空格 都是stl有的東西我不想造輪子== class Solution { public: string reverseWords(string input) { const char delimiter = ' '; string output = ""; stringstream input_stream(input); while (!input_stream.eof()) { string sub_str; getline(input_stream, sub_str, delimiter); reverse(sub_str.begin(), sub_str.end()); output.append(sub_str); if(!input_stream.eof()) output.append(" "); } return output; } }; -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.248.143.172 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1663817895.A.ABB.html ※ 編輯: yam276 (60.248.143.172 臺灣), 09/22/2022 11:38:36

09/22 11:42, 1年前 , 1F
不造輪子的話執行時間很難看ㄚ
09/22 11:42, 1F

09/22 11:43, 1年前 , 2F
但考慮實務 造輪子不切實際 真要當輪子大師我就去寫組語了
09/22 11:43, 2F

09/22 11:44, 1年前 , 3F
比起造輪子能根據地形換正確的輪子才是重點
09/22 11:44, 3F

09/22 12:21, 1年前 , 4F
這個一般來說都是two pointers解ㄅ,實際上寫起來也
09/22 12:21, 4F

09/22 12:21, 1年前 , 5F
很簡單 而且你這樣做浪費空間,還要copy data
09/22 12:21, 5F

09/22 14:59, 1年前 , 6F
後來改用two pointers了
09/22 14:59, 6F
文章代碼(AID): #1ZAzYdgx (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1ZAzYdgx (Marginalman)