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

字符串问题,需要先确定是不是只有ASCII码。

如果是,可以用char[256],也可以用位向量。位向量的实现参照《编程珠玑》。i&MASK就是取余。i>>SHIFT就是取商。

 class BitVector {
public:
BitVector(int n) {
arr = new int[ + n / INT_LEN];
memset(arr, , ( + n / INT_LEN) * sizeof(int));
} ~BitVector() {delete[] arr;} void set(int i) {
arr[i >> SHIFT] |= ( << (i & MASK));
} void clear(int i) {
arr[i >> SHIFT] &= ~( << (i & MASK));
} bool isSet(int i) {
return (arr[i >> SHIFT] & ( << (i & MASK)));
} private:
int* arr;
enum {INT_LEN = , SHIFT = , MASK = 0x1f};
}; bool unique(char *p) {
bool ch[];
memset(ch, false, sizeof(bool) * );
while (*p) {
if (ch[*p]) return false;
ch[*p] = true;
p++;
}
return true;
} bool unique2(char *p) {
BitVector bv(); while (*p) {
if (bv.isSet(*p)) return false;
bv.set(*p);
p++;
}
return true;
}

1.2 Implement a function void reversefchar* str) in C or C++ which reverses a null-terminated string.

 void reverse(char* str) {
if (str == NULL) return;
int n = strlen(str);
int i = , j = n - ;
while (i < j) {
swap(str[i], str[j]);
i++;
j--;
}
}

1.3 Given two strings, write a method to decide if one is a permutation of the other。

忘了一点,就是要先确认是不是case sensitive和whitespace significant。

 bool isPerm(char* s1, char* s2) {
int n1 = strlen(s1), n2 = strlen(s2);
if (n1 != n2) return false;
sort(s1, s1 + n1);
sort(s2, s2 + n2);
return strcmp(s1, s2) == ;
} bool isPerm2(char* s1, char* s2) {
int ch[];
memset(ch, , sizeof(int) * ); while (*s1) ch[*s1++]++;
while (*s2) {
if (--ch[*s2++] < ) return false;
}
return true;
}

1.4 Write a method to replace all spaces in a string with '%20'. You may assume that the string has sufficient space at the end of the string to hold the additional characters, and that you are given the "true" length of the string.

 char* replaceSpace(char* str) {
int b = ;
char *p = str;
while (*p != '\0') if (*p++ == ' ') b++; char *np = p + (b << );
while (p >= str) {
if (*p == ' ') {
*np-- = '';
*np-- = '';
*np-- = '%';
} else {
*np-- = *p;
}
p--;
}
return str;
}

如果最左和最右的空格不需要替代,那么情况就会复杂一些,不能in place地实现了。

写的有点乱。

 char* replaceSpace2(char* str) {
char *p = str;
while (*p && *p == ' ') p++;
if (!*p) return str;
str = p;
int b = , n = ;
char *t;
while (*p) {
if (*p != ' ') {
n++;
t = p;
} else b++;
p++;
}
b -= (p - t - );
n += b;
char * newstr = new char[n + b * ];
p = newstr;
for (int i = ; i < n; ++i) {
if (str[i] == ' ') {
*p++ = '%';
*p++ = '';
*p++ = '';
} else {
*p++ = str[i];
}
}
*p = '\0';
return newstr;
}

1.5 Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the "compressed" string would not become smaller than the original string, your method should return the original string.

careercup上的code是O(n)时间复杂度和O(n)空间复杂度。另外就是不用stringbuffer的字符串拼接的开销是O(n^2)。

 int digits(int n) {
int i = ;
while (n > ) {
n /= ;
i++;
}
return i;
} int int2str(int n, char *s) {
int i = ;
while (n > ) {
s[i++] = n % + '';
n /= ;
} int k = , m = i - ;
while (k < m) {
swap(s[k], s[m]);
k++;
m--;
}
return i;
} char* compress(char* str) {
if (str == NULL) return str;
char *p = str;
int n1 = , n2 = , n;
while (*p) {
char *t = p;
while (*p && *p == *t) p++;
n = p - t;
n1 += n;
n2 += + digits(n);
}
if (n2 >= n1) return str;
cout << n2 << endl;
char *s = new char[n2];
int i = ;
p = str;
while (*p) {
char *t = p;
while (*p && *p == *t) p++;
n = p - t;
s[i++] = *t;
i += int2str(n, s + i);
}
s[i] = '\0';
return s;
}

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?

Leetcode有,点

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

Leetcode有,点

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 (e.g., "waterbottLe" is a rotation of "erbottLewat").

如果s2是s1旋转而来的,那么s1和s2的长度一定相同,s2一定是s1+s1的子串。反过来亦然。

