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 分析 题目要求编写一个 ...
随机推荐
- 注册、卸载DLL
注册.卸载DLL,一般命令写在bat文件中,下面以注册.卸载SWFToImage.DLL为例. 1.注册文件(Install.bat)内容: REM copying files to the syst ...
- (6) 深入理解Java Class文件格式(五)
前情回顾 本专栏的前几篇博文, 对class文件中的常量池进行了详细的解释. 前文讲解了常量池中的7种数据项, 它们分别是: CONSTANT_Utf8_info CONSTANT_NameAndTy ...
- Linq join on 多条件
var a = from m in DbContext.Set<T1>() join q in DbContext.Set<T2>() on new { m.ID, Phone ...
- iOS及Mac开源项目和学习资料【超级全面】
UI 下拉刷新 EGOTableViewPullRefresh – 最早的下拉刷新控件. SVPullToRefresh – 下拉刷新控件. MJRefresh – 仅需一行代码就可以为UITable ...
- [问题2014A03] 复旦高等代数 I(14级)每周一题(第五教学周)
[问题2014A03] 设 \(A=(a_{ij})\) 为 \(n\,(n\geq 3)\) 阶方阵,\(A_{ij}\) 为第 \((i,j)\) 元素 \(a_{ij}\) 在 \(|A|\) ...
- [Python正则表达式] 字符串中xml标签的匹配
现在有一个需求,比如给定如下数据: 0-0-0 0:0:0 #### the 68th annual golden globe awards #### the king s speech earns ...
- monkey中的一些问题
一起来看下导致App Crash的那些原因: 1. 空指针异常:错误日志定位java.lang.NullPointerException,详细日志记录如下 2. 安全异常:错误日志定位 ...
- 《BI项目笔记》增量ETL数据抽取的策略及方法
增量抽取 增量抽取只抽取自上次抽取以来数据库中要抽取的表中新增或修改的数据.在ETL使用过程中.增量抽取较全量抽取应用更广.如何捕获变化的数据是增量抽取的关键.对捕获方法一般有两点要求:准确性,能够将 ...
- C语言中const的正确用法
今天看<Linux内核编程>(Claudia Salzberg Podriguez等著)时,文中(p39)有一个错误,就是关于const的用法. 原文中举例说明:const int *x中 ...
- [Oracle] SQL*Loader 详细使用教程(4)- 字段列表
在上一篇中我们介绍了SQL*Loader中最重要的文件——控制文件,而本篇要介绍控制文件中最重要的部分——字段列表,字段列表的作用是把数据文件中的记录和数据库中表的列对应起来,下面是字段列表的一个例子 ...