国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

leetcode- Letter Combinations of a Phone Number

2019-11-08 02:26:08
字體:
供稿:網(wǎng)友

Question: Given a digit string, return all possible letter combinations that the number could rePResent.

A mapping of digit to letters (just like on the telephone buttons) is given below.

Input:Digit string “23” Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”].

Solution:

class Solution {public: vector<string> letterCombinations(string digits) { vector<string> res; int len = digits.size(); if(len <= 0) return res; int first = 0; while(digits[first] <'2' || digits[first] > '9') first++; int num = (digits[first] - '2') * 3; if(digits[first] <= '7'){ stringstream ss; string s; ss<<(char)('a'+num); ss>>s; res.push_back(s); ss.clear(); ss<<(char)('b'+num); ss>>s; res.push_back(s); ss.clear(); ss<<(char)('c'+num); ss>>s; res.push_back(s); } if(digits[first] == '7'){ res.push_back("s"); } else if(digits[first] == '8'){ res.push_back("t"); res.push_back("u"); res.push_back("v"); } else if(digits[first] == '9'){ res.push_back("w"); res.push_back("x"); res.push_back("y"); res.push_back("z"); } for(int i = 1 ; i < len ;i++){ if(digits[i] < '2' || digits[i] > '9') continue; int num = (digits[i] - '2') * 3; string tmp = res.front(); int nowlen = tmp.size(); while(tmp.size() == nowlen){ res.erase(res.begin()); if(digits[i] <= '7'){ res.push_back(tmp+(char)(97 + num)); res.push_back(tmp+(char)(98 + num)); res.push_back(tmp+(char)(99 + num)); } if(digits[i] == '7'){ res.push_back(tmp+"s"); } else if(digits[i] == '8'){ res.push_back(tmp+"t"); res.push_back(tmp+"u"); res.push_back(tmp+"v"); } else if(digits[i] == '9'){ res.push_back(tmp+"w"); res.push_back(tmp+"x"); res.push_back(tmp+"y"); res.push_back(tmp+"z"); } tmp = res.front(); } } return res; }};

Attention:

methods of char to string:

“”+ char //wrong char+ “”//wrong stringstream ss;string s;//right stringstream ss;ss.str()//wrong 2. from key ‘7’ ,it make changes. not ‘9’ 3. vector has’t function of pop_front(), but list have.


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 酒泉市| 高州市| 兴文县| 琼结县| 潞西市| 惠东县| 盘锦市| 自贡市| 鄱阳县| 鹤山市| 清流县| 聂荣县| 湖口县| 农安县| 泗洪县| 民丰县| 隆子县| 安顺市| 乌恰县| 四川省| 灌云县| 绍兴县| 泗洪县| 尼木县| 米泉市| 营山县| 淮北市| 肥东县| 南召县| 确山县| 衡水市| 库伦旗| 灌云县| 海阳市| 富蕴县| 永仁县| 聊城市| 康马县| 泽州县| 奈曼旗| 绥化市|