题目:

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

// 不允许使用额外数据结构判断一个字符串中是否所有的字符都不相同
//char * const cp : 定义一个指向字符的指针常数,即const指针
//const char* p : 定义一个指向字符常数的指针
//char const* p : 等同于const char* p #include <cstdio>
#include <cstring>
#include <bitset> bool check1(char const * str) {
bool c[] = {false};
int len = strlen(str);
for (int i = ; i < len; ++i) {
if (!c[str[i]]) c[str[i]] = true;
else return false;
}
return true;
} bool check2(char const * str) {
int bit = ;
int len = strlen(str);
for (int i = ; i < len; ++i) {
// printf("%d\n", str[i] - 'a');
int v = str[i] - 'a';
if (bit & ( << v)) return false;
else bit |= << v;
}
return true;
} bool check3(char const * str) {
std::bitset<> bit;
// for (int i = 0;i <256;++i) printf("%d ", (int) bit[i]);
int len = strlen(str);
for (int i = ; i < len; ++i) {
if (bit[str[i]]) return false;
else bit.set(str[i]);
}
return true;
} bool check4(char const * str) {
int bit[] = {};
int len = strlen(str);
for (int i = ; i < len; ++i) {
int v = (int) str[i];
int idx = v / , shift = v % ;
if (bit[idx] & ( << shift)) return false;
else bit[idx] |= << shift;
}
return true;
} int main() {
char *t1 = "asdsdfff";
char *t2 = "asdksdas";
char *t3 = "wkjpul";
char *t4 = "anmzxsfghij";
char *t5 = "%+-anmzxsfghij";
printf("test %s %d\n", t1, check1(t1));
printf("test %s %d\n", t2, check1(t2));
printf("test %s %d\n", t3, check1(t3));
printf("test %s %d\n", t4, check1(t4));
printf("test %s %d\n", t1, check2(t1));
printf("test %s %d\n", t2, check2(t2));
printf("test %s %d\n", t3, check2(t3));
printf("test %s %d\n", t4, check2(t4));
printf("test %s %d\n", t5, check3(t5));
printf("test %s %d\n", t5, check4(t5));
return ;
}

pg95 1.2 Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.) _________________________________________________________________

http://www.cnblogs.com/chenchenluo/p/3522195.html

这个教训终身难忘。

#include <cstdio>
#include <cstring> char* reverse(char * str) {
char *ret = str, *end = str;
if (str) {
while (*end) {
++end;
}
--end;
while (str < end) {
char tmp = *str;
*str++ = *end;
*end-- = tmp;
}
}
return ret;
} char* reverse2(char *str) {
char *ret = str;
int s = , e = strlen(str) - ;
while (s < e) {
char tmp = str[s];
str[s++] = str[e];
str[e--] = tmp;
}
return ret;
} int main() {
char t1[] = "23123/0)*(&^&*$%^$^%asdsdfff";
char t2[] = "a3234][\\sdksdaacas";
char t3[] = "/.,';]][[])(*wkjpul";
char t4[] = "21['./a2na34324mzxsfghij";
char t5[] = "%+-anmzxsfghij";
reverse(t1);
printf("test %s\n", t1);
printf("test %s %s\n", t2, reverse(t2));
printf("test %s %s\n", t3, reverse2(t3));
printf("test %s %s\n", t4, reverse(t4));
printf("test %s %s\n", t5, reverse2(t5));
return ;
}

pg96 1.3 Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional variables are fine. An extra copy of the array is not. FOLLOW UP Write the test cases for this method. _________________________________________________________________

pg97 1.4 Write a method to decide if two strings are anagrams or not.

_________________________________________________________________

pg 99 1.5 Write a method to replace all spaces in a string with ‘%20’.

________________________________________________________________

pg 100 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?

________________________________________________________________

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

pg 102 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”).

代码:

