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. c实现队列

    使用链表实现队列的入队和出队 #include <iostream> #include <stdio.h> #include <string.h> #include ...

  2. 在Ubuntu上安装openResty #1

    在Ubuntu上安装openResty #1 OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库.第三方模块以及大多数的依赖项.用于方 ...

  3. 分页——为Mybatis配置PageHelper

    1.pom.xml追加 pagehelper : 4.1.4 2.mappers.xml中追加 <plugins> <plugin interceptor="com.git ...

  4. 内存管理2-set方法的内存管理-程序解析

    创建class Book .h 有@ property float price;  //@synthesize 自动 ------------ 创建class Student #import &quo ...

  5. Appium Inspector定位Webview/H5页面元素

    目录 操作步骤 Python操作该混合App代码 Appium在操作混合App或Android App的H5页面时, 常常需要定位H5页面中的元素, 传统方式是 翻墙 + 使用Chrome://ins ...

  6. keras 模型简介

    keras模型在keras中主要有两种模型,顺序模型,以及模型类(类的内部有函数) model.layers 是层的列表,他们组成了模型 model.inputs 是模型输入的张量 model.out ...

  7. Mybatis 批量操作-删除、修改和查询

          批量操作的核心就是一次传入多个数据然后进行相关操作,增删改查中掌握其中一个,其它的就可以举一反三,触类旁通.它之所以执行效率高,是因为合并后日志量(MySQL的binlog和InnoDB的 ...

  8. CentOS6.8上Docker配置阿里云镜像加速器

    1.打开网站https://dev.aliyun.com,点击管理中心,登录阿里云账号(没有的可以注册,也可以用淘宝等第三方账号登录). 2.点击镜像加速器,复制加速器地址 3.配置本机Docker运 ...

  9. Web Services之基本认识

    参考:http://www.w3school.com.cn/webservices 1.什么是Web Services Web Services 可使您的应用程序成为 Web 应用程序.Web Ser ...

  10. SpringBoot保存数据报错:could not execute statement; SQL [n/a]; constraint [PRIMARY];nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

    使用SpringBoot做JAVA开发时,用Repository.save();保存数据的时候遇到了报错: could not execute statement; SQL [n/a]; constr ...