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); //isSubstring
5 }

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

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 4

    二叉查换树,左孩子小于等于根,右孩子大于根. 完全二叉树,除最后一层外,每一层上的节点数均达到最大值:在最后一层上只缺少右边的若干结点. complete binary tree 满二叉树,完美二叉树 ...

  3. [CareerCup] 11.2 Sort Anagrams Array 异位词数组排序

    11.2 Write a method to sort an array of strings so that all the anagrams are next to each other. 这道题 ...

  4. python使用fpdf生成pdf文件章节(chapter),包含:页眉、页脚、章节主题、数据排版等;

    python使用fpdf生成pdf文件章节(chapter),包含:页眉.页脚.章节主题.数据排版等: #仿真数据 The year 1866 was marked by a bizarre deve ...

  5. python使用fpdf生成pdf章节(chapter)文件包含:页眉、页脚、章节主体、章节内容等;

    python使用fpdf生成pdf章节(chapter)文件包含:页眉.页脚.章节主体.章节内容等: 目录

  6. Chapter 1 快速搭建-服务的注册与发现(Eureka)

    Chapter 1 快速搭建-服务的注册与发现(Eureka) 一.Spring Cloud简介 为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智能路由,微代 ...

  7. 吴恩达Coursera机器学习 - Chapter 4 多变量线性回归

    Chapter 4 – 多变量线性回归(Linear Regression with Multiple Variables) 相比第二章,这一章无非就是数据集的特征数由一个变为多个,并引入了矩阵的概念 ...

  8. Chapter 1 Securing Your Server and Network(9):使用Kerberos用于身份验证

    原文: Chapter 1 Securing Your Server and Network(9):使用Kerberos用于身份验证 原文出处:http://blog.csdn.net/dba_hua ...

  9. [CareerCup] 17.7 English Phrase Describe Integer 英文单词表示数字

    17.7 Given any integer, print an English phrase that describes the integer (e.g., "One Thousand ...

最新文章

  1. 可以查到的资料和可以淘到的原件 DIY 四轴
  2. 电脑屏保在哪里设置_超火的时钟屏保,有点个性!
  3. step2 . day1 Linux和C语言的高级应用
  4. 打印的图片不清晰_如何调节图片kb,但又不改变图片的清晰度?
  5. hibernate状态_Hibernate状态的自然身份证
  6. 合泰单片机做电压表_启士 | 蓝桥杯零基础单片机教程9 I2C介绍(上)
  7. SQL where 条件顺序对性能的影响有哪些
  8. Star Schema完全参考手册读书笔记三
  9. 基于Spark的电影推荐系统(电影网站)
  10. Android四大组件之Activity组件
  11. Video Copilot VCReflect for Mac/win (AE倒影插件) 支持2022多帧渲染​
  12. 智鼎在线测评是测什么_人才测评工具和人才测评方法
  13. 安装bzz1.0教程
  14. 硬件学习之滤波电容的阻抗特性
  15. docer 设置 拉取http协议的私有仓库
  16. linux系统防火墙关闭22端口,Linux系统防火墙关闭及端口开放
  17. 96Boards MIPI CSI Camera Mezzanine V2.1
  18. 计算广告概述【计算广告】
  19. html文件只能打印一页,javascript – 使用window.print()打印巨大的表只打印一页
  20. 启动vue项目出现的错误合集

热门文章

  1. wpf checkbox选中触发事件_Cypress 可操作事件
  2. SDOI2014 LIS
  3. 【软件工程】第0次个人作业
  4. JS数组去重的6种算法实现
  5. 填个小坑,Vue不支持IE8及以下,跨域ajax不支持IE9
  6. 科普:WiFi是谁申请的专利?高通吗?错!
  7. Effective Java 阅读笔记——方法
  8. oracle稳定执行计划1
  9. UNIQUE和PRIMARY 约束的区别
  10. yum卸载遇到的问题--待解决