原命题很显然。逆命题证明如下:

假设s1和s2长度相同,s2是s1+s1的子串。假设s1+s1=x+s2+y,令s2=m+n,使得len(x)+len(m)=len(s1),那么s1+s1=x+m+n+y;

于是有x+m=n+y=s1;且len(y)=len(s1)-len(n)=len(s2)-len(n)=len(m)。同理可证len(x)=len(n)。

因为len(y)=len(m),len(x)=len(n),x+m=n+y=s1,所以有x=n,y=m。于是s1=x+m=x+y,s2=m+n=y+x。所以s2是s1旋转而来的。

 bool isRotate(string s1, string s2) {
if (s1.length() != s2.length()) return false;
string s1s1 = s1 + s1;
return (s1s1.find(s2) != string::npos); //isSubstring
}

Careercup | Chapter 1的更多相关文章

  1. Careercup | Chapter 3

    3.1 Describe how you could use a single array to implement three stacks. Flexible Divisions的方案,当某个栈满 ...

  2. Careercup | Chapter 2

    链表的题里面,快慢指针.双指针用得很多. 2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow w ...

  3. Careercup | Chapter 8

    8.2 Imagine you have a call center with three levels of employees: respondent, manager, and director ...

  4. Careercup | Chapter 7

    7.4 Write methods to implement the multiply, subtract, and divide operations for integers. Use only ...

  5. CareerCup Chapter 9 Sorting and Searching

    9.1 You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. ...

  6. CareerCup chapter 1 Arrays and Strings

    1.Implement an algorithm to determine if a string has all unique characters What if you can not use ...

  7. CareerCup Chapter 4 Trees and Graphs

    struct TreeNode{ int val; TreeNode* left; TreeNode* right; TreeNode(int val):val(val),left(NULL),rig ...

  8. Careercup | Chapter 6

    6.2 There is an 8x8 chess board in which two diagonally opposite corners have been cut off. You are ...

  9. Careercup | Chapter 5

    5.1 You are given two 32-bit numbers, N andM, and two bit positions, i and j. Write a method to inse ...

随机推荐

  1. "帮你"-用户模板和用户场景

    场景/故事/story 典型用户: 用户性质 典型用户介绍 姓名 小李 年龄 20岁 职业 学生 代表的用户在市场上的比例和重要性 代表学校内广大普通学生,因此有很大的重要性. 使用本软件的典型场景 ...

  2. Django基础之Form表单验证

    Form表单验证 1.创建Form类(本质就是正则表达式的集合) from django.forms import Form from django.forms import fields from ...

  3. MySQL时间字段究竟使用INT还是DateTime

    今天解析DEDECMS时发现deder的MYSQL时间字段,都是用 `senddata` ) unsigned '; 随后又在网上找到这篇文章,看来如果时间字段有参与运算,用int更好,一来检索时不用 ...

  4. Careercup - Microsoft面试题 - 5799446021406720

    2014-05-12 07:17 题目链接 原题: Given below is a tree/trie A B c D e F a<b<e<>>c<>d&l ...

  5. IOS开发学习笔记011-xcode使用技巧

    xcode使用技巧 1.自动生成类 2.断点调试 3.代码段保存 4.注释标记 1.新建类,自动生成两个文件和基本结构 第一步  第二步,选择新建一个类,而不是一个源文件  第三步,书写类名一级自己要 ...

  6. MFC深入浅出读书笔记第三部分1

    第八章 Document-View 深入探讨(总结) 1.Document Document 在MFC 的CDocument 里头被具体化.CDocument 本身并无任何具体数据,它只是提供一个空壳 ...

  7. [python][django学习篇][15]博客侧栏--自定义模板标签

    我们的博客侧边栏有四项内容:最新文章.归档.分类和标签云. 这些内容相对比较固定,且在各个页面都会显示,如果像文章列表或者文章详情一样,从视图函数中获取然后传递给模板,则每个页面对应的视图函数里都要写 ...

  8. POJ 2217:Secretary(后缀数组)

    题目大意:求两个字符串的公共子串. 分析: 模板题,将两个字符串接起来用不会出现的字符分割,然后求分属两个字符串的相邻后缀lcp的最大值即可. 代码: program work; type arr=. ...

  9. [HAOI2010][bzoj2424] 订货 [费用流]

    题面 传送门 思路 这题其实挺水的......做过餐巾计划问题就能明白,是同一个道理 首先,显然刚刚好满足每一个月的需求,会得到最优解(废话-_-||) 然后我们发现,货物在不同的月之间的转移,可以比 ...

  10. python网络

    try: import urllib.request as urllib2 except ImportError: import urllib2 response = urllib2.urlopen( ...