
CC150
文章平均质量分 57
TodorovChen
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
8.6-填充颜色
就像画图工具里面的颜料桶,在指定范围内全部变为新的颜色,转载 2014-09-02 09:45:42 · 445 阅读 · 0 评论 -
8.8-N皇后
#include using namespace std;int n=8;int tot=0;int col[8];void dfs(int cur){ if(cur==n) tot++; else for(int i=0; i<n; i++) { int ok=1; col[cur]=i原创 2014-09-08 09:21:13 · 688 阅读 · 0 评论 -
9.1-数组合并
You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B.Write a method to merge B into A in sorted order.原创 2014-09-08 10:35:29 · 466 阅读 · 0 评论 -
5.2-浮点数转二进制
#include #include #include using namespace std;string print_binary(string str_decimal){ int pos = str_decimal.find('.', 0); int intpart = atoi(str_decimal.substr(0, pos).c_str()); do原创 2014-08-09 14:47:52 · 675 阅读 · 0 评论 -
5.5-两个二进制间转换的步数
#include using namespace std;int bitConvertNum(int a, int b){ int count=0; for(int c=a^b;c!=0;c=c>>1) { if(c&1==1) count++; } return count;}int main(){ c原创 2014-08-09 15:55:24 · 610 阅读 · 0 评论 -
5.3-比x大的数中最小和比x小的数中最大
转自http://hawstein.com/posts/5.3.html题目原文:Given an integer, print the next smallest and next largest number that have the same number of 1 bits in their binary representation.译文:给定一转载 2014-08-09 15:19:15 · 1332 阅读 · 0 评论 -
5.7-找出数组中丢失的那个
转自http://hawstein.com/posts/5.7.html#include #include using namespace std;int fetch(int a[], int i, int j){ return (a[i] >> j) & 1; //return 0/1}int get(int a[], int i){ int ret =转载 2014-08-09 16:26:37 · 505 阅读 · 0 评论 -
5.6-奇偶位互相交换
#include using namespace std;int swap_bits(int x){ //a=1010 5=0101 return ( ((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1) );}int main(){ int x=6; cout 1001 return 0;原创 2014-08-09 16:11:12 · 651 阅读 · 0 评论 -
1.8-s2是否是s1的rotation(调用一次isSubstring)
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原创 2014-08-02 11:06:32 · 615 阅读 · 0 评论 -
4.8-找出二叉树和为sum的路径(始末点不一定是跟和叶)
You are given a binary tree in which each node contains a value. Design an algorithm to print all paths which sum up to that value. Note that it can be any path in the tree - it does not have to start原创 2014-08-07 20:26:31 · 847 阅读 · 0 评论 -
1.7-设置矩阵0元素的所在行和列全部为0
Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.原创 2014-08-01 17:07:17 · 1608 阅读 · 0 评论 -
8.7-硬币组合
#include using namespace std;int cent[]={25,10,5,1};int n=21;int tot=0;void dfs_order(int cur){ if(cur>n) return; if(cur==n) { tot++; } else for(int i=0;i<4;i原创 2014-09-08 09:58:44 · 685 阅读 · 0 评论 -
9.2-anagrams排序
首先,要弄清楚什么是变位词。变位词就是组成的字母相同,但顺序不一样的单词。 比如说:live和evil就是一对变位词。OK,那么这道题目的意思就很清楚了, 它并不要求我们将字符串数组中的字符串按字典序排序,否则我们直接调用STL中的sort 函数就可以了。它要求我们在排序的过程中,按照变位词的准则来排序。 这种情况下,我们还是可以调用sort函数,不过要自己写一个对比函数。 一般情况下我们如果要排转载 2014-09-08 10:56:46 · 713 阅读 · 0 评论 -
cc150 50道高频总结
多人都说把这150题做了一遍或几遍,但是我感觉算法题才是重点,其他的很多题面试基本碰不上,没看出来有必要全做。这里总结一下自己认为重要的题。第一章 :全部重要 (1.6, 1.7 Leetcode上有)。1.5 面A碰到 (string compression)1.7面Z碰到 (set 0)1.8面Bigfish碰到 (string rotation)第二章 (转载 2014-09-24 11:24:35 · 1009 阅读 · 0 评论 -
8.5-有效的括号组合(same in LeetCode)
看自己写的就行:http://blog.youkuaiyun.com/todorovchen/article/details/24419571原创 2014-08-14 21:00:36 · 605 阅读 · 0 评论 -
8.4-字符串全排列
Write a method to compute all permutations of a string#include #include #include using namespace std;#define MAXN 100int N;bool visit[MAXN];vectorans;void dfs_permute(int i, string l原创 2014-08-14 17:22:53 · 465 阅读 · 0 评论 -
9.5-字符串有序数组的二分查找
Given a sorted array of strings which is interspersed with empty strings, write a method to find the location of a given string.Example: find “ball” in [“at”, “”, “”, “”, “ball”, “”, “”, “car”, “”,原创 2014-09-10 23:00:05 · 911 阅读 · 0 评论 -
8.3-求子集
Write a method that returns all subsets of a set.原创 2014-08-14 14:47:17 · 557 阅读 · 0 评论 -
9.6-二维数组搜索
其实很简单,关键在于搜索的qi#include using namespace std;int* search_matrix(int matrix[][4], int n, int m, int x){ int i=0,j=m-1; int *ans = new int[2]; while(i=0) { if(matrix[i][原创 2014-09-11 04:24:08 · 628 阅读 · 0 评论 -
8.2-走格子
Imagine a robot sitting on the upper left hand corner of an NxN grid. The robot can only move in two directions: right and down. How many possible paths are there for the robot?FOLLOW UPImagine ce原创 2014-08-13 10:37:28 · 532 阅读 · 0 评论 -
9.4-外部排序+k路归并
转自http://hawstein.com/posts/9.4.html当面试官说到2GB文件的时候,他其实就是在暗示你, 他并不希望一次性把所有的数据都载入内存。这样子的话,我们要怎么做呢? 我们每次就将部分数据载入内存就好了。算法:首先我们要了解,可以用的内存有多大?假设我们有X MB的内存可用。我们将文件分为K份,其中X*K=2GB。每次取其中一份载入到内存中,转载 2014-09-10 11:39:34 · 1738 阅读 · 0 评论 -
9.3-两段升序列搜索
#include using namespace std;/*Input: find 5 in array (15 16 19 20 25 1 3 4 5 7 10 14)Output: 8 (the index of 5 in the array)*/int search(int A[], int n, int target){ int low=0, high=n-1;原创 2014-09-08 11:42:10 · 546 阅读 · 0 评论 -
8.1-Fibonacci number
Write a method to generate the nth Fibonacci number.#include using namespace std;//0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144int fibonacci(int n){ if(n==0) return 0; else if原创 2014-08-11 21:37:04 · 457 阅读 · 0 评论 -
5.1-位的子串
You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e.g., M becomes a substring of N located at i and starting at原创 2014-08-08 22:42:49 · 520 阅读 · 0 评论 -
3.5-两个栈模拟一个队列
#include #include using namespace std;class MyQueue{public: stack *stk1; stack *stk2; int push(int value); void pop(); int front();//first into the queue int back();原创 2014-08-05 21:02:13 · 366 阅读 · 0 评论 -
1.6-Rotate Image(same in LeetCode)
顺时针旋转90度N*N矩阵。leetc原创 2014-08-01 16:32:00 · 425 阅读 · 0 评论 -
3.6-栈内部排序
Write a program to sort a stack in ascending order. You should not make any assumptions about how the stack is implemented. The following are the only functions that should be used to write this progr原创 2014-08-05 21:44:37 · 506 阅读 · 0 评论 -
3.2-添加栈的min()功能函数
How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element? Push, pop and min should all operate in O(1) time.#include #include原创 2014-08-04 14:10:27 · 588 阅读 · 0 评论 -
3.3-多个栈组合模拟一个栈
如果不用实现popAt#include #include #include using namespace std;class setofstacks{public: vector > stk_vector; int max_capacity; void push(int value); void pop(); void popAt(int原创 2014-08-04 20:53:27 · 661 阅读 · 0 评论 -
2.3-删除链表中间节点
一开始就是renw#include using namespace std;struct ListNode{ int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};void show(ListNode *head){ while(head) {原创 2014-08-02 15:52:39 · 360 阅读 · 0 评论 -
3.1-一个数组模拟三个栈
Describe how you could use a single array to implement three stacks.class array_stack{private: int *buf; int ptop[3]; int size;public: array_stack(int size = 300) {原创 2014-08-02 22:09:49 · 592 阅读 · 0 评论 -
2.5-找链表环的起点(same in LeetCode)
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 points t原创 2014-08-02 16:41:57 · 444 阅读 · 0 评论 -
2.1-链表去重
Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this problem if a temporary buffer is not allowed?原创 2014-08-02 14:55:07 · 655 阅读 · 0 评论 -
2.2-找链表倒数第k个(same in LeetCode)
Implement an algorithm to find the nth to last element of a singly linked list.快慢zhiz#include using namespace std;struct ListNode{ int val; ListNode *next; ListNode(int x) : val(x原创 2014-08-02 15:11:38 · 800 阅读 · 0 评论 -
1.3-字符串去重in-place
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原创 2014-08-01 14:33:48 · 464 阅读 · 0 评论 -
1.5-替换字符串中的空格为%20
Write a method to replace all spaces in a string with ‘%20’.先计算空格数量,然后malloc新的zchar* replaceSpaces(char* str){ char *p = str; int len=0,spaceNum=0; while(*p) { len++;原创 2014-08-01 15:57:05 · 502 阅读 · 0 评论 -
4.1-判断二叉树是否平衡(same in LeetCode)
Implement a function to check if a tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that no two leaf nodes differ in distance from the root by more tha原创 2014-08-06 16:05:41 · 562 阅读 · 0 评论 -
4.5-求BST的中序后继节点
Write an algorithm to find the ‘next’ node (e.g., in-order successor) of a given node in a binary search tree where each node has a link to its parent.#include #include using namespace std;原创 2014-08-07 10:26:24 · 695 阅读 · 0 评论 -
4.3-有序数组转二叉搜索树
#include #include using namespace std;struct TreeNode{ int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {}};TreeNode *createTree(int *n原创 2014-08-07 16:20:36 · 505 阅读 · 0 评论 -
4.6-二叉树的2个节点的第一个公共祖先节点
Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure. NOTE: This is not necessarily a binary search tr原创 2014-08-07 15:39:43 · 543 阅读 · 0 评论