CCI_chapter 1
1.1Implement an algorithm to determine if a string has all unique characters What if you can not use additional data structures?
bool isUniqueChars(string str) {
unsigned int checklittle = ;
unsigned int checklarger = ;
for(int i = ; i < str.size();i++)
{
bool flag = str[i] - 'a' >= ;
unsigned int temp;
temp = flag ? << (str[i] - 'a') :
<< (str[i] - 'A');
if(flag){
if( checklittle & temp ) return false;
else checklittle |= temp;
}else {
if(checklarger & temp) return false;
else checklarger |= temp;
}
}
return true;
1.2 Write code to reverse a C-Style String
void reverse(char *str){
if(NULL == str) return;
char *p = str;
while(*p)++p;
--p;
while(str < p){
char temp = *p;
*p = *str;
*str = temp;
--p;
++str;
}
}
1.3 Design an algorithm and write code to remove the duplicate characters in a string without using any additional bufer NOTE: One or two additional variables are fine .An extra copy of the array is not
void removeDuplicates(char[] str){
if(NULL == str) return ;
int len = strlen(str);
if(len < ) return ;
int tail = ;
for(int i = ; i < len ; i++){
int j;
for(j = ; j < tail ; j++){
if(str[j] == str[i]) break;
}
if(j == tail){
str[tail] = str[i];
++tail;
}
}
str[tail] = '\0';
}
这道题里面判断重复也可以使用1.1的思想,不过要提前搞清楚输入参数的字符集
1.4Write a method to decide if two strings are anagrams or not
bool anagram(string s, string t){
if( s.size() != t.size()) return false;
if( s.size() == ) return true;
int table[];
memset(table, , sizeof(int) * );
for(char c : s){
table[c]++;
}
for(char c: t){
table[c]--;
}
for (int c : table){
if(c != ) return false;
}
return true;
}
1.5
1.6Given 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?
void rotate(vector<vector<int> > &matrix) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int n = matrix.size();
if(n <= ) return;
for( int lay = ; lay < n/; lay++){
int start = lay;
int end = n - lay -;
for(int i = start ; i < end ; i++){
// record top
int temp = matrix[start][i];
//left to top
matrix[start][i] = matrix[n -- i][start];
// bottom to left
matrix[n -- i][start] = matrix[end][n --i];
//right to bottom
matrix[end][n--i] = matrix[i][end];
// top to right
matrix[i][end] = temp;
}
}
}
1.7Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0
void setZeroes(vector<vector<int> > &matrix) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int m = matrix.size();
if(m < ) return ;
int n = matrix[].size();
bool zeroR = false, zeroC = false;
for(int i = ; i< n ; i++)
if(matrix[][i] == ){
zeroR = true;
break;
}
for( int i = ; i< m ; i++){
if(matrix[i][] ==){
zeroC = true;
break;
}
}
for(int i = ; i< m; i++)
for(int j = ; j< n; j++)
if(matrix[i][j] == ){
matrix[][j] = ;
matrix[i][] = ;
}
for(int i = ; i< m;i++)
for( int j = ; j< n; j++)
if(matrix[][j] == || matrix[i][] == )
matrix[i][j] = ;
for(int i = ; i< n && zeroR ; i++) matrix[][i] = ;
for(int i = ; i< m && zeroC ; i++) matrix[i][] = ;
}
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”)
bool isRotation(string s1, string s2){
if(s1.size() != s2.size()) return false;
if(s1.size() == ) return true;
string ss = s1+ s1;
if(string::npos != ss.find(s2)) return true;
return false ;
}
CCI_chapter 1的更多相关文章
- CCI_chapter 19 Moderate
19 1 Write a function to swap a number in place without temporary variables void swap(int &a, i ...
- CCI_chapter 16 Low level
16.5 Write a program to find whether a machine is big endian or little endian Big-Endian和Little-Endi ...
- CCI_chapter 13C++
13.9Write a smart pointer (smart_ptr) class template<class T>class SmartPoint{ public: SmartPo ...
- CCI_chapter 8 Recurision
8.1 水题 8.2 Imagine a robot sitting on the upper left hand corner of an NxN grid The robot can only m ...
- CCI_chapter 4 trees and Grapths
4.1Implement a function to check if a tree is balanced For the purposes of this question,a balanced ...
- CCI_chapter 3 Stacks and Queues
3.1Describe how you could use a single array to implement three stacks for stack 1, we will use [0, ...
- CCI_chapter 2 Linked Lists
2.1 Write code to remove duplicates from an unsorted linked list /* Link list node */ struct node { ...
随机推荐
- 【HDOJ】4972 A simple dynamic programming problem
水题. #include <cstdio> #include <cstring> #include <cstdlib> int abs(int x) { ? -x: ...
- Codeforces 460 DE 两道题
D Little Victor and Set 题目链接 构造的好题.表示是看了题解才会做的. 假如[l,r]长度不超过4,直接暴力就行了. 假如[l,r]长度大于等于5,那么如果k = 1,显然答案 ...
- css样式:列表
css code: /*系统自带的*/ ul li{ list-style-style: disc; } /*自定义图标*/ ul li{ list-style-image: url("im ...
- 手势触摸定位(UIPanGestureRecognizer)
/** 1.相对于父坐标系而言,表示当前触摸点所在的位置 */ CGPoint locationPoint = [panGestureRecognizer locationInView:panGest ...
- 如何用SVN进行个人版本管理
事实上SVN的确是我用过的最好的源码管理工具,虽然我用过的这类工具并不多,只有VSS.CVS和SVN,其它像PVCS. TeamSource.ClearCase之类的只有耳闻,因为它们都是商业产品,并 ...
- Android编程之ActivityManager: Segmentation fault
今天运行代码时,出现了一个不能运行的故障问题:ActivityManager: Segmentation fault 是的,这个原因网上有诸多解释:包名不能是中文或者非法字符,或者重启新的avd来解决 ...
- [React] React Router: Named Components
In this lesson we'll learn how to render multiple component children from a single route. Define a n ...
- python:字符串取值
某个字符串为stmp="abcdef54321" 取前面5个stmp[:5] #abcde 取后面5个stmp[-5:] #54321 从前面开始取,不包括最后两个stmp[:-2 ...
- 在Linux系统中如何把文件拷贝到U盘
Linux下把所有的都当成文件处理,如果在linux系统下需要拷贝文件,哪么你需要先把U盘挂载到系统中的某一个位置,然后再使用cp命令完成拷贝. 工具/原料 Linux操作系统一台 U盘一枚 方法 ...
- 关于获得本机Mac Address的方法
网络上有讲获得Mac address的方法有如下: 1. 发送ARP命令,利用返回的Mac Address缓冲区得到 2. 用NetworkInterface.GetAllNetworkInterfa ...