[問題] Leet Code第17題網路解答的2個問題

看板C_and_CPP作者 (阿東)時間7年前 (2019/01/16 15:14), 7年前編輯推噓2(203)
留言5則, 4人參與, 7年前最新討論串1/1
Code如下 vector<string> solution(string digits) { if (digits.empty()) return {}; vector<string> res; string dict[] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "t uv", "wxyz"}; for (int i = 0; i < digits.size(); ++i) { vector<string> t; int index=digits[i]-'0'; string str = dict[index]; for (int j = 0; j < str.size(); ++j) { for (string s : res) t.push_back(s + str[j]); } res = t; } return res; } 各位版友好, 用C++刷leetcode第17題時遇到幾個問題, 這是在網路上找到的解答... 有2個問題想請教, 1. int index=digits[i]-'0';中,digits[i] ]-'0'的意義是什麼? 2. for (string s : res) t.push_back(s + str[j]);中,res並沒有給予初始值,請問 這個for迴圈能順利執行? 這個答案放上leetcode是可以順利解答的, 但是在local端沒辦法得到解答... 麻煩理解的版友幫忙解惑,謝謝! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.137.226.8 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1547622869.A.B60.html

01/16 15:24, 7年前 , 1F
1.是把文字的"數字"轉成int
01/16 15:24, 1F
原來如此,感謝!

01/16 16:55, 7年前 , 2F
我看到好多問號
01/16 16:55, 2F

01/16 17:08, 7年前 , 3F
人工空白吧,我猜
01/16 17:08, 3F
手機發文,晚點修改,傷眼抱歉 ※ 編輯: Dong0129 (114.137.226.8), 01/16/2019 17:13:50 ※ 編輯: Dong0129 (220.137.85.178), 01/16/2019 19:45:48

01/16 22:04, 7年前 , 4F
2. 是C++11的Range-based for loop
01/16 22:04, 4F

01/16 22:04, 7年前 , 5F
原來有這種用法...謝謝! ※ 編輯: Dong0129 (220.137.85.178), 01/16/2019 23:45:58
文章代碼(AID): #1SFjdLjW (C_and_CPP)