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的更多相关文章

  1. CCI_chapter 19 Moderate

    19 1  Write a function to swap a number in place without temporary variables void swap(int &a, i ...

  2. 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 ...

  3. CCI_chapter 13C++

    13.9Write a smart pointer (smart_ptr) class template<class T>class SmartPoint{ public: SmartPo ...

  4. 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 ...

  5. 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 ...

  6. 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, ...

  7. CCI_chapter 2 Linked Lists

    2.1  Write code to remove duplicates from an unsorted linked list /* Link list node */ struct node { ...

随机推荐

  1. MD中bitmap源代码分析--清除流程

    bitmap的清零是由bitmap_daemon_work()来实现的.Raid1守护进程定期执行时调用md_check_recovery,然后md_check_recovery会调用bitmap_d ...

  2. linux mysql默认安装在哪个目录

    MySQL安装完成后不象SQL Server默认安装在一个目录,它的数据库文件.配置文件和命令文件分别在不同的目录,了解这些目录非常重要,尤其对于Linux的初学者,因为 Linux本身的目录结构就比 ...

  3. JBossESB教程(二)——将JBossESB部署到JBossAS中

    前言 上篇讲了JBossESB的环境搭建,但是细心的同学会发现,我们在添加JBoss AS的时候,实际上添加的是jbossesb-server,而这个里面是没有EJB的支持的.如果我们想要使开发环境能 ...

  4. android 初学: 提示No Launcher activity found!

    提示No Launcher activity found! 三步检查: 1 必须有 <category android:name="android.intent.category.LA ...

  5. SpringMVC 拦截器(interceptors)对样式(css),JavaScript(js),图片(images)链接的拦截

    因为在web.xml配置了 <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pa ...

  6. Android笔记(一):从this关键字发散

    this指的是直接包含它的类的实例. 例如: public class MyClass{ int num; public MyClass(int num){ this.num = num; } } 这 ...

  7. Python 获取Facebook用户的Friends的爱好中的Top10

    CODE; #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-12 @author: guaguastd @name: f ...

  8. Android浏览图片,点击放大至全屏效果

    做到照片浏览的功能,对于QQ空间中点击图片放大至全屏,感觉效果很赞,于是也做了个类似的效果.如下. 我不知道QQ那个是怎么做的,我的思路如下: 首先,从图片缩略界面跳转到图片详情页面,应该是从一个Ac ...

  9. jquery之营销系统(会员促销)

    var appPath = getAppPath(); var cnt = 0; var loadCnt = 0; $(function() { $("#opreateHtml") ...

  10. 利用ajax从txt读取数据

    html代码: <div id="news"></div> txt: [ {"id":"1", "news ...