今天我很郁闷,在实验室凑合睡了一晚,准备白天大干一场,结果一整天就只做出了一道算法题.看来还是经验不足呀,同志仍需努力呀. 算法题目要求是这样的: Return the number of total permutations of the provided string that don't have repeated consecutive letters. Assume that all characters in the provided string are each unique.F…
问题描述:给定一个字符串,输出该字符串所有排列的可能.如输入“abc”,输出“abc,acb,bca,bac,cab,cba”. 虽然原理很简单,然而我还是折腾了好一会才实现这个算法……这里主要记录的是解决问题中的思路. 我实现的是最普通的递归算法,也没有除重,嗯非递归及除重的算法以后再补上吧. 实现过程 首先明确函数的输入和输出,输入是一个字符串,输出么对于JS而言用数组来表示最恰当了,所以函数的雏形应该是这样的: function permutate(str) { var result =…
Source, git Heap is a data structure that can fundamentally change the performance of fairly common algorithms in Computer Science. The heap data structure is called a heap because it satisfies the heap property. The heap property states, that if P i…
1. Problem These two algorithm are all used to find a minimum spanning tree for a weighted undirected graph. 2.Kruskal's algorithm 2.1 Pseudocode A = ∅ foreach v ∈ G.V: MAKE-SET(v) foreach (u, v) in G.E ordered by weight(u, v), increasing: if FIND-SE…
A method is presented for finding a shortest path from a starting place to a destination place in a traffic network including one or more turn restrictions, one or more U-turns and one or more P-turns using a Dijkstra algorithm. The method as sets a…
An Python implementation of heap-sort based on the detailed algorithm description in Introduction to Algorithms Third Edition import random def max_heapify(arr, i, length): while True: l, r = i * 2 + 1, i * 2 + 2 largest = l if l < length and arr[l]…
堆基础 堆(Heap)是具有这样性质的数据结构:1/完全二叉树 2/所有节点的值大于等于(或小于等于)子节点的值: 图片来源:这里 堆可以用数组存储,插入.删除会触发节点shift_down.shift_up操作,时间复杂度O(logn). 堆是优先级队列(Priority queue)的底层数据结构,较常使用优先级队列而非直接使用堆处理问题.利用堆的性质可以方便地获取极值,例如 LeetCode 题目 215. Kth Largest Element in an Array,时间复杂度O(nl…
/* * POJ_1146.cpp * * Created on: 2013年10月8日 * Author: Administrator */ #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; char s[60]; int l; bool get(){ int i = l - 1; int j; while(i…
地址 http://blog.csdn.net/chen77716/article/details/6166675 中文wiki http://zh.wikipedia.org/zh-cn/Paxos%E7%AE%97%E6%B3%95 在分布式系统设计领域,Paxos可谓是最重要一致性的算法.连Google的大牛们也在论文Chubby中说到All working protocols for asynchronous consensus we have so far encountered ha…