当前位置: 首页 > news >正文

计算机网络 网站创建网页的三种方法

计算机网络 网站,创建网页的三种方法,中国十大搜索引擎排名最新,阿里云服务器免费一年给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。 给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。 示例 1: 输入:digits "23" 输出&…

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母
que
示例 1:

输入digits = "23"
输出["ad","ae","af","bd","be","bf","cd","ce","cf"]

示例 2:

输入digits = ""
输出[]

示例 3:

输入digits = "2"
输出["a","b","c"]

提示:

0 <= digits.length <= 4

digits[i] 是范围 ['2', '9'] 的一个数字。


AC:

/** @lc app=leetcode.cn id=17 lang=cpp** [17] 电话号码的字母组合*/// @lc code=start
class Solution {
private:const string letterMap[10] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz",};
public:string s;vector<string> result;void backtracking(const string& digits, int index) {if(digits.size() == index) {result.push_back(s);return ;}int digit = digits[index] - '0';string letter = letterMap[digit];for(int i = 0; i < letter.size(); i++){s.push_back(letter[i]);backtracking(digits, index + 1);s.pop_back();}}vector<string> letterCombinations(string digits) {s.clear();result.clear();if(digits.size() == 0)return result;backtracking(digits, 0);return result;}
};
// @lc code=end

AC
The function uses a private member variable letterMap to map each digit to its corresponding set of letters. The letterMap is an array of strings, where each string represents the set of letters corresponding to a digit. For example, letterMap[2] is the string “abc”, which represents the letters ‘a’, ‘b’, and ‘c’ corresponding to the digit 2.

The function uses a recursive backtracking approach to generate all possible letter combinations. The backtracking function takes two arguments: the input string of digits and an index representing the current position in the input string. The function first checks if the current position is equal to the length of the input string. If so, it adds the current combination of letters to the result vector and returns.

If the current position is less than the length of the input string, the function retrieves the set of letters corresponding to the current digit using the letterMap array. It then iterates over each letter in the set and adds it to the current combination of letters. The function then recursively calls itself with the updated combination of letters and the next index. After the recursive call returns, the function removes the last letter from the combination of letters to backtrack and try the next letter in the set.

The letterCombinations function initializes the result vector and calls the backtracking function with the input string and an initial index of 0. If the input string is empty, the function returns an empty vector.

Overall, this implementation is a simple and efficient way to generate all possible letter combinations of a given string of digits using a backtracking algorithm. One possible way to improve the code would be to add error checking to ensure that the input string only contains digits. Additionally, the variable names could be more descriptive to make the code easier to read and understand.


How does the backtracking algorithm work in generating all possible letter combinations?

The backtracking algorithm used in this code works by recursively generating all possible letter combinations of a given string of digits. The algorithm uses a private member variable letterMap to map each digit to its corresponding set of letters.

The backtracking function takes two arguments: the input string of digits and an index representing the current position in the input string. The function first checks if the current position is equal to the length of the input string. If so, it adds the current combination of letters to the result vector and returns.

If the current position is less than the length of the input string, the function retrieves the set of letters corresponding to the current digit using the letterMap array. It then iterates over each letter in the set and adds it to the current combination of letters. The function then recursively calls itself with the updated combination of letters and the next index. After the recursive call returns, the function removes the last letter from the combination of letters to backtrack and try the next letter in the set.

The letterCombinations function initializes the result vector and calls the backtracking function with the input string and an initial index of 0 . If the input string is empty, the function returns an empty vector.

Overall, the backtracking algorithm works by generating all possible letter combinations of a given string of digits by recursively iterating over each digit and its corresponding set of letters. The algorithm uses a simple and efficient approach to generate all possible combinations of letters, and is a common technique used in many other combinatorial problems .

http://www.yayakq.cn/news/821160/

相关文章:

  • 网站免费推广策划方案南京中企动力有限公司
  • 网站维护提示怎么做用wordpress建的网站
  • 企业网站管理是什么网站标题关键字
  • 沈阳网站设计制作手机网站开发计划
  • 网站屏蔽ip地址广告创意策划
  • 免费浪漫网页制作网站通辽建设工程网站
  • 网站怎么推广比较好做网站外链需要多少钱
  • 如何制作局域网站wordpress widget logic
  • 网站建设推广选stso88效果好上海羽贝网站建设
  • 企业网站备案域名可以用个人的怎样制作免费网页
  • 展示型的网站开发价格文山知名网站建设公司
  • 网站维护需要的知识网站建设企业 熊账号
  • 漳州 网站建设多少钱大连网站开发哪儿好薇
  • 大学生课程设计网站济南优化官网公司
  • 北京网站搭建设计微信开放文档
  • 手机公众平台网站开发阿里巴巴网站域名
  • 福州网站seo公司中国世界排名前100大学
  • 铁岭做网站html基本知识
  • 上传自己做的网站后台怎么办小说网站开发实录
  • 建设网站有何要求net域名大网站
  • 西宁网站建设维护广州科技网络公司排名
  • 重点实验室网站建设方案网站建设地位
  • 色彩搭配 网站做网站需要画原型图么
  • 江门网站建设模板网站建立失败的原因是
  • 佛山网站设计代理商公司网站建设分录
  • 德国室内设计网站ui设计包括哪些内容
  • 浪漫网站建设腾讯云服务器可以退款吗
  • 如何制作网站机器人河南住房和城乡建设厅网站主页
  • 网站的论坛怎么做营销的方法和技巧
  • 买了云服务器怎么做网站石狮app网站开发