题目:

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. 循环多次ajax请求

    最近在做网页前端,其中有个功能按钮是从数据表格中同时删除多条数据,涉及到循环多次ajax请求 但是老是出现一些请求被忽视的情况,应该是由于for循环在极短时间内被完成,所以第一次请求后的几次请求时,x ...

  2. nwjs如何打包文件为exe文件并修改exe图标

    1.下载nw.js,如果是SDK版的可以调试页面,打包后可不可以调试还没有试,不是SDK的话没有调试选项,试了一下,打包后的文件也一样调试不了. 2.把要打包的文件和package.json都放在nw ...

  3. HashCode

    如果一个类的对象要用做hashMap的key,那么一定要注意覆盖该类的equals和hashCode方法. equals()是基类Object的方法,用于判断对象是否有相同地址及是否为同一对象 pub ...

  4. hadoop2.0初识1.1

    1.伪分布式hdfs文件系统的搭建(单节点文件系统) 1.1.根据上节的讲解,配置主机映射.jdk和解压hadoop压缩包 1.2.配置namenode 在/opt/modules/hadoop-2. ...

  5. VirtualBox4.3.12 Centos6.5-i386 设置共享文件夹

    新在虚拟机下安装个CentOS6.5,准备设置个与win7的共享文件夹,遇到一个问题,搞了好几天呢 现在先说一下: 首先,在虚拟机下安装好CentOS这里不说了 然后启动,点击安装增强功能 如下图: ...

  6. Java面向对象三大特点之继承

    概念: 继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为. 生活中的继承: 兔子和羊属于食草动物类,狮子和豹属于食肉动物类 ...

  7. JavaEE开发环境搭建(1)---(jdk的安装)

    ----------我是前言---------- 有时候从网上找资料真的是一件很开(dan)心(teng)的事情, 因为很多你想要了解的东西总是很巧妙的被略去了... 或者... 根本没有... 真是 ...

  8. php_match/preg_match_all 默认有字符串长度限制

    php_match/preg_match_all 默认有字符串长度限制:52500(或许你的服务器环境是更长,或者更短),当字符串长度大于52500,只能匹配到52500数据,超出的部分会被系统自己截 ...

  9. Windows 7 封装与定制不完全教程

    Windows 7 封装与定制不完全教程 从定制Win7母盘到封装详细教程 手把手教你定制WIN7小母盘 Windows 7 封装与定制不完全教程 [教程] Windows 7 封装与定制不完全教程( ...

  10. openssl evp 哈希算法(md5,sha1,sha256)

    1. 简述 openssl提供了丰富密码学工具,一些常用的哈希算法 比如md5,sha 可以直接用提供的md5.h ,sha.h 接口使用: 为了方便开发者使用,openssl 又提供了一个EVP, ...