cracking the coding interview No1.8
来源:互联网 发布:suse linux 安装gcc 编辑:程序博客网 时间:2024/05/20 10:52
1.8Assume you have a method isSubstring which checks if one word is a substring of another.
Given two strings,s1 and s2,write code to check if s2 is a rotation of s1 using only onw call to is
Substring (eg,”waterbottle” is a rotation of “erbottlewat”.)
Answer:
//C语言风格bool judge(char *str1,char *str2){if (str1 == NULL || str2 == NULL)return false;char *str3 = str1;char *str4 = str1;while (*str1++);while (*str1++=*str3++);//连接str1和str3*str1 = '\0';while (*str4!='\0'){char *str5 = str2;while (*str4++ == *str5++ ){if (*str5 == '\0')return true;}str4++;}return false;}
//Java简洁的代码public static boolean isRotation(String s1, String s2) {int len = s1.length();/* check that s1 and s2 are equal length and not empty */if (len == s2.length() && len > 0) { /* concatenate s1 and s1 within new buffer */String s1s1 = s1 + s1;return isSubstring(s1s1, s2);}return false;}
0 0
- cracking the coding interview No1.8
- cracking the coding interview No1.1
- cracking the coding interview No1.2
- cracking the coding interview No1.3
- cracking the coding interview No1.4
- cracking the coding interview No1.5
- cracking the coding interview No1.6
- cracking the coding interview No1.7
- Cracking the Coding Interview
- 《Cracking the coding interview》
- Cracking The Coding Interview
- Cracking the coding interview
- Cracking the coding interview
- Cracking the coding interview--Q1.8
- Cracking the coding interview--Q1.8
- Cracking the coding interview--Q1.8
- Cracking the coding interview--Q4.8
- Cracking the coding interview--Q8.8
- javascript里的条件判断
- 【智能车新手入门】-赛车行驶策略
- POJ 2456 Aggressive cows(二分+贪心)
- hdu-1527
- CentOS──xxx is not in the sudoers file解决方法
- cracking the coding interview No1.8
- 针对android方法数64k的限制,square做出的努力
- 项目结构、包、编译为exe!
- MyEclipse中更换JDK及工程警告问题
- 人机博弈-吃子棋游戏(四)搜索算法
- Ubuntu下安装eclipse
- Linux内核分析 - 网络[一]:收发数据包的调用
- java的封装原理
- Torque3D向场景加入对象