博客
关于我
Leetcode 面试经典 01.02 判定是否互为字符重排(C/C++)
阅读量:607 次
发布时间:2019-03-11

本文共 670 字,大约阅读时间需要 2 分钟。

大家好,我是小黄呀

题目大意

给定两个字符串 s1s2,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。

思路分析

  1. sort排序后比较:直接对两个字符串进行字符排序,用到sort函数,然后对排序后的结果进行比较,若相同则为true,否则为false。
  2. 数组下标转换法:利用字符对应数组下标进行转换,定义一个大小为128的数组,将字符串s1s2中的每个字符都分别进行转换,字符串s1中出现某字符,则对应数组下标值+1,字符串s2中出现某字符,则对应数组下标值-1,然后对整个数组进行遍历,若存在某数组下标对于值不为0,则说明结果为false,反之,为true

具体代码

//sort排序后比较 class Solution{   	public:		bool CheckPermutation(string s1, string s2){   			sort(s1.begin(),s1.end());			sort(s2.begin(),s2.end());			return s1==s2;		}};
//数组转换法 class Solution{   	public:		bool CheckPermutation(string s1, string s2){   			int len1=s1.size();			int len2=s2.size();			if(len1!=len2)				return false;			int len[128]={   0};			for(int i=0;i

转载地址:http://rzhtz.baihongyu.com/

你可能感兴趣的文章
Nginx启动SSL功能,并进行功能优化,你看这个就足够了
查看>>
nginx启动脚本
查看>>
Nginx在Windows上和Linux上(Docker启动)分别配置基本身份认证示例
查看>>
Nginx在Windows下载安装启动与配置前后端请求代理
查看>>
Nginx多域名,多证书,多服务配置,实用版
查看>>
nginx如何实现图片防盗链
查看>>
Nginx学习总结(13)——Nginx 重要知识点回顾
查看>>
Nginx学习总结(14)——Nginx配置参数详细说明与整理
查看>>
Nginx安装与常见命令
查看>>
Nginx安装及配置详解
查看>>
Nginx实战经验分享:从小白到专家的成长历程!
查看>>
Nginx实现反向代理负载均衡
查看>>
nginx实现负载均衡
查看>>
nginx开机启动脚本
查看>>
nginx异常:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf
查看>>
nginx总结及使用Docker创建nginx教程
查看>>
nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:128
查看>>
nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in usrlocalnginxconfnginx.conf128
查看>>
nginx日志分割并定期删除
查看>>
Nginx日志分析系统---ElasticStack(ELK)工作笔记001
查看>>