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. open, create, close

    1.open 系统调用 说明: 调用open函数打开或者创建一个文件.函数定义如下:  #include <fcntl.h> int open(const char *pathname, ...

  2. Luogu4191 [CTSC2010]性能优化【多项式,循环卷积】

    题目描述:设$A,B$为$n-1$次多项式,求$A*B^C$在系数模$n+1$,长度为$n$的循环卷积. 数据范围:$n\leq 5*10^5,C\leq 10^9$,且$n$的质因子不超过7,$n+ ...

  3. cdh版hbase构建Phoenix 遇到的坑

    Phoenix 构建cdh版hbase遇到的坑 1. 安装phoenix 下载:在github上下载对应版本https://github.com/apache/phoenix 解压:略 编译: 修改根 ...

  4. 微信小程序之简单记账本开发记录(一)

    下载并安装微信开发者工具 在选择开发记账本程序的时候犹豫着选择android studio还是微信小程序 最后选择了微信小程序,因其便利和快捷. 话不多说,第一步,下载并安装微信开发者工具.下面是教程 ...

  5. JavaWeb_(Spring框架)认识Spring中的aop

    1.aop思想介绍(面向切面编程):将纵向重复代码,横向抽取解决,简称:横切 2.Spring中的aop:无需我们自己写动态代理的代码,spring可以将容器中管理对象生成动态代理对象,前提是我们对他 ...

  6. sql注入笔记-sqlite

    1. SQLite 1. 常用语句及基本结构 (1)sqlite因为其比较简易每个db文件就是一个数据库,所以不存在information_schema数据库,但存在类似作用的表sqlite_mast ...

  7. Jenkins与gitlib实现自动化部署与持续构建

    Jenkins概念 Jenkins是一个功能强大的应用程序,允许持续集成和持续交付项目,无论用的是什么平台.这是一个免费的源代码,可以处理任何类型的构建或持续集成.集成Jenkins可以用于一些测试和 ...

  8. CentOS7下安装php-redis扩展

    yum -y install php70w-pecl-redis

  9. ubuntu docker 环境安装

    转载:https://www.cnblogs.com/blog-rui/p/9946382.html 1. 在Ubuntu中安装Docker 更新ubuntu的apt源索引 sudo apt-get ...

  10. [C++]数据结构:线性表之顺序表

    1 顺序表 ADT + Status InitList(SeqList &L) 初始化顺序表 + void printList(SeqList L) 遍历顺序表 + int ListLengt ...