博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Careercup | Chapter 1
阅读量:6306 次
发布时间:2019-06-22

本文共 5862 字,大约阅读时间需要 19 分钟。

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就是取商。

1 class BitVector { 2 public: 3     BitVector(int n) { 4         arr = new int[1 + n / INT_LEN]; 5         memset(arr, 0, (1 + n / INT_LEN) * sizeof(int)); 6     } 7  8     ~BitVector() {delete[] arr;} 9 10     void set(int i) {11         arr[i >> SHIFT] |= (1 << (i & MASK));        12     }13 14     void clear(int i) {15         arr[i >> SHIFT] &= ~(1 << (i & MASK));16     }17 18     bool isSet(int i) {19         return (arr[i >> SHIFT] & (1 << (i & MASK)));20     }21 22 private:23     int* arr;24     enum {INT_LEN = 32, SHIFT = 5, MASK = 0x1f};25 };26 27 bool unique(char *p) {28     bool ch[256];29     memset(ch, false, sizeof(bool) * 256);30     while (*p) {31         if (ch[*p]) return false;32         ch[*p] = true;33         p++;34     }35     return true;36 }37 38 bool unique2(char *p) {39     BitVector bv(256);40 41     while (*p) {42         if (bv.isSet(*p)) return false;43         bv.set(*p);    44         p++;45     }46     return true;47 }

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

1 void reverse(char* str) { 2     if (str == NULL) return; 3     int n = strlen(str); 4     int i = 0, j = n - 1; 5     while (i < j) { 6         swap(str[i], str[j]); 7         i++; 8         j--; 9     }10 }

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

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

1 bool isPerm(char* s1, char* s2) { 2     int n1 = strlen(s1), n2 = strlen(s2); 3     if (n1 != n2) return false; 4     sort(s1, s1 + n1); 5     sort(s2, s2 + n2); 6     return strcmp(s1, s2) == 0; 7 } 8  9 bool isPerm2(char* s1, char* s2) {10     int ch[256];11     memset(ch, 0, sizeof(int) * 256);12 13     while (*s1) ch[*s1++]++;14     while (*s2) {15         if (--ch[*s2++] < 0) return false;16     }17     return true;18 }

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. 

1 char* replaceSpace(char* str) { 2     int b = 0; 3     char *p = str; 4     while (*p != '\0') if (*p++ == ' ') b++; 5  6     char *np = p + (b << 1); 7     while (p >= str) { 8         if (*p == ' ') { 9             *np-- = '0';10             *np-- = '2';11             *np-- = '%';12         } else {13             *np-- = *p;14         }15         p--;16     }17     return str;18 }

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

写的有点乱。

1 char* replaceSpace2(char* str) { 2     char *p = str; 3     while (*p && *p == ' ') p++; 4     if (!*p) return str; 5     str = p; 6     int b = 0, n = 0; 7     char *t; 8     while (*p) { 9         if (*p != ' ')  {10             n++;11             t = p;12         } else b++;13         p++;14     }15     b -= (p - t - 1);16     n += b;17     char * newstr = new char[n + b * 2];18     p = newstr;19     for (int i = 0; i < n; ++i) {20         if (str[i] == ' ') {21             *p++ = '%';22             *p++ = '2';23             *p++ = '0';24         } else {25             *p++ = str[i];26         }27     }28     *p = '\0';29     return newstr;30 }

 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)。

1 int digits(int n) { 2     int i = 0; 3     while (n > 0) { 4         n /= 10; 5         i++; 6     } 7     return i; 8 } 9 10 int int2str(int n, char *s) {11     int i = 0;12     while (n > 0) {13         s[i++] = n % 10 + '0';14         n /= 10;15     }16 17     int k = 0, m = i - 1;18     while (k < m) {19         swap(s[k], s[m]);20         k++;21         m--;22     }23     return i;24 }25 26 char* compress(char* str) {27     if (str == NULL) return str;28     char *p = str;29     int n1 = 0, n2 = 0, n;30     while (*p) {31         char *t = p;32         while (*p && *p == *t) p++;33         n = p - t;34         n1 += n;35         n2 += 1 + digits(n);36     }37     if (n2 >= n1) return str;38     cout << n2 << endl;39     char *s = new char[n2];40     int i = 0;41     p = str;42     while (*p) {43         char *t = p;44         while (*p && *p == *t) p++;45         n = p - t;46         s[i++] = *t;47         i += int2str(n, s + i);48     }49     s[i] = '\0';50     return s;51 }

 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旋转而来的。

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

 

 

转载于:https://www.cnblogs.com/linyx/p/3772631.html

你可能感兴趣的文章
如何在 Ubuntu Linux 16.04 LTS 中使用多个连接加速 apt-get/apt
查看>>
《OpenACC并行编程实战》—— 导读
查看>>
机器学习:用初等数学解读逻辑回归
查看>>
如何在 Ubuntu 中管理和使用逻辑卷管理 LVM
查看>>
Oracle原厂老兵:从负面案例看Hint的最佳使用方式
查看>>
把自己Github上的代码添加Cocoapods支持
查看>>
C语言OJ项目参考(2493)四则运算
查看>>
零基础入门深度学习(二):神经网络和反向传播算法
查看>>
find和xargs
查看>>
数据结构例程—— 交换排序之快速排序
查看>>
WKWebView代理方法解析
查看>>
IOS定位服务的应用
查看>>
[SMS&WAP]实例讲解制作OTA短信来自动配置手机WAP书签[附源码]
查看>>
IOS中图片(UIImage)拉伸技巧
查看>>
【工具】系统性能查看工具 dstat
查看>>
基于zepto或jquery的手机端弹出框成功,失败,加载特效
查看>>
php引用(&)
查看>>
关押罪犯
查看>>
k8s-高可用架构设计
查看>>
第93天:CSS3 中边框详解
查看>>