国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 新宾| 北流市| 民乐县| 凤凰县| 阳城县| 卓尼县| 阿拉尔市| 湾仔区| 绥化市| 泗洪县| 札达县| 江川县| 姜堰市| 崇文区| 故城县| 昆山市| 常宁市| 禄劝| 抚宁县| 五峰| 文山县| 昌乐县| 宜宾县| 宜兰市| 黄浦区| 冷水江市| 饶河县| 永德县| 廊坊市| 伊吾县| 丽水市| 兴业县| 丰台区| 崇义县| 南安市| 四子王旗| 阿拉善盟| 乌什县| 肇源县| 安新县| 古田县|