[google面试CTCI] 1-8.判断子字符串】的更多相关文章

[字符串与数组] Q:Assume 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 one call to isSubstring (i e , “waterbottle” is a rotation of “e…
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public static void main(String args[]) { String str = "helloworld"; // 字符串对象 if (str.contains("world")) { // 子字符串存在 System.out.println("…
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public static void main(String args[]) { String str = "helloworld"; // 字符串对象 if (str.indexOf("world") != -1) { // 能找到子字符串 System.out.println(…
[字符串与数组] Q:Write a method to decide if two strings are anagrams or not 题目:写一个算法来判断两个字符串是否为换位字符串.(换位字符串是指组成字符串的字符相同,但位置不同) 解答: 方法一:假设为ascii2码字符串,那么可以分配两个256大小的int数组,每个数组用于统计一个字符串各个字符出现的次数,最后,比较这两个int数组,看是否每个元素都相同.时间复杂度为O(n). int anagrams1(char* str1,c…
[字符串与数组] Q:Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer NOTE: One or two additional variables are fine An extra copy of the array is not FOLLOW UP Write the test cases for this…
[字符串与数组] Q:Write a method to replace all spaces in a string with ‘%20’ 题目:写一个算法将一个字符串中的空格替换成%20 解答: 很直观的解法,首先统计出字符串中空格个数,然后分配新的内存空间,依次从头到尾复制原字符串到新字符串中,遇到空格,则复制%20这三个字符.还有没有其他更好点的方法呢?? char* replace(char* str){ int len=strlen(str); int spaceNum=0; int…
[字符串与数组] Q:Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0. 题目:写一个算法,如果一个 MxN矩阵中某个元素为0,则将该元素所在的行.列都置为0. 解答: 方法一:错误的解法,思维误区.依次遍历二维数组(矩阵),遇到一个0,就将这个0所在的行和列全部置为0,咋看一下没问题,仔细一想不对啊,这样做后,很有可能操作完后,这个二维数…
[字符串与数组] Q:Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees Can you do this in place? 题目:假定一幅图像能用NxN的矩阵表示,每个像素是四字节.写一个算法将图像旋转90度,你能否在原地进行操作(也即不分配额外的存储空间)? 解答: 我们不知…
e.g. <c:forEach items="${datas}" var="data"> <c:if test="${not fn:containsIgnoreCase(data, 'apple')}"> <p>Doesn't contain 'apple'</p> </c:if> </c:forEach> 使用el标签,在jsp页面需要声明: <%@ taglib u…
[链表] Q:Write code to remove duplicates from an unsorted linked list      FOLLOW UP      How would you solve this problem if a temporary buffer is not allowed? 题目:编码实现从无序链表中移除重复项.          如果不能使用临时缓存,你怎么编码实现? 解答: 方法一:不使用额外的存储空间,直接在原始链表上进行操作.首先用一个指针指向链…