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. php 图片格式转换-亲测ok

    代码如下 /** * 图片格式转换 * @param string $image_path 文件路径或url * @param string $to_ext 待转格式,支持png,gif,jpeg,w ...

  2. jar包中的依赖jar版本冲突

    背景,项目中之前引入了一个 图片上传的jar <groupId>commons-fileupload</groupId> <artifactId>commons-f ...

  3. spring事务配置异常

    spring事务配置不回滚spring事务管理配置,一般来说都是可以回滚的,最近在开发的过程中遇到了一个异常不回滚的问题,最终找到了原因,贴出来一下 1.首先这里定义一个接口 在接口中定义几个方法 2 ...

  4. 【Golang】基于录制,自动生成go test接口自动化用例

    背景 之前写过一篇博客,介绍怎么用Python通过解析抓包数据,完成自动化用例的编写.最近这段时间在使用go test,所以就在想能不能也使用代码来生成自动化用例,快速提升测试用例覆盖率.说干就干. ...

  5. RAD,Eclipse切換界面語言(中日英)

    找到RAD的EXE的位置: 右鍵→屬性→Link先(Target) 將原來的"C:\Program Files\IBM\SDP\eclipse.exe" -product com. ...

  6. 通过Vagrant搭建PHP环境(一) Vagrant box添加配置

    系统Windows10 Vagrant 1.8.1 VirtualBox 5.0.20 vagrant box下载地址:http://cloud.centos.org/centos/7/vagrant ...

  7. PHP学习之图像处理-水印类

    <?php $image = new Image(); $image->water('./upload/up_5cf0caca0565b.png','./upload/up_5cf0cb3 ...

  8. express利用nodemailer发送邮件(163邮箱)

    Nodemailer 是一个简单易用的Node.js邮件发送组件 首先安装这个组件 npm install nodemailer --save 安装之后,可以在某个get请求下,发送邮件,具体路由代码 ...

  9. fidder修改参数

    进入截栏模式 inspectors,webfroms run

  10. js时间戳转为日期函数

    js时间戳转为日期函数 function add0(m){ return m<10?'0'+m:m; } //timestamp参数示例:1501234567 function format(t ...