![](https://img-blog.csdnimg.cn/20201014180756930.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
ctci
文章平均质量分 74
纯黑老白
这个作者很懒,什么都没留下…
展开
-
Ch1-5: Write a method to replace all spaces in a string with ‘%20’.
在剑指offer里面有提到这道题:Write a method to replace all spaces in a string with ‘%20’. 原因是server不能识别空格或者一些字符,所以将data send 给server前要将空格换成一些能识别字符(就像正则一样,有时候为了以后的模式识别,故意增加一些换行,---,%20之类的features,作为flag方便以后提取)。#原创 2013-12-30 07:01:17 · 802 阅读 · 0 评论 -
Ch1-7: clear the row i and column j iff a[i][j]==0
in this problem, both monish001 and hawstein use O(n^2) solution which first tranverse the char** a to find the a[i][j]==0 and mark that row/col to be: TRUE.Then the send loop make the TURE row/col原创 2013-12-31 06:04:27 · 1110 阅读 · 0 评论 -
Ch3-6: sorting a stack in an ascending order
if only use 1 additional stack, then mimic the selection sort.or with 2 additional stacks, we can mimic merge sort or quick sort.Full code:// solution for Ch3-6 by mimicing selection sort原创 2014-01-15 17:49:29 · 659 阅读 · 0 评论 -
cc150做题计划
计划:12.29.2013 - 1.29.2013 做完第一遍题目,然后2月做leetcode。2月底面试前完成200题C++。这里记录一下ctci学习的参考资料。1. hawstein的blog2. https://github.com/ChengTian/CtCI3. absolute C++ 5th4. Primer C++5. learncpp:http原创 2014-01-14 08:45:30 · 2734 阅读 · 0 评论 -
Ch3-5: implement myqueue with 2 stacks
since Stack is LIFO, Queue is FIFO;in order to implement LIFO, need to have a stack1 to buff the data and back to stack2 for pop. it is just like cooling the hot water with 2 cups, it can reverse th原创 2014-01-15 16:04:39 · 554 阅读 · 0 评论 -
ch4.6: find the LCA of 2 nodes in a binary tree with 3 different methods
In this problem, it is important to fully understand the definition of least common ancestor of 2 nodes:it means: (from leetcode blog: 点击打开链接) _______3______ / \原创 2014-01-24 09:43:32 · 1201 阅读 · 0 评论 -
ch4.7: given 2 huge Binary tree t1, t2(>1millon nodes), decide if one tree is subtree of another one
// Ch4.7: You have two very large binary trees: T1, with millions of nodes, and T2, // with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1#include #include #includ原创 2014-01-25 12:37:56 · 946 阅读 · 0 评论 -
1/22/2014: 纪念第一个C++ programmer电面--F家
经过20天的CC刷题(其实是学习),才刚刚做到第四章。看看其他非ACM牛人,都是leetcode,CC,EPI滚瓜烂熟才去面。而且onsite确实是靠实力,要有做过大project之后才能真正融会贯通C++精神。Anyway,谢谢郭志给的refer,不然连电面的机会都没有。开头是C++基础题:1. Fib的2种写法:recursive和DP2. Struct和Class的区别;在C原创 2014-01-24 09:53:39 · 1127 阅读 · 0 评论 -
Ch4-1: identify if a tree is "balanced" (注:CTCI 第四版和第五版的定义不一样)
To identify if the most different leaves, it is: If So we need to traverse the tree to find the leaves' depth, so we pick 1 of 3 traverse oders: the inorder.In order to implement a tree, need a s原创 2014-01-19 12:03:36 · 761 阅读 · 0 评论 -
Ch4.8: find all the path that a Binary tree sums up to a given value, may/maynot have parent pointer
In this problem, we should consider the path to the bottom, instead of hit-and-run.For example:The given sums up value is 8:then these are all correct paths:8;8=-2=2;2=0=6;2=0=6=-1=1;原创 2014-01-27 05:54:35 · 638 阅读 · 0 评论 -
Ch8.3: find all the subsets of a set
In this problem, We can still use the power of BINARY number to represent the nth number is shown or not.For example:{2, 0, 1, 4}, if I use _ _ _ _ (4 bits) to represent each numbers. Then I can h原创 2014-01-30 04:01:44 · 802 阅读 · 0 评论 -
ch8-4: find the permutations of a string
This is a similar problem to ch 8.3.In recursion, we can think a little bit different: so here are two recursion version://Write a method to compute all permutations of a string// http://www.chao原创 2014-01-30 06:31:18 · 799 阅读 · 0 评论 -
Ch8.8: 8 queen problem
In order to solve this problem efficiently,need to use backtracking method instead of iterations. Aka: Backtracking is a general algorithm for finding all (or some) solutions to some computational p原创 2014-02-02 08:15:41 · 829 阅读 · 0 评论 -
Ch8.6: write a method to paint-fill a closed ring with the same color
Implement the “paint fill” function that one might see on many image editing programs. That is, given a screen (represented by a 2-dimensional array of Colors), a point, and a new color, fill in the s转载 2014-02-02 06:54:06 · 701 阅读 · 0 评论 -
ch9.3: binary search a number from a sorted & twisted array
Given a sorted array of n integers that has been rotated an unknown number of times, give an O(log n) algorithm that finds an element in the array. You may assume that the array was originally sorte原创 2014-02-04 09:37:50 · 624 阅读 · 0 评论 -
复习一下dfs和bfs 用java
dfs 和 bfs都是用来找从source 到 target的path的算法。dfs用的是recursion,bfs则用queue原创 2014-04-30 01:24:15 · 1024 阅读 · 1 评论 -
Ch4.4: array to Balanced tree and print each level
Q: an ordered array is given, need to convert into a balanced binary tree and then BFSly print each level as a D-dimentional linked listNeed to understand list, vector, BFS, recursion, scope of memb原创 2014-01-22 17:16:41 · 627 阅读 · 0 评论 -
Ch3-6参考资料:C++的7种排序
看看对不对,再学习。#include #include using namespace std;//C++經典的7種排序演算法/*參考網頁:: http://www.caogenit.com/caogenxueyuan/yingyongfangxiang/rengongzhineng/1724.html*////////////////////////////转载 2014-01-15 16:24:27 · 524 阅读 · 0 评论 -
Ch1.6: IN-place-ly rotate a pic(NxN matrix) by 90 digree in Verilog(CPP)
这个有点像DSP FPGA Chang Choo的题目,一般是放大插值,DFT,IIR之类的Verilog题目。题目在这: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.原创 2013-12-31 05:38:08 · 1158 阅读 · 0 评论 -
Ch1-4: anagram string
1. The simplest method is to sort these strings (O(nlogn)) and then compare them(O(n)).bool isAnagram1(string s, string t){ // why cannot use &? if(s=="" || t=="") return false; if(s.lengt原创 2013-12-29 09:36:20 · 774 阅读 · 0 评论 -
CH1-3: remove duplicate char in a string, with/without additional buffer
1. 记录各个字符出现情况的bool型scoreboard可以保存下标大于size的吗?eg: bool a[256]; //cuz it stored the ASCII memset(a,0,sizeof(a));void block(char* s){ const int* curptr = s; for (int i = 0; i<len; ++i){原创 2013-12-29 05:59:30 · 810 阅读 · 0 评论 -
Ch2-1: de-duplicate a linked-list with/without additional buffer
Chap 2 deal with linked-list. So I finished XuetangCh2-1:The different is the time complexity and the reason we use 2 poniters/3 pointers:p is the base addr, q is the increased pointer;c is th原创 2014-01-05 07:01:40 · 781 阅读 · 0 评论 -
Ch2-2: return the nth to the last node data of a singly linked list
In this problem, I need to find the nth to the last node data of a singly linked list. In order to do so, one good way is to use recursion, since it is automatically put recursion part into stack an原创 2014-01-05 13:56:51 · 1160 阅读 · 0 评论 -
Ch2-3: remove the middle node in a singly linked list
Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node.EXAMPLEInput: the node ‘c’ from the linked list a->b->c->d->e Result: nothing is retur原创 2014-01-10 10:19:09 · 839 阅读 · 0 评论 -
Ch2-4:add reverse 1 digit format number in linked list
You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1’s digit is at the head of the list. Write a function原创 2014-01-11 09:06:13 · 797 阅读 · 0 评论 -
Ch2-5: find the beginning of loop in a circular linked list---two solutions
Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.DEFINITIONCircular linked list: A (corrupt) linked list in which a node’s next pointer poin原创 2014-01-11 09:48:28 · 851 阅读 · 0 评论 -
Ch3-1: use a single array to implement three stacks.
Here it explains why Hawstein use this in his constructor, because he use size a parameter in his constructor while "size" is also a member variable(it's private), so he use this->size = size, explici原创 2014-01-13 06:57:13 · 989 阅读 · 0 评论 -
Ch3-2: implement min() for a stack with O(1)
Here will show the thinking flow from bad to good.idea 1:const int MAX_INT = ~(1<<31);//2147483647struct node{ int val, min;};class StackWithMin{public: StackWithMin(int size){原创 2014-01-13 17:13:24 · 859 阅读 · 0 评论 -
Ch4-2: find if 2 given vertices are connected in a directed map,BFS + JUST fix the DFS recursion
In this problem, there are 2 ways: BFS or DFS.Hawstein used BFS:#include #include #include #include using namespace std;const int maxn = 100;bool g[maxn][maxn], visited[maxn];int n;原创 2014-01-20 14:49:56 · 819 阅读 · 0 评论 -
Ch3-3: popat(int idx) of setofstack
Continue to use class to design solution, also, start to use STL, such as container (vector, stack, set, ...). save time and to be more professional.The design without popat method is similar to the原创 2014-01-15 07:00:24 · 856 阅读 · 0 评论 -
Ch3-4: solve Hanoi in C++ with recursion, and with stack explicitly
Hanoi has these conditions:3 rods, N disks, initially N disks are ascendingly from top to down in 1st rod. Objective: move all N disks to the 3rd rod.It must follow these 3 laws:1. every t原创 2014-01-15 08:41:59 · 616 阅读 · 0 评论 -
Ch4.3: transform an ordered array into a balanced Tree
In this problem, it is like the classical recursion example: draw the ruler as the sign of algorithms 4th textbook.In order to draw a balanced tree from an array, we need to keep left subtree.height原创 2014-01-22 08:45:52 · 750 阅读 · 0 评论 -
Ch4.5: find the (in-order tranversely) successor of a node in BST.
There are 3 methods:1. simply in order tranverse and get the ascending array, every next element is the successor of previous element.2. It is Hawstein's method: There are 2 conditions: 1st: hav原创 2014-01-23 11:14:30 · 780 阅读 · 0 评论 -
复习recursion
1. 如果这样private void lop(int x, int size, double[] jerry){ if(x==size-1){ jerry[x] = -1; return; } if(jerry[x+1]==0){ jerry[x+1] = jerry[x]- 1; //if so, lop(x+1, size, jerry); }原创 2014-05-11 15:07:37 · 895 阅读 · 0 评论