Lintcode(1)-两个字符串是变位词
来源:互联网 发布:电脑淘宝直播怎么看 编辑:程序博客网 时间:2024/04/29 15:51
Q:
写出一个函数 anagram(s, t) 去判断两个字符串是否是颠倒字母顺序构成的。
给出 s=”abcd”,t=”dcab”,返回 true
A:
一开始理解错意思了。以为只是判断一个字符串是另一个字符串的倒序。
运行就是通不过。直到看到函数名anagram是回文。就是说这里指的是两个字符串的内容是相同的,但是允许顺序不同。这些就简单了。
bool anagram(string s, string t) { typedef unsigned int uint32; typedef unsigned char uint8; uint32 s_length = s.length(); uint32 t_length = t.length(); if (s_length != t_length) { return false; } std::vector<uint8> v1(s.begin(), s.end()); std::vector<uint8> v2(t.begin(), t.end()); std::sort(v1.begin(), v1.end()); std::sort(v2.begin(), v2.end()); for ( uint32 i = 0; i < s_length; ++i ) { if ( v1[i] != v2[i] ) { return false; } } return true; }
还有另外的实现为不使用辅助的vector,就遍历s的同时去遍历t,查看s中的字符是否在t中也存在。
0 0
- Lintcode(1)-两个字符串是变位词
- LintCode 两个字符串是变位词
- LintCode 两个字符串是变位词
- [lintcode]两个字符串是变位词
- LintCode:两个字符串是变位词
- 【LintCode】两个字符串是变位词
- LintCode : 两个字符串是变位词
- lintcode -- 两个字符串是变位词
- 两个字符串是变位词-LintCode
- Lintcode两个字符串是变位词
- Lintcode—(1)两个字符串是变位词
- LintCode Two Strings Are Anagrams 两个字符串是变位词
- lintcode python— 两个字符串是变位词
- 两个字符串是变位词
- 两个字符串是变位词
- 两个字符串是变位词
- 两个字符串是变位词
- 两个字符串是变位词
- Maven构建web项目在Eclipse中部署的几种方法
- java 制作exe
- Request的getParameter和getAttribute方法的区别
- 欢迎使用CSDN-markdown编辑器
- Android Lint 检查规则列表
- Lintcode(1)-两个字符串是变位词
- jquery获得父页面
- 造成笔记本电池损耗的主要原因
- ios开发学习笔记(经验大全)
- 操作系统之文件系统
- explicit constructor的用法
- 图片滚动从右向左滚动网页代码
- gitlab 一键安装 centos 6.5
- JAVA 关闭窗口的设置 DISPOSE_ON_CLOSE和EXIT_ON_CLOSE 的区别