算法
文章平均质量分 75
believexfr
这个作者很懒,什么都没留下…
展开
-
判断出栈顺序是否正确
给定压栈顺序和出栈顺序,判断出栈顺序是否正确原创 2016-04-23 15:35:13 · 2461 阅读 · 0 评论 -
leetcode--594--Longest Harmonious Subsequence
题目来源:https://leetcode.com/problems/longest-harmonious-subsequence/description/转载请注明出处:题目:We define a harmonious array is an array where the difference between its maximum v原创 2017-09-17 17:07:58 · 201 阅读 · 0 评论 -
leetcode--136--Single Number
题目来源:https://leetcode.com/problems/single-number/discuss/转载请注明出处:题目:Given an array of integers, every element appears twice except for one. Find that single one.Note:原创 2017-09-17 17:56:15 · 323 阅读 · 0 评论 -
map 按key和value排序
C++ map stl转载 2017-09-10 11:16:13 · 320 阅读 · 0 评论 -
c++ template使用
C++ template转载 2017-09-10 12:00:38 · 199 阅读 · 0 评论 -
红黑树介绍
红黑树转载 2017-09-10 17:34:47 · 243 阅读 · 0 评论 -
红黑树操作
红黑树转载 2017-09-10 17:38:00 · 449 阅读 · 1 评论 -
c++ 双向链表操作
双向链表转载 2017-09-03 13:00:54 · 445 阅读 · 0 评论 -
leetcode371-----Sum of Two Integers
问题:Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.Example:Given a = 1 and b = 2, return 3.解答:int getSum(int a, int b) { if(b == 0原创 2016-09-14 15:05:47 · 284 阅读 · 0 评论 -
数组如何模拟链表
模拟链表:和链表相似,只不过将链表的next域用一个数组表示。数组data用来存放具体数字,而另一个数组right用来存放当前序列中每一个元素右边的元素在数组data中的位置的。例如: right[1]的值为2,表示当前序列中元素右边的元素存放在data[2]中;如果是0,例如right[9]的值为0,表示当前序列中9号元素的右边没有元素。(数组下标从1开始)例题:向已排序后转载 2017-10-22 13:34:02 · 475 阅读 · 0 评论 -
利用深度优先搜索算法和广度优先搜索算法解迷宫问题
本来应该是在大一就该掌握的内容,我却迟迟没有去了解,最近比较闲,今天就好好讨论一下图搜索算法的两个分支,深度优先算法与广度优先算法。本篇博客将分别利用深度优先算法和广度优先算法解决迷宫问题。 所谓迷宫问题,这里我们用一个二维数组map[5][5]={ 0,1,0,0,1 0,0,0,0,0 1,0,0,0,1转载 2017-10-22 14:35:54 · 2698 阅读 · 1 评论 -
leetcode--23--Merge k Sorted Lists
原题:Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.解答:C++版本 class Solution {public: ListNode *mergeKLists(vector原创 2016-10-03 14:47:38 · 1052 阅读 · 0 评论 -
leetcode--34--Search for a Range
、原创 2016-10-05 12:07:48 · 255 阅读 · 0 评论 -
leetcode--37--Sudoku Solver
原题:Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character '.'.You may assume that there will be only one unique solutio原创 2016-10-05 15:47:39 · 254 阅读 · 0 评论 -
leetcode--1--two sum
原题:public int[] twoSum(int[] nums, int target) { MapInteger, Integer> map = new HashMap for (int i = 0; i nums.length; i++) { int complement = target - nums[i]; if (map.cont原创 2016-09-26 22:03:20 · 251 阅读 · 0 评论 -
排序算法之快排
快速排序 算法转载 2017-09-03 16:54:59 · 662 阅读 · 0 评论 -
排序算法之---堆排序
堆排序转载 2017-09-03 17:20:51 · 141 阅读 · 0 评论 -
B和B+树
B树和B+树转载 2017-09-16 14:52:30 · 286 阅读 · 0 评论 -
冒泡排序
冒泡排序转载 2017-09-16 14:46:02 · 351 阅读 · 0 评论 -
面试各种常见排序
1--归并排序 12--快速排序 23--插入排序 44--堆排序 55--计数排序 76--基数排序 8 1--归并排序/*算法类型:归并算法算法思路:分为两个函数,merge使用递归将数组分解并最终合原创 2016-09-28 14:57:04 · 476 阅读 · 0 评论 -
hash冲突解决方法
对于Hash,我们是怎样来处理冲突的。现在就来介绍一些经典的Hash冲突处理的方法。主要包括 (1)开放地址法 (2)拉链法 (3)再哈希法 (4)建立公共溢出区 (1)开放地址法 基本思想:当发生地址冲突时,按照某种方法继续探测Hash表中其它存储单元,直到找到空位置为止。描述如下转载 2016-09-29 13:38:36 · 330 阅读 · 0 评论 -
图的广度优先遍历与深度优先遍历
写在最前的三点:1、所谓图的遍历就是按照某种次序访问图的每一顶点一次仅且一次。2、实现bfs和dfs都需要解决的一个问题就是如何存储图。一般有两种方法:邻接矩阵和邻接表。这里为简单起见,均采用邻接矩阵存储,说白了也就是二维数组。3、本文章的小测试部分的测试实例是下图:一、深度优先搜索遍历1、从顶点v出发深度遍历图G的算法① 访转载 2016-09-29 15:42:18 · 678 阅读 · 0 评论 -
求最小生成树的Prim算法
//算法名称:最小生成图//算法思路:prim算法#include "stdafx.h"#include #include using namespace std; int prim(int **graph, int n); const int MAXCOST = 0x7FFFFFFF;int main() { int vertexNum,原创 2016-09-29 16:06:40 · 405 阅读 · 0 评论 -
最小生成树的prim算法
最小生成树之prim算法边赋以权值的图称为网或带权图,带权图的生成树也是带权的,生成树T各边的权值总和称为该树的权。 最小生成树(MST):权值最小的生成树。 生成树和最小生成树的应用:要连通n个城市需要n-1条边线路。可以把边上的权值解释为线路的造价。则最小生成树表示使其造价最小的生成树。 构造网的最小生成树必须解决下面两个问题: 1、尽转载 2016-09-29 16:21:44 · 459 阅读 · 0 评论 -
老鼠和毒药问题
有100只一模一样的瓶子,编号1-100。其中99瓶是水,一瓶是看起来像水的毒药。只要老鼠喝下一小口毒药,一天后则死亡。现在,你有7只老鼠和一天的时间,如何检验出哪个号码瓶子里是毒药? 这儿把它叫做‘问题1’,解决此题的方法可谓二进制应用的经典: 首先,将瓶子的10进制编号数改成7位的2进制码。然后,让第1只老鼠喝所有2进制码第1位是1的瓶子中的水;让第2只老鼠喝所有2进制码第2位转载 2016-10-15 16:35:59 · 20218 阅读 · 5 评论 -
设计模式--观察者模式
1. 概述 有时被称作发布/订阅模式,观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态发生变化时,会通知所有观察者对象,使它们能够自动更新自己。2. 解决的问题 将一个系统分割成一个一些类相互协作的类有一个不好的副作用,那就是需要维护相关对象间的一致性。我们不希望为了维持一致性而使各类紧密耦合,这样会给维护、扩展和重用都带转载 2016-09-29 22:14:00 · 184 阅读 · 0 评论 -
leetcode--3--最长不重复字符串
原题:Longest Substring Without Repeating CharactersGiven a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", which the l原创 2016-09-30 13:27:55 · 226 阅读 · 0 评论 -
二叉树最大路径和
给出一个二叉树,找到其中的最大路径和。路径可以从树中任意一个节点开始和结束。例如:给出如下二叉树, 1 / \ 2 3返回6。 初始思路为了简化分析,我们先假设二叉树中所有节点的值都是正数。通过观察可以发现,一棵二叉树的最大路径,就是其左子树的最大路径加上右子树的最大路径。看起来可以从转载 2016-10-15 22:33:56 · 1801 阅读 · 0 评论 -
leetcode--5--最长回文串
原题:Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromi原创 2016-09-30 14:51:04 · 295 阅读 · 1 评论 -
leetcode--8--实现atoi函数
原题: String to Integer (atoi) Implement atoi to convert a string to an integer.解答:C++版本int atoi(const char *str) { int sign = 1, base = 0, i = 0; while (str[i] == ' ') { i++; }if (st原创 2016-09-30 15:31:20 · 282 阅读 · 0 评论 -
leetcode--22--产生有效括号
原题:Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:[ "((()))", "((原创 2016-10-03 12:54:19 · 402 阅读 · 1 评论 -
反转链表
原题:反转链表 将一个链表反转,比如原链表为1-2-3-4,反转后为4-3-2-1,输出头指针解答:C++版本 ListNode* swapPairs(ListNode* head) { ListNode * dummy = new ListNode(0); dummy->next = head;ListNode * d_next = head;原创 2016-10-03 17:45:23 · 211 阅读 · 0 评论 -
KMP的next数组
原题: Implement strStr() Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.解答:C++版本 int strStr(string haystack, string needle) {原创 2016-10-04 16:58:26 · 273 阅读 · 0 评论 -
Comparable 和 Comparator 的区别
Comparable & Comparator 都是用来实现集合中元素的比较、排序的,只是 Comparable 是在集合内部定义的方法实现的排序,Comparator 是在集合外部实现的排序,所以,如想实现排序,就需要在集合外定义 Comparator 接口的方法或在集合内实现 Comparable 接口的方法。Comparator位于包Java.util下,而Comparable位于包转载 2016-10-06 16:48:03 · 170 阅读 · 0 评论 -
最大容积--Container With Most Water of leetcode
QUESTIONhttps://leetcode.com/problems/container-with-most-water/description/ Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines原创 2017-08-19 10:22:03 · 899 阅读 · 0 评论 -
C/C++/JAVA 双向链表实现
双向链表实现转载 2017-09-16 11:27:14 · 441 阅读 · 0 评论