自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(29)
  • 收藏
  • 关注

转载 LeetCode : Surrounded Regions

转自出处Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that surrounded region .For example,X X

2013-04-30 10:32:51 701

原创 数组分割,把数组分割成和相等的两部分--递归方法

// 数组分割问题2.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include using namespace std;int getSum(int A[], int len){ int sum = 0; for (int i = 0; i<len; i++

2013-04-26 14:41:51 6498 1

原创 数组分割, 把数组分解成和相等的两部分--动态规划方法

#include "stdafx.h"#include "stdlib.h"#include using namespace std;bool isSubsetSplit(int A[], int len, int sum, stack &st){ int **X = (int **)malloc((len+1)*sizeof(int *)); for (int idx =

2013-04-26 14:39:02 11024 1

原创 用priority_queue实现找出数组中前K个大的元素

// 利用priority_queue查找数组中最大的K个数.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include#include #include #include #include using namespace std;void fin

2013-04-22 23:25:22 2478

原创 找出比N小的所有素数

// 查找所有比N小的素数.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include #include using namespace std;int findPrime(int N, std::vector &vector){ if (N < 3) {

2013-04-22 15:18:15 1040

转载 微软笔试题 统计英文电子书中出现次数最多的k个单词

转自出处        在v_JULY_v的文章中找到了这个问题的解法后用C++实现了一下,发现C++的代码非常的简洁。主要用到了标准库中的hash_map,优先级队列priority_queue。        算法的思路是:从头到尾遍历文件,从文件中读取遍历到的每一个单词。把遍历到的单词放到hash_map中,并统计这个单词出现的次数。遍历hash_

2013-04-22 00:09:35 5766

转载 C++priority_queue的用法

转自出处priority_queue调用 STL里面的 make_heap(), pop_heap(), push_heap() 算法实现,也算是堆的另外一种形式。先写一个用 STL 里面堆算法实现的与真正的STL里面的 priority_queue用法相似的priority_queue, 以加深对 priority_queue 的理解?

2013-04-22 00:03:43 4355

原创 0(n)时间复杂度,找出最接近当前元素的 1)比当前元素大的左元素 2)比当前元素小的右元素

// o(n)在元素左侧最近位置找出比元素大的数.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include #include #include using namespace std;typedef struct withindex{ int val

2013-04-18 11:45:14 831

转载 找出一个重复元素

转自出处1. 问题描述取值为[1,n-1]含n个元素的整数数组至少存在一个重复数,O(n)时间内找出其中任意一个重复数。如a[]={1,2,2,4,5,4},则2和4均是重复元素。2. 解决方案【方案一】 使用大小为N位图,记录每个元素是否出现过,一旦遇到一个已经出现过的元素,则直接输出。时间复杂度是O(N), 空间复杂度为O(N)。

2013-04-17 23:22:07 616

原创 找出数组中所有重复的数

// 找出数组中重复出现的元素.cpp : Defines the entry point for the console application.//#include "stdafx.h"/*for i := 0 to n - 1 while A[A[i]] != A[i] swap(A[i], A[A[i]]) end whileend for

2013-04-17 23:17:52 1832 2

原创 把数组中的0移到数组前面,其它数字移到后面,并且保持数字间的相对顺序

// 在O(n)时间内把数组中的0移到数组前面,其他数字移到数组后面,并保持相对顺序.cpp : Defines the entry point for the console application.//#include "stdafx.h"/*Given an Array With random 0s and non 0 numbers, shift all the 0s to t

2013-04-17 14:44:27 3610

转载 一个函数产生0/1的概率为 二分之一, 如何生成一个新函数使得产生0的概率为十分之三 产生1的概率为十分之七

转自出处(1)  有一个函数fun能返回0和1两个值,返回0和1的概率都是1/2,问怎么利用这个函数得到另一个函数fun2,使fun2也只能返回0和1,且返回0的概率为0.3,返回1的概率为0.7。 分析: Nathan  16:42:59随机生成长度为4的01串0000~1111每个串出现的概率都为1/16Nathan  16:44

2013-04-16 23:06:49 3373

原创 对如输入字符串 a,b, 输出 aa ab ba bb, 按字典输出顺序

// 对如输入字符串 a,b, 输出 aa ab ba bb, 按字典输出顺序.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include #include using namespace std;void print_list(std::list::itera

2013-04-16 18:44:09 2643

原创 在int数组中找出和为10的所有子集

// 在int数组中找出和为10的所有子集.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include #include using namespace std;void print_list(std::list::iterator &itr1, std::

2013-04-16 14:21:06 1199 1

原创 N的加法组合问题

网上的解法很多,我自己昨晚也想了两个多小时,今天上班途中偶尔突发灵感,中午吃饭的时候写了一下// n的加法组合.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include #include using namespace std;#define MIN

