作者Dong0129 (阿东)
看板C_and_CPP
标题[问题] Leet Code第17题网路解答的2个问题
时间Wed Jan 16 15:14:27 2019
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://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1547622869.A.B60.html
1F:→ jerryh001: 1.是把文字的"数字"转成int01/16 15:24
原来如此,感谢!
2F:推 s4300026: 我看到好多问号01/16 16:55
3F:推 x246libra: 人工空白吧,我猜01/16 17:08
手机发文,晚点修改,伤眼抱歉
※ 编辑: Dong0129 (114.137.226.8), 01/16/2019 17:13:50
※ 编辑: Dong0129 (220.137.85.178), 01/16/2019 19:45:48
4F:→ nickchen1202: 2. 是C++11的Range-based for loop 01/16 22:04
原来有这种用法...谢谢!
※ 编辑: Dong0129 (220.137.85.178), 01/16/2019 23:45:58