字符串的组合

引用剑指offer

 1 //组合,从字符串str中取m个字符的所有组合,结果保存在vector中
 2 void combination(char* str,int m,vector<char>& result){
 3     //有两个停止条件:m==0或者*str=='\0'
 4     //先判断m
 5     if(m==0){
 6         for(vector<char>::iterator it=result.begin();it!=result.end();it++)
 7             cout<<*it;
 8         cout<<endl;
 9         return;
10     }
11     if(str==NULL|| *str=='\0')
12         return;
13     //情况1:当前字符被选中,m--
14     result.push_back(*str);
15     combination(str+1,m-1,result);
16     //上一步将含有*str的组合都输出了,下一步将*str移出result
17     result.pop_back();
18     //情况2:当前字符没被选中,m不变
19     combination(str+1,m,result);
20 }

 

转载于:https://www.cnblogs.com/liuzhiminxd/p/3983102.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值