![](https://img-blog.csdnimg.cn/20201014180756757.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法
文章平均质量分 73
martin_liang
这个作者很懒,什么都没留下…
展开
-
Leetcode 213. 打家劫舍 II
你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的。同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额。class Solution {public: ...原创 2020-03-29 21:46:19 · 271 阅读 · 0 评论 -
调整数组 使得数组中负数排在前面正数排在后面,并且正数和负数的相对位置不变 (递归思路)
假设一整型数组存在若干正数和负数,现在通过某种算法使得该数组的所有负数在正数的左边,且保证负数和正数间元素相对位置不变。时空复杂度要求分别为:o(n)和o(1)解题思路:假设X代表正数,Y代表负数,下标代表正负数的序列位置,数组大小为arraySize设有一数组 [X1, Y1, Y2, X2, Y3, Y4, X3, Y5, X4, X5] (可以理解为 [1, -1, -原创 2012-05-28 07:56:23 · 5760 阅读 · 3 评论 -
求两个有序数组的中位数
转自 success041000求两个有序数组的中位数 如果有两个有序的数组,都是已经排好序的。那么求它们的中位数应该怎样求呢。如果采用对这两个数组进行排序的方法,最快的时间复杂度也要o(nlogn)的时间。但是,如果采用中位数和顺序统学的方法来寻找,则可以在o(n)的时间内解决这个问题。 我们先寻找每个数组的中位数,因为是排好顺序的数组,因转载 2012-05-28 18:53:50 · 2196 阅读 · 0 评论 -
最长单调递减序列
// 最长单调递减子序列.cpp : Defines the entry point for the console application.//#include "stdafx.h"int error = 0;/* * 函数功能 : 打印最长递减子序列 * 函数参数 : pArray指向源数组,pB指向辅助数组,k表示最长子序列的末尾元素(值最大的元素下标) *原创 2012-06-04 23:57:35 · 814 阅读 · 0 评论 -
字符串全排列
// 字符串的全排列.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "string.h"/** 解题思路:* 假设字符串 abcd* 思路就是 把每个字符都交换一下* * 最外层循环* 1) abcd, 2) ba原创 2012-06-04 23:03:41 · 418 阅读 · 0 评论 -
找零问题_动态规划
// 找零问题_动态规划.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include #include using namespace std ; void MinCoin(int NumOfValue , int Money , int *CoinValue原创 2012-06-04 23:38:50 · 1403 阅读 · 0 评论 -
八皇后问题
// 8皇后问题_回溯算法.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include using namespace std; /* * S[i]表示第i个皇后* S[i] = j 表示第 i 个皇后所在的列为 j* 能否用递归算法?*/原创 2012-06-05 00:23:37 · 411 阅读 · 0 评论 -
求数组数对之差的最大值
// 数对之差的最小值.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "string.h"#define max(a, b) (a > b? a : b)#define min(a, b) (a < b? a : b)int findMaxDiff(原创 2012-06-05 09:54:18 · 621 阅读 · 0 评论 -
编写代码实现删除一个字符串中连续出现超过一次的空格
// delete the blank.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include char MyArray[] = " I XX abcd, abcd is just a bold shit";char *myMemCpy原创 2012-07-04 00:57:34 · 962 阅读 · 0 评论 -
在两个有序的数组中找第N个数,O(lgm+lgn)级
转自出处问题描述:Give a divide and conquer algorithm for the following problem:you are given two sorted lists of size m and n, and are allowed unit time access to the ith element of each lis转载 2012-07-11 00:28:36 · 2329 阅读 · 0 评论 -
返回数组中第二大的数
int findSecondLarge(int *a, int N){ int max1=a[0]; int max2=0; for (int i=1; i < N; i++) { if (a[i] > max1) { max2=max1; max1=a[i]; }原创 2012-07-12 23:59:07 · 1870 阅读 · 0 评论 -
查找BST中第 Kth 大的元素
其实整个算法用到的是中序遍历的思想,只不过是先遍历右子树,然后遍历根节点,再遍历右子树,每次遍历一个节点的时候thK减1,当thK为0时,就是需要找的节点Node* findKthNodeImp(Node *pNode, int& thK){ if ((pNode == NULL)|| (thK <= 0)) { return NULL; }原创 2012-07-15 15:32:37 · 680 阅读 · 0 评论 -
判断一颗二叉树是否是平衡二叉树
方法一,参考 http://blog.csdn.net/forestlight/article/details/6575507templatetypename T> int DepthTree(BSTreeNode *pbs) { if (pbs==NULL) return 0; else {原创 2012-07-25 01:10:40 · 1618 阅读 · 1 评论 -
根据一个已排序数组构造一棵二叉树,要求树的高度最少
Node* creatBTree(int *pArray, int startPos, int endPos){ if (endPos < startPos) { return NULL; } if (startPos == endPos) { Node* pNode = new Node(pArray[startP原创 2012-07-26 00:36:11 · 1257 阅读 · 0 评论 -
求中序遍历的下一个节点
转自出处Node *findInOrderSuccessor(Node *root, Node *target){ Stack s; Node *cur = root; Node *prev = NULL; while(true){ while(cur) { s.push(cur);原创 2012-07-12 23:19:28 · 1011 阅读 · 0 评论 -
找出二叉树中序遍历的所求节点的下一个节点
typedef struct node{ int data; struct node* left; struct node* right; struct node* parent;}NODE;NODE *findSuccessorInorder(NODE* node){ NODE *pNode = node; if (pNode->ri原创 2012-09-07 21:32:38 · 2533 阅读 · 0 评论 -
删除字符串中重复出现的字符
char * deleteDuplicatedChar(char *string){ if (strlen(string) <= 1) return string; char *pCurPos = &string[1]; char *pLastPos = &string[1]; while (*pCurPos != '\0') {原创 2012-09-08 11:37:36 · 938 阅读 · 0 评论 -
判断一棵树是否BST
bool checkBST(NODE *Head){ // 本题的解题思想主要用到后续遍历 pNode = Head; if (pNode == NULL) return true; if ((checkBST(pNode->left)) && (checkBST(pNode->right))) { bool leftO原创 2012-09-07 21:58:13 · 1139 阅读 · 0 评论 -
删除字符串中连续的两个0
char* removeSuccessTwoZero(char *Array){ char *pCur; char *pCopy; int iZeroNum = 0; pCur = pCopy = Array; while(*pCur != '\0') { if (*pCur == '0') {原创 2012-09-26 00:12:20 · 1372 阅读 · 0 评论 -
对一个栈的元素进行从小到大的排序
stack sortStack(stack &s){ stack nS; while (!s.isEmpty()) { int tmp = s.pop(); while (!nS.isEmpty() && nS.top < tmp) { s.push(nS.pop()); }原创 2012-07-25 00:33:13 · 1481 阅读 · 0 评论 -
遍历n个元素, 等概率随机取出其中之一元素
转自出处问题描述1.一个文件中含有n个元素,只能遍历一遍,要求等概率随机取出其中之一。 先讲一个例子,5个人抽5个签,只有一个签意味着“中签”,轮流抽签,那么这种情况,估计这5个人都不会有异议,都觉得这种方法是公平的,这确实也是公平的,“抓阄”的方法已经有很长的历史了,要是不公平的话老祖先们就不干了。 或许有人觉得先抓的人中签的概率会大一些,转载 2012-07-04 00:25:57 · 1703 阅读 · 0 评论 -
交换一个无符号整形数的奇偶位
void OddEventBitSwap1(unsigned &A){ unsigned int mask = 0x3; int loop = sizeof(unsigned)*8/2; for(int idx = 0; idx < loop; idx++) { if (((A & mask) != mask) && ((A & mask) !=原创 2012-09-24 21:14:32 · 638 阅读 · 0 评论 -
按层输出二叉树
void printLevel(NODE *pHead){ std::list NodeList; //这里不能用vector NODE *pCur = pHead; if (!pHead) return; NodeList.push_back(pHead); std::list::iterator itr = NodeList.b原创 2012-09-25 23:29:31 · 2124 阅读 · 0 评论 -
求下一个比当前数大,且拥有相同比特数的数
转自出处假设数组为X=156X=10011100所求的数为 10100011解题步骤1) 先求比当前数大,并且拥有相同比特数的数 X'10011100->10101100 : 10101100比100111002) 对X'进行修正,因为X'并不是满足"下一个"的条件10101100->10100011 X''3) X''为所转载 2012-10-28 23:12:27 · 697 阅读 · 0 评论 -
把一个BST变为Double Link
NODE *bst_to_dll(NODE *pNode, NODE **ppHead){ static NODE *prev; //通过static来保存prev是这个算法的关键 if (pNode) { bst_to_dll(pNode->left, ppHead); if(prev == NULL) {原创 2012-10-31 00:13:26 · 451 阅读 · 0 评论 -
用一个随机生成N的函数构造随机生成M的函数
1. 已知有个rand7()的函数,返回1到7随机自然数,让利用这个rand7()构造rand10() 随机1~10分析:要保证rand10()在整数1-10的均匀分布,可以构造一个1-10*n的均匀分布的随机整数区间(n为任何正整数)。假设x是这个1-10*n区间上的一个随机整数,那么x%10+1就是均匀分布在1-10区间上的整数。由于(rand7()-1)*7+rand7()可以构转载 2012-11-04 23:22:11 · 1093 阅读 · 0 评论 -
打印出指定数目的{}的所有组合
转自出处// 打印所有{}的组合.cpp : Defines the entry point for the console application.//#include "stdafx.h"# include# define MAX_SIZE 100void _printParenthesis(int pos, int n, int open, int clo转载 2012-10-28 23:01:49 · 437 阅读 · 0 评论 -
用bit map进行数组排序
//定义每个Byte中有8个Bit位#include <memory.h>#define BYTESIZE 8void SetBit(char *p, int posi){ for(int i=0; i < (posi/BYTESIZE); i++) { p++; } *p = *p|(0x01<<(posi%BYTESIZE));//将该Bit位赋值1转载 2012-11-10 23:39:31 · 650 阅读 · 0 评论 -
百度2013年校招笔试题——pszStringRotate
转自出处百度2013校园招聘笔试题 算法与程序设计 第4题: 字符串左移,void *pszStringRotate(char *pszString, int nCharsRotate),比如ABCDEFG,移3位变DEFGABC,要求空间复杂度O(1),时间复杂度O(n)。 字符串位移,再简单不过了,方法也很多. 有朴素算法(逐位移动,移动n转载 2012-11-13 00:29:10 · 745 阅读 · 0 评论 -
百度2013校园招聘笔试题[软件研发]及答案
转自出处 一、简答题1.简述数据库以及线程死锁产生的原理及必要条件,简述如何避免死锁。1)互斥条件:指进程对所分配到的资源进行排它性使用,即在一段时间内某资源只由一个进程占用。如果此时还有其它进程请求资源,则请求者只能等待,直至占有资源的进程用毕释放。2)请求和保持条件:指进程已经保持至少一个资源,但又提出了新的资源请求,而该资源已被其它进程占有,此时请求进程阻塞,但又转载 2012-11-13 00:41:21 · 707 阅读 · 0 评论 -
求字符串的所有子集
// 字符串的组合输出.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "string.h"#include #include #include using namespace std; /** 输出 * 1* 12* 123* 1234原创 2012-06-05 00:13:41 · 1394 阅读 · 0 评论 -
大数相乘
// 大数相乘.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include #include using namespace std;// 12345//* 45//------std::string Multiple(std::string &STR1,原创 2012-11-14 22:20:33 · 370 阅读 · 0 评论 -
快速排序(非递归算法)
// 快速排序_非递归.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include using namespace std;int myArray[] = {3,0,6,1,11,7,2,12,4,10,9,5,8};void printArray(int *Arr原创 2012-11-16 23:22:39 · 787 阅读 · 0 评论 -
去除string中空格的方法
转自出处http://stackoverflow.com/questions/83439/remove-spaces-from-stdstring-in-cThe best thing to do is to use the algorithm remove_if and isspace:remove_if(str.begin(), str.end(), i转载 2012-12-05 11:59:09 · 873 阅读 · 0 评论 -
blogs
值得关注的博客地址:http://blog.csdn.net/hackbuteer1/article/category/830720http://www.cnblogs.com/hlxs/archive/2011/09/01/2161940.htmlhttp://bbs.csdn.net/topics/390222747http://blog.csdn.net/wuchuanp原创 2012-11-10 23:27:07 · 392 阅读 · 0 评论 -
找出数组中和为0的子数组
private static void subArraySumsZero() { int [] seed = new int[] {1,2,3,4,-9,6,7,-8,1,9}; int currSum = 0; //key: is the currSum value: the index HashMap sumMap = new HashMap();原创 2013-01-08 23:40:04 · 4010 阅读 · 0 评论 -
按层次打印二叉树
void printLevelOrder(BinaryTree *root) { if (!root) return; queue nodesQueue; int nodesInCurrentLevel = 1; int nodesInNextLevel = 0; nodesQueue.push(root); while (!nodesQueue.empty()) {原创 2013-01-09 00:29:26 · 1052 阅读 · 0 评论 -
数组排列,使得数组的值最大
#include #include #include #include using namespace std;void SortMax(int* arr, int len);bool StringCompare(string s1, string s2);string toString(int val);int main(){ int arr[5] =原创 2013-01-13 23:22:56 · 692 阅读 · 0 评论 -
判断一个字符串是否IPV4的地址
转自出处C++的做法/* return 1 if string contain only digits, else return 0 */int valid_digit(char *ip_str){ while (*ip_str) { if (*ip_str >= '0' && *ip_str <= '9') ++ip_str;转载 2013-01-13 23:29:01 · 6293 阅读 · 1 评论 -
二维数组行列递增排列,找出第K小的数
这道题之前我想了很久,但是由于思路不对,一直没有很好解决,今晚看到了一个方案,顿时茅塞顿开先贴上代码int min = a[0][0];int minOfRows[rows];for(i = 0; i < rows; i++) minOfRows[i] = 0;minOfRows[0] = 1; for(i = 1; i < k; i++){ m原创 2013-01-19 23:37:52 · 5166 阅读 · 0 评论