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 分析 题目要求编写一个 ...
随机推荐
- oracle 卸载
由于工作需要,重装了一下Oracle,然后发现同SQLServer,MySQL等数据库相比,Oracle的卸载重装真是不一般的麻烦. 整理了一下我的Oracle的卸载重装过程,给自己备忘,同时 ...
- mac攻略(三) -- apache站点配置
Mac OS X 中默认有两个目录可以直接运行你的 Web 程序, 一个是系统级的 Web 根目录:/Library/WebServer/Documents/ 此根目录我们平常使用地址http://l ...
- CentOS7网卡的命名规则
一.前两个字符的含义 en 以太网 Ethernet wl 无线局域网 WLAN ww 无线广域网 WWAN 二.第三个字符的含义 o on-board device index number s h ...
- sql 函数的理解
CAST (expression AS data_type) 用于数据的转化 isnull(@s+',','select years,months,') 判断(@s+',')是否为空,注意点,即使(@ ...
- linux(centos )mongodb install
file down :http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.4.9.tgz /usr/local/ tar zxvf mongo ...
- C#:Hashtable和Dictionary
Dictionary<TKey, TValue> () Hashtable() 第一.存储的数据类型 Hashtable不是泛型的,不是类型安全的:Dictionary是泛型的, ...
- linux python pip包安装
python -m pip install --trusted-host pypi.python.org
- jquery mobile的事件
有个问题困扰了我两天,知道今天才解决. 那就是page的pagecreate事件,只调用一次 如果想随时更新,就要调用pageshow事件,每次都会调用, 这个事情再次告诉我,基础要扎实啊,不然会浪费 ...
- Js获取下拉框选定项的值和文本
Js获取下拉框的值和文本网上提供了2种方法:但有些人很不负责任,他们根本没考虑到浏览器之间的差异导致的错误,导致很多新手琢磨了半天找不出错误! 下面我总结下Firefox和IE下获取下拉框选定项的值和 ...
- 程序设计入门—Java语言 第五周编程题 2井字棋(5分)
2 井字棋(5分) 题目内容: 嗯,就是视频里说的那个井字棋.视频里说了它的基本思路,现在,需要你把它全部实现出来啦. 你的程序先要读入一个整数n,范围是[3,100],这表示井字棋棋盘的边长.比如n ...