- 博客(253)
- 收藏
- 关注
原创 Socket UDP【简单聊天实例】
#include #include #include #include #include #include #include #include #include using namespace std;#define MAXDATASIZE 1024#define IP "127.0.0.1"int send_head_port;int send_tail_port;
2014-10-05 09:31:47
684
原创 c++解析json文件: Rapidjson
#include "rapidjson/document.h"#include "rapidjson/prettywriter.h"#include "rapidjson/filestream.h" #include #include #include using namespace rapidjson;using namespace std;int main(){
2014-10-05 00:30:16
4818
转载 Socket TCP【简单聊天实例】
Server端:#include #include #include #include #include #include #include #include #include #include #include #include #define MYPORT 3490 //定义端口 #define BACKLOG
2014-10-02 02:08:01
704
转载 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
992
转载 8连块
题目:输入一个n*n的黑白图像(1表示黑色,0表示白色),任务是统计其中八连块的个数。如果两个黑格子有公共边或者公共顶点,就说它们属于同一个八连块。输入:第一行输入一个整数n(n输出:输出有多少个八连块,以及每个块的面积。一个方格的面积为1。分析:把图片最外层扩展开来,在最外面加一层白色的框框。。设置一个数组来存储每个八连块的面积。。DFS递归调用每一个黑色的方
2014-09-24 01:31:03
868
原创 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
619
原创 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
893
转载 9.4-外部排序+k路归并
转自http://hawstein.com/posts/9.4.html当面试官说到2GB文件的时候,他其实就是在暗示你, 他并不希望一次性把所有的数据都载入内存。这样子的话,我们要怎么做呢? 我们每次就将部分数据载入内存就好了。算法:首先我们要了解,可以用的内存有多大?假设我们有X MB的内存可用。我们将文件分为K份,其中X*K=2GB。每次取其中一份载入到内存中,
2014-09-10 11:39:34
1723
原创 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
534
转载 9.2-anagrams排序
首先,要弄清楚什么是变位词。变位词就是组成的字母相同,但顺序不一样的单词。 比如说:live和evil就是一对变位词。OK,那么这道题目的意思就很清楚了, 它并不要求我们将字符串数组中的字符串按字典序排序,否则我们直接调用STL中的sort 函数就可以了。它要求我们在排序的过程中,按照变位词的准则来排序。 这种情况下,我们还是可以调用sort函数,不过要自己写一个对比函数。 一般情况下我们如果要排
2014-09-08 10:56:46
696
原创 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
458
原创 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
676
原创 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
674
原创 8.5-有效的括号组合(same in LeetCode)
看自己写的就行:http://blog.youkuaiyun.com/todorovchen/article/details/24419571
2014-08-14 21:00:36
590
原创 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
451
原创 【leetcode】Subsets
#include #include #include using namespace std;vector > ans;int len;void dfs(int dep, vector line, vector num){ ans.push_back(line); if(dep == len) { return;
2014-08-13 21:11:08
464
原创 *【Wikioi】1983-等式问题
#include //1 2 3 4 5 6 7 8 9=Nusing namespace std;int ans = 0,N;void dfs(int dep , int sum){ int tmp = 0; if(sum == N && dep>9) { ans++; } else for(int i = dep ; i <=
2014-08-13 17:44:44
993
原创 【wikioi】2956-排队问题
有N个学生去食堂,可教官规定:必须2人或3人组成一组,求有多少种不同分组的方法。方法1:dp(和pa),公式#include using namespace std;long long ans=0;long long temp_sum=0;long long dp(int n){ long long f[151]= {0}; f[2]=f[3]=1;
2014-08-13 16:41:19
760
原创 【Wikioi】1116四色问题
#include #include #define MAXN 8int N;int g[MAXN][MAXN];int history[MAXN];int count;void dfs2(int i){ if (i == N) { count++; } else for (int color = 1; color < 5; colo
2014-08-13 11:22:28
850
原创 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
523
原创 素数环
这#include #include using namespace std;//6//143256//165234bool isPrime(int x){ if(x==1) return false; for(int i=2; i<=sqrt(x); i++) { if(x%i==0) retur
2014-08-12 16:57:22
581
原创 【CC150-8.2】走格子路径
DFS其实是很灵活的,虽然我习惯按照void d#include #include using namespace std;typedef struct point{ int x, y;} point;const int MAXN = 20;int g[MAXN][MAXN];point vp[MAXN+MAXN];vector pvec;void pr
2014-08-12 16:52:09
640
原创 【Wikioi】1294全排列
做了zhedao#include #include using namespace std;#define MAXN 10int N;int history[MAXN];bool visit[MAXN];vector >ans;vectorline;void dfs(int i){ int j, k; if (i == N) { f
2014-08-12 16:12:01
502
原创 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
447
转载 一致性hash算法 - consistent hashing
一致性 hash 算法( consistent hashing )张亮consistent hashing 算法早在 1997 年就在论文 Consistent hashing and random trees 中被提出,目前在cache 系统中应用越来越广泛;1 基本场景比如你有 N 个 cache 服务器(后面简称 cache ),那么如何将一个对象 object 映射到 N
2014-08-10 15:34:14
437
转载 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
488
原创 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
627
原创 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
593
转载 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
1296
原创 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
656
原创 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
501
原创 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
829
转载 Zz: CS硕士菜鸟美帝求职经验
以下内容针对在美国读硕士,准备以软件工程师作为求职目标的学生。简历简历是面试的敲门砖,一张简历最重要有两部分:毕业院校和实习经历。毕业院校当然是CMU, Stanford, MIT, Harvard这几家为最好,本科比硕士好。实习经历有比没有好,Facebook & Google又比绝大多数其他公司好一些。准备简历是一个长期的过程,找实习的时候好好找,如果没有找到好的实习,建议自己花时间做
2014-08-07 17:23:48
1501
转载 算法题库1
1. Google: Write a code to merge N sorted array. 2. Microsoft: Reverse linked list by block eg. block size is 3 and linked list is 1 2 3 4 5 6 7 8 output is 3 2 1 6 5 4 8 7
2014-08-07 17:19:43
888
转载 面经1
过去的一年多里,参加了一些面试,虽然面过的公司不多,但都从头一直走到尾。毕竟自己也是花了大量的时间和精力在这一场场的面试里。所以,就絮叨下自己的一些经验,希望能给在美国找实习找工作的同学们提供一点点帮助。 开始前的一些说明:1. 笔者只是一介小本科,虽然留了学,但是留了级,学识浅薄,目光短浅,文章若有不恰之处,恳请各位大牛不吝指正!2. 笔者面试的岗位均为Softw
2014-08-07 17:11:52
616
原创 4.7-判断是否为子树
#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:39:50
511
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人