- 博客(15)
- 资源 (4)
- 收藏
- 关注
原创 CareerCup1.7
Question1.7: Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0. 1. 遍历一遍,记录下需要置零的行和列 2. 对已记录的行和列置零 代码: #include #include bool ReadFile(in
2013-02-23 15:35:06
462
原创 CareerCup1.6
Question1.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? 从最外层到最里层逐层反转 顺时针的话:
2013-02-23 15:33:23
467
原创 CareerCup1.5
Question1.5: Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2b1c5a3. If the "compressed" string woul
2013-02-23 15:31:10
509
原创 CareerCup1.4
Question1.4: Write a method to replace all spaces in a string with '%20'. You may assume that the string has sufficient space at the end of the string to hold the additional characters, and that you
2013-02-23 15:29:17
455
原创 CareerCup1.3
Question1.3: Given two strings, write a method to decide if one is a permutation of the other. 采用一个256大小的int型数组来计算string中每个char所出现的次数,初始化为0,对第一个string出现一次就+1,对第二个string出现一次就-1 遍历完两个string后检查数组中的int
2013-02-23 15:27:00
485
原创 CareerCup1.2
Cracking the Coding Interview 习题1.2: Implement a function void reverse(char* str) in C or C++ which reverses a null-terminated string. 定义一个头指针一个尾指针,不停地交换他们指向的元素并且头指针++,尾指针-- 代码: #include "stdafx.h
2013-02-23 15:23:12
490
原创 CareerCup1.1
Cracking the coding Interview 习题1.1: Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures? 代码: #include "stdafx.h" #inclu
2013-02-23 15:18:14
455
原创 寻找最大重复子字符串
来自于CareerCup,原题: Given a string, find the longest substring that occurs more than once. Overlapping is allowed. 这道题感觉利用类似于KMP算法比较好,可惜KMP算法不是很熟,更别提根据这道题修改了,先把做法写在这里,等温故了KMP再回来改进: #include
2013-02-15 21:47:33
432
原创 改变链表顺序
出自于CareerCup,原题如下: Suppose that we have a sorted singly linked list with integer values. For example: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 We want to change the pointers of this linked list so that it b
2013-02-14 21:12:37
768
原创 不使用额外空间字符串去重
题目:要求在不使用额外空间(一两个变量的空间还是允许的)的情况下去除一个string中重复的char,特别使用另外一个大小相当于目标string的数组的额外空间是绝对不允许的。 原先想记录下string中出现过的char然后在后面如果有相同的char则去除的,但是这样如果string中重复的较少的话就会使用到较大的额外空间,与题意不符。 只能一遍遍的循环判断string中的重复字符并去除。从第
2013-02-11 11:10:53
991
原创 不使用四则运算符计算两数之和
来自于Cracking the Coding Interview,挺有意思的一道题 先转换成二进制然后再一位位相加,因为二进制只有0和1所以可以列举各种可能性而不用用到+号,解法如下: #include "stdafx.h" #include #include #include using namespace std; int _tmain(int argc, _TCHAR*
2013-02-10 18:34:27
796
原创 寻找丑数
题目:我们把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。习惯上我们把1当做是第一个丑数。求出第n个丑数 最容易想到的是从1开始依次判断是否为丑数并计数,但基于丑数的增长速度快当n较大时程序的运行效率会非常缓慢。 我利用丑数必定是若干个2,3,5的乘积,维护一个顺序的丑数数组,而这个数组的第n个元素即是第n个丑数。
2013-02-09 13:33:32
411
原创 添加最少括号匹配给定括号字符串
同样也是查括号匹配问题是引出的,原题好像是一道ACM吧: 给定包含'('')''['']'的一串字符串,要求出至少必须添加多少括号才能让所有括号匹配。 知道应该是用动态规划的,但想了很久也没想出做法,最后还是看了别人的分析后才实现的 动态规划数组所中的a[i][j]代表的是要让字符串索引从i到j的子字符串中括号匹配所要添加的最少括号数 而a[i][j+1]的一种可能是a[i][j]
2013-02-08 19:55:31
2114
原创 判断括号是否匹配
挺简单的问题,判断一串包含'('')''['']'括号的字符串是否匹配,在查打印匹配括号问题时查到的,堆栈好久不用了权当熟悉一下吧: #include #include #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { string pairstr; cin>>pairstr; sta
2013-02-07 21:02:56
2315
原创 打印匹配括号
开始抽空做一些算法题,留下记录作为菜鸟的成长见证吧。 这道题来自于Cracking the Coding Interview, 要求打印n对括号的所有可能匹配。 我采用递归来做,这样编写比较方便而且容易弄懂,但用迭代应该会效率快不少而且递归一定能转换成迭代吧,以后有空研究下 代码如下: #include "stdafx.h" #include #include #include
2013-02-04 20:20:48
649
[英文原版计算机信息技术图书].68.Sybex.Complete.Java.2.Certification.Study.Guide.Apr.2005.eBook-DDU.pdf
2010-04-04
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人