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 { ...
随机推荐
- FILTER 执行次数
select count(*) from SAVJ_ATOMJOURBAK where ((list_flag='1' and prt_flag='0') and acct_no not in (se ...
- BZOJ3394: [Usaco2009 Jan]Best Spot 最佳牧场
3394: [Usaco2009 Jan]Best Spot 最佳牧场 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 11 Solved: 9[Sub ...
- 【转】JAVA 读写二进制文件
原文网址:http://shiminghua234.blog.163.com/blog/static/263912422011619102350866 import java.io.*; /** * ...
- 计蒜客 444 / xtuoj 1024 京东的物流路径(并查集+离线lca)或者 (点分治)
题意:一颗树,定义一条路径的权值等于路径的边权之和,需要求这颗树所有路径中权值的最大值 思路: 考虑到路径权值与点权的最值有关,而最值的问题通常可以通过排序就行处理,于是想到先把点权排序. 容易看出如 ...
- Codeforces - ZeptoLab Code Rush 2015 - D. Om Nom and Necklace:字符串
D. Om Nom and Necklace time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Sublime Text 3配置记录
G++ /** * 工具->编译系统->新编译系统 */ { "cmd": ["g++", "${file}", "- ...
- Myeclipse中java文件注释格式设置
点击菜单windows->preferences,然后在左侧栏选择java ->Code Style -> CodeTemplates然后在右侧栏选择comments -> 依 ...
- JS点击按钮弹出窗口
由于没有系统学习过JS,遇到一个需求:点击按钮,弹出一个独立的窗口. 在网上百度了一下,并没有找到满意的结果,最重要的是各种方法很复杂.最终,仔细研究了一下,原来只是需要只要一个简单的函数就能满足自己 ...
- Cache-control
网页的缓存是由HTTP消息头中的“Cache-control”来控制的,常见的取值有private.no-cache.max-age.must-revalidate等,默认为private.其作用根据 ...
- 获取布局 ActionBar
LayoutInflater inflater = getLayoutInflater();View imageLayout = inflater.inflate(R.layout.preferenc ...