11-10 CC150第一章
题目:
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第一章的更多相关文章
- 《Django By Example》第一章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:本人目前在杭州某家互联网公司工作, ...
- Nova PhoneGap框架 第一章 前言
Nova PhoneGap Framework诞生于2012年11月,从第一个版本的发布到现在,这个框架经历了多个项目的考验.一直以来我们也持续更新这个框架,使其不断完善.到现在,这个框架已比较稳定了 ...
- 第一章 Java多线程技能
1.初步了解"进程"."线程"."多线程" 说到多线程,大多都会联系到"进程"和"线程".那么这两者 ...
- 《JavaScript高级程序设计(第3版)》阅读总结记录第一章之JavaScript简介
前言: 为什么会想到把<JavaScript 高级程序设计(第 3 版)>总结记录呢,之前写过一篇博客,研究的轮播效果,后来又去看了<JavaScript 高级程序设计(第3版)&g ...
- 第一章.C语言简介
C语言第一章 C语言简介 目录 一.C语言介绍 二.C语言特点 三.Hello World 四.转义符 五.占位符 六.俄罗斯方块游戏 七.文件下载 一.C语言介绍 C是一种通用的编程语言,广泛用 ...
- 第一章 数据库概述、MySQL的安装和配置
第一章 数据库概述.MySQL的安装和配置 1.为什么要使用数据库 最早是纸质文件来存储数据 缺点:不易保存,占用空间大 计算机出现以后,采用软件来进行保存(excel) 缺点:容易损坏 文件 ...
- SICP— 第一章 构造过程抽象
SICP Structure And Interpretation Of Computer Programs 中文第2版 分两部分 S 和 I 第一章 构造过程抽象 1,程序设计的基本元素 2,过 ...
- PRML读书会第一章 Introduction(机器学习基本概念、学习理论、模型选择、维灾等)
主讲人 常象宇 大家好,我是likrain,本来我和网神说的是我可以作为机动,大家不想讲哪里我可以试试,结果大家不想讲第一章.估计都是大神觉得第一章比较简单,所以就由我来吧.我的背景是统计与数学,稍懂 ...
- [置顶] 【其他部分 第一章 矩阵】The C Programming Language 程序研究 【持续更新】
其他部分 第一章 矩阵 一.矩阵的转置 问题描述: 编写函数,把给定的任意一个二维整型矩阵转换为其转置矩阵. 输入: 1 2 3 4 5 6 输出: 1 4 2 5 3 6 分析 题目要求编写一个 ...
随机推荐
- SSI-Server Side Inclued
SSI是指将内容发送到浏览器之前,可以使用“服务器端包含 (SSI)”指令将文本.图形或应用程序信息包含到网页中. IIS.Apache等主流web服务器都支持,cassini不支持.它并不经过asp ...
- 2x2矩阵相乘模版
由于Unity只有4x4矩阵,今天要做一个2x2矩阵的旋转,居然忘了顺序.故写下作为模版记录. 顺序: 下面是使用其进行旋转的C#代码: public struct Position { public ...
- crontab 的使用
1. 创建一个文件 mycrontab 2. 将此文件运用到系统的定时器中 crontab mycrontab 3. crontab -e (或直接编辑 mycrontab, 但 ...
- [问题2014A04] 解答
[问题2014A04] 解答 (1) 由条件可得 \(AB+BA=0\), 即 \(AB=-BA\), 因此 \[AB=A^2B=A(AB)=A(-BA)=-(AB)A=-(-BA)A=BA^2=B ...
- XML学习笔记(三) -- Schema
标签(空格分隔): 学习笔记 Schema的格式 XML Schema文档是由元素.属性.命名空间和XML文档中的其他节点构成的. XML Schema有两种重要的Schema模型:Microsoft ...
- Sprint(第七天11.20)
燃尽图
- <a>每次点击都会让浏览器重新打开一个窗口问题
<a> 标签的 target 属性规定在何处打开链接文档.如果在一个 <a> 标签内包含一个 target 属性,浏览器将会载入和显示用这个标签的 href 属性命名的.名称与 ...
- const char* && string && String^ 类型转换
const char* && string && String^ 类型转换 const char* ---> string const char * cw= &q ...
- socket初级使用(客户端)
在国庆这段时间里用零星的一些时间看了一下socket的学习资料,由于笔者偏向学习实用方面的内容,因此此篇文章涉及理论知识较少,主要是以实现思路(怎么做)为主,但在实现之前还是需要了解一些基础的理论知识 ...
- 探究chrome下的开发工具的各功能
F12 一.网页寻找js事件的位置,或者某一个次究竟在哪个相关的文件中可以全局搜索: ①控制台右上角的:(Customize and control DevTools)三个点点开,---->Se ...