2013-04-16 12:38:24 1619

转载 调整01的位置,使得0在基数位1在偶数位,并且只能一次扫描,没有额外空间

转自出处Modified 2 color sort problem i.e. you are given an array of integers containing only 0s and 1s.You have to place all the 0s in even position and 1s in odd position. And if suppose, no. of 0

2013-04-12 00:16:28 577

原创 查找二叉树中最长的连续白色节点的长度

// 找出二叉树中最长的连续白色节点的长度.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include using namespace std;typedef enum _color{ COLOR_WHITE = 0, COLOR_BLACK}COLOR;

2013-04-11 18:11:33 680

原创 给定BST先序遍历序列,不构造BST的情况下判断BST是否每个node都只有一个child

// 给定BST先序遍历序列_判断BST是否每个node都只有一个child.cpp : Defines the entry point for the console application.//#include "stdafx.h"bool isOnlyHaveOneChild(int A[], int len){ for (int i = 0; i < len-1; i++)

2013-04-11 00:36:02 859

原创 判断一个整型数是否是回文的形式

// isPalindrome.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include bool isPalindrome(int N){ int M = N; int remain = 0; int revertNum = 0; while (M !=

2013-04-10 23:40:03 1292

转载 计算时间复杂度公式

转自出处对于T(n) = a*T(n/b)+c*n^k;T(1) = c 这样的递归关系,有这样的结论:if (a > b^k)   T(n) = O(n^(logb(a)));logb(a)b为底a的对数if (a = b^k)   T(n) = O(n^k*logn);if (a T(n) = 25T(n/5)+n^2 = 25(25T(n/25)+

2013-04-09 23:42:47 3212

原创 in place 字符串替换--利用KMP匹配

实现字符串替换假设原字符串长度足够大, replaced 字符串 和 new 字符串的 长度没有限制算法中用到了 KMP 的匹配算法// 字符串替换2.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include #include /

2013-04-09 17:38:02 828

原创 整数数除以3不用除法

// 正数数除以3不用除法.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include int divide3(int N){ if (N >= -2 && N <= 2) return 0; if (N == 3) return 1; if (N

2013-04-09 12:43:12 1397

原创 找出数组中和为10的元素的组合

Find all Subsets that sum upto 10. example int [] arr ={1,2,3,4,5,6} Subsets are : 4,5,1 4,6 2,3,5 etc.// 找出数组中和为10的元素的组合.cpp : Defines the entry point for the console application./

2013-04-09 00:22:29 1762

原创 两数组包含问题(来自微软面试题)

You have given two arrays, sayA: 4, 1, 6, 2, 8, 9, 5, 3, 2, 9, 8, 4, 6B: 6, 1, 2, 9, 8where B contains elements which are in A in consecutive locations but may be in any order.Find

2013-04-08 23:24:52 709

转载 给定一个入栈序列,求所有可能的出栈序列

转自出处网上有很多解法,但个人感觉不够清晰。下面本人献丑来写下自己的解法。力求简明易懂。首先这是个卡特兰数,学过组合数学的同学都知道。没学过的可以看下下面这个例子。有2n个人排成一队进入剧场。入场费5元。其中只有n个人有一张5元钞票,另外n人只有10元钞票,剧院无其它钞票可找零,问有多少中方法使得只要有10元的人买票,售票处就有5元的钞票找零?(将持5元者到达视作将5元

2013-04-08 17:14:27 1310

原创 一个数组,除了三个数唯一出现外,其它数均成对出现,求这三个数

// 有三个数出现一次,其他均出现两次,找出这三个数.cpp : Defines the entry point for the console application.//#include "stdafx.h"#define BIT_SIZE 32#define isON(n, i) ((n)&1<<(i))// 0x0111// 0x1011// 0x1100// 0x0

2013-04-07 18:10:59 1391

原创 链表排序

// merger_link2.cpp : Defines the entry point for the console application.//#include "stdafx.h"// Link 排序.cpp : Defines the entry point for the console application.//#include "stdafx.h"type

2013-04-04 22:46:20 559

原创 把数组中负数放在前面,0放中间,正数放后面,并保持相对顺序

void reSortArray(int Array[], int len){ int NextNegPos = 0; for (int idx = 0; idx < len; idx++) { if (Array[idx] < 0) { int tmp = Array[idx]; for (int j = idx-1; j >= NextNegPos; j--)

2013-04-03 23:38:38 4563

转载 Given the string of parentheses only, write the function to check if they are balanced. ((())) is ba

function isBalanced(str){ var stack = new Stack(); for(var c=0;c<str.length;c++){ var chr = str[c]; if(isOpenBracket(chr)){ stack.push(chr); } else if(isCloseBracket(chr)){

2013-04-03 17:31:05 804

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除