leetcode 第17题 分析 char*和string相互转化

char*(或者char)转string 可以看看string的构造函数
default (1)	         string();
copy (2) string (const string& str);
substring (3) string (const string& str, size_t pos, size_t len = npos);
from c-string (4) string (const char* s);
from buffer (5) string (const char* s, size_t n);
fill (6) string (size_t n, char c);
range (7) template <class InputIterator> //vector<string> v;
//str(v.begin(),v.end());
string (InputIterator first, InputIterator last);
initializer list (8) string (initializer_list<char> il);
move (9) string (string&& str) noexcept;
1. char* ->string          string s(地址,n) or string s(地址);
2. char -> string          string s(n个char,char)
 
 
3. string ->char*    使用strcpy(地址,str.c_str());
将string转char*,可以使用string提供的c_str()或者data()函数。
其中c_str()函数返回一个以'\0'结尾的字符数组,而data()仅返回字符串内容,而不含有结束符'\0'。
c_str()返回是临时指针不能进行操作。并且其指向的是原string数据,改变其指针指向的值,string会发生改变。
#include <iostream>
#include <vector>
#include <string> using namespace std; class Solution { public:
vector<string> letterCombinations(string digits) {
vector< vector<char> >phone{{'a','b','c'},{'d','e','f'},{'g','h','i'},{'j','k','l'},{'m','n','o'},{'p','q','r','s'},{'t','u','v'},{'w','x','y','z'}};
if(digits.empty()) return vector<string>{}; vector<string> resNext=letterCombinations(digits.substr()); int curNum=digits[]-''-;
vector<char> vc(phone[curNum]);
vector<string> resCur; for(auto &each:vc){
vector<string> temp(resNext);
//这一行不能丢,temp为空的时候 char->string if(temp.empty()){string s(&each,);resCur.push_back(s);continue;}
for(auto& str:temp){
str=each+str;
}
resCur.insert(resCur.end(),temp.begin(),temp.end());
}
return resCur;
}
}; int main(){
Solution sol;
string digits="";
vector<string> res=sol.letterCombinations(digits);
for(int i=;i<res.size();++i){
cout<<res[i]<<endl;
}
}

17.Letter Combinations of a Phone Number (char* 和 string 相互转化)的更多相关文章

  1. Leetcode 17. Letter Combinations of a Phone Number(水)

    17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive ...

  2. 刷题17. Letter Combinations of a Phone Number

    一.题目说明 题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合. 如输入23所有可能的输出: ...

  3. [LeetCode][Python]17: Letter Combinations of a Phone Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...

  4. 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  5. Java [leetcode 17]Letter Combinations of a Phone Number

    题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...

  6. Leetcode 17.——Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  7. [leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...

  8. 17. Letter Combinations of a Phone Number(bfs)

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...

  9. 17. Letter Combinations of a Phone Number (backtracking)

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

随机推荐

  1. 畅通工程续(HDU 1874)(简单最短路)

    某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多.这让行人很困扰. 现在 ...

  2. MySQL数据分析-(1) 数据库前言

    (一)开场白 大家好,欢迎大家跟我一起学习<MySQL数据分析实战>这门课程,对于数据分析师来说,数据库是每一个从业者都必须掌握的课程,我们这门课是从实战的角度出发,我会帮助大家梳理MyS ...

  3. ACM之路(12)—— KMP & 扩展KMP & Manacher

    最近做完了kuangbin的一套关于kmp的题目(除了一道字典树的不会,因为还没学字典树所以先放放),做个总结.(kuangbin题目的链接:http://acm.hust.edu.cn/vjudge ...

  4. 小程序开发:用原生还是选框架(wepy/mpvue/uni-app/taro)?

    小程序开发:用原生还是选框架(wepy/mpvue/uni-app/taro)? 自 2017-1-9微信小程序诞生以来,历经2年多的迭代升级,已有数百万小程序上线,成为继Web.iOS.Androi ...

  5. Exception in thread "main" java.util.ConcurrentModificationException解决方案

    我想判断一个集合里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素, 当时的做法是: public class ListIterator ...

  6. [GIT]比较不同分支的差异

        比如我们有 2 个分支:master, dev,现在想查看这两个 branch 的区别,有以下几种方式: undefined 1.查看 dev 有,而 master 中没有的: 1.查看 de ...

  7. Tornado实现监控数据实时展示

    前言: It has been a while since I last updated my blogs. 使用Tornado开发一个实时监控信息系统,其中包括 CUP.内存.网卡.磁盘使用率. 涉 ...

  8. x86架构64位模式下的寄存器列表

    在此列出x86架构处理器在64位模式下的可用寄存器列表,方便查阅- 这里要注意的是,在64位模式下,所有通用寄存器都能访问第8位部分,低16位部分以及低32位部分. 以下是64位模式下AMD64 AB ...

  9. UC浏览器打开百度显示 丰臣吉秀.网址

  10. 修改IDEAL 快捷键风格

    选择File------Setting-----进入到设置窗口-------keymap--------选择自己适应的快捷键风格 选择Eclipse后Ideal的快捷键就和Eclipse相同