Cracking the coding interview--Q1

Chapter 1 | Arrays and Strings

1.1 Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?

使用位图,o(n)

1.2 Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.)

strrev,先移动指针到尾部,然后一个一个往回换。

1.3 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 method.

位图。但要问清楚题意。

1.4 Write a method to decide if two strings are anagrams or not.

辅助数组。

1.5 Write a method to replace all spaces in a string with ‘%20’.

拷贝,映射,辅助数组

1.6 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?

13-1,13-4,13-16,3*4=12次。原题解14次。

1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.

原解想法不错,直接开数组存需要做的动作。

1.8 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 “erbottlewat”).

原解想法不错,使用两倍长度的字符串来看是否是旋转字符串,用子串来判定。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值