11-10 CC150第一章的更多相关文章

  1. 《Django By Example》第一章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:本人目前在杭州某家互联网公司工作, ...

  2. Nova PhoneGap框架 第一章 前言

    Nova PhoneGap Framework诞生于2012年11月,从第一个版本的发布到现在,这个框架经历了多个项目的考验.一直以来我们也持续更新这个框架,使其不断完善.到现在,这个框架已比较稳定了 ...

  3. 第一章 Java多线程技能

    1.初步了解"进程"."线程"."多线程" 说到多线程,大多都会联系到"进程"和"线程".那么这两者 ...

  4. 《JavaScript高级程序设计(第3版)》阅读总结记录第一章之JavaScript简介

    前言: 为什么会想到把<JavaScript 高级程序设计(第 3 版)>总结记录呢,之前写过一篇博客,研究的轮播效果,后来又去看了<JavaScript 高级程序设计(第3版)&g ...

  5. 第一章.C语言简介

    C语言第一章 C语言简介   目录 一.C语言介绍 二.C语言特点 三.Hello World 四.转义符 五.占位符 六.俄罗斯方块游戏 七.文件下载 一.C语言介绍 C是一种通用的编程语言,广泛用 ...

  6. 第一章 数据库概述、MySQL的安装和配置

      第一章 数据库概述.MySQL的安装和配置   1.为什么要使用数据库 最早是纸质文件来存储数据 缺点:不易保存,占用空间大 计算机出现以后,采用软件来进行保存(excel) 缺点:容易损坏 文件 ...

  7. SICP— 第一章 构造过程抽象

    SICP  Structure And Interpretation Of Computer Programs 中文第2版 分两部分  S 和 I 第一章 构造过程抽象 1,程序设计的基本元素 2,过 ...

  8. PRML读书会第一章 Introduction(机器学习基本概念、学习理论、模型选择、维灾等)

    主讲人 常象宇 大家好,我是likrain,本来我和网神说的是我可以作为机动,大家不想讲哪里我可以试试,结果大家不想讲第一章.估计都是大神觉得第一章比较简单,所以就由我来吧.我的背景是统计与数学,稍懂 ...

  9. [置顶] 【其他部分 第一章 矩阵】The C Programming Language 程序研究 【持续更新】

    其他部分 第一章 矩阵 一.矩阵的转置   问题描述: 编写函数,把给定的任意一个二维整型矩阵转换为其转置矩阵. 输入: 1 2 3 4 5 6 输出: 1 4 2 5 3 6 分析 题目要求编写一个 ...

随机推荐

  1. UIButton设置圆角和边框及边框颜色

    1. 按钮边框颜色 //设置边框颜色 [btn.layer setMasksToBounds:YES]; [btn.layer setCornerRadius:10.0]; //设置矩形四个圆角半径 ...

  2. java中快速排序的理解以及实例

    所谓的快速排序的思想就是,首先把数组的第一个数拿出来做为一个key,在前后分别设置一个i,j做为标识,然后拿这个key对这个数组从后面往前遍历,及j--,直到找到第一个小于这个key的那个数,然后交换 ...

  3. WordCount示例深度学习MapReduce过程(1)

    我们都安装完Hadoop之后,按照一些案例先要跑一个WourdCount程序,来测试Hadoop安装是否成功.在终端中用命令创建一个文件夹,简单的向两个文件中各写入一段话,然后运行Hadoop,Wou ...

  4. Network-POJ3694并查集+LCA

    Network Time Limit: 5000MS   Memory Limit: 65536K       Description A network administrator manages ...

  5. Auty自动化测试框架第六篇——垃圾代码回收、添加suite支持

    [本文出自天外归云的博客园] 垃圾代码回收 添加脚本恢复机制,因为框架会自动生成一些代码,如果代码生成后出现问题导致代码没有正常删除掉,则会造成代码垃圾,在auty目录添加recovery.py文件: ...

  6. 如何使用VS2013对C++进行编程

    https://msdn.microsoft.com/zh-cn/library/bb384842.aspx

  7. Cannot find class for bean with name '/hello' defined in ServletContext resource

    Cannot find class for bean with name '/hello' defined in ServletContext resource [/WEB-INF/chapter2- ...

  8. InfoSet

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  9. How to install .deb file in Ubuntu

    if you have a .deb file: You can install it using sudo dpkg -i /path/to/deb/file followed by sudo ap ...

  10. php : 单例设计演示

    单例 : 保证只有一个实例 <?php /* * 单例设计 */ // 单例: 只能"创造"出它的一个对象实例 class Single{ // 第一步: 私有化构造方法 p ...