随笔分类 -  算法

游戏视野系统算法 (FOV using recursive shadowcasting)
摘要:原理 http://www.roguebasin.com/index.php?title=FOV_using_recursive_shadowcasting python代码实现 http://www.roguebasin.com/index.php?title=Python_shadowcasti 阅读全文

posted @ 2017-08-19 16:07 lakeone 阅读(1001) 评论(0) 推荐(0)

三角形面积计算(海伦公式或叉积绝对值的一半)
摘要:#include <iostream> #include <cmath> using namespace std; struct Point { float x; float y; Point(float a, float b) : x(a), y(b) { } }; double Length(P 阅读全文

posted @ 2016-10-03 00:58 lakeone 阅读(1890) 评论(0) 推荐(0)

n皇后问题的递归和迭代版 leetcode N-Queens
摘要:题目如下图: 递归版 迭代版 阅读全文

posted @ 2016-09-16 11:26 lakeone 阅读(834) 评论(0) 推荐(0)

动态规划求字符串的回文子串
摘要:string s是输入字符串, palindrome[i][j]表示s.substr(i, j - i + 1)是否为回文。算法时间复杂度和空间复杂度都为O(n^2)。 阅读全文

posted @ 2016-08-11 14:14 lakeone 阅读(376) 评论(0) 推荐(0)

lower_bound和upper_bound的实现
摘要:int lowerBound(int* nums, int numsSize, int target) { //注意left和right的初始值必须是left = 0, right = numsSzie, 因为返回的位置可能是[0,numsSize] int left = 0; int right = numsSize; int mid; while (l... 阅读全文

posted @ 2016-08-10 17:24 lakeone 阅读(293) 评论(0) 推荐(0)

链表的归并排序与快速排序
摘要:链表数据结构 链表归并排序 基于值交换的链表快速排序 基于节点交换的链表快速排序 阅读全文

posted @ 2016-08-10 15:03 lakeone 阅读(255) 评论(0) 推荐(0)

如何判断一个数是2的幂
摘要:如何判断一个数是2的幂,主要是要找出2的幂次方的数的特点。我们知道,1个数乘以2就是将该数左移1位,而2的0次幂为1, 所以2的n次幂(就是2的0次幂n次乘以2)就是将1左移n位, 这样我们知道如果一个数n是2的幂,则其只有首位为1,其后若干个0,必然有n & (n - 1)为0。(在求1个数的二进... 阅读全文

posted @ 2015-12-27 01:51 lakeone 阅读(3060) 评论(0) 推荐(0)

用异或交换两个整数的陷阱
摘要:前面我们谈到了,可用通过异或运算交换两个数,而不需要任何的中间变量。 如下面:void exchange(int&a,int&b){a ^=b;b ^=a;a ^=b;}然而,这里面却存在着一个非常隐蔽的陷阱。通常我们在对数组进行操作的时候,会交换数组中的两个元素,如exchang(&a[i], &... 阅读全文

posted @ 2015-06-22 22:54 lakeone 阅读(1744) 评论(0) 推荐(0)

二分查找确定lower_bound和upper_bound
摘要:lower_bound当target存在时, 返回它出现的第一个位置,如果不存在,则返回这样一个下标i:在此处插入target后,序列仍然有序。代码如下:int lower_bound(int* nums, int numsSize, int target) { //注意left和right的... 阅读全文

posted @ 2015-06-21 17:41 lakeone 阅读(545) 评论(0) 推荐(0)

异或的性质
摘要:原文链接:http://blog.sina.com.cn/s/blog_a53544e0010146nv.html异或运算^。是个很重要的位运算。简单的说:0^0=01^0=11^1=0并且有A^0=A A^A=0并且B^A^A=B 因为B^A^A=B^(A^A)=B^0=B然后可以利用他来交换AB... 阅读全文

posted @ 2015-04-14 11:19 lakeone 阅读(4908) 评论(0) 推荐(0)

UVA 160 - Factors and Factorials
摘要:Factors and FactorialsThe factorial of a numberN(writtenN!) is defined as the product of all the integers from 1 toN. It is often defined recursively ... 阅读全文

posted @ 2014-12-07 21:18 lakeone 阅读(645) 评论(0) 推荐(0)

Uva 642 - Word Amalgamation sort qsort
摘要:Word AmalgamationIn millions of newspapers across the United States there is a word game calledJumble. The object of this game is to solve a riddle, b... 阅读全文

posted @ 2014-12-06 20:45 lakeone 阅读(447) 评论(0) 推荐(0)

埃拉托色尼筛法 素数
摘要:运行时间和下式成正比:N+N/2+N/3+N/7+N/11+……#include #include using namespace std;int main(int argc, char* argv[]){ int n; if (argc != 2) { printf("usage:bin n\n... 阅读全文

posted @ 2014-12-05 16:20 lakeone 阅读(493) 评论(0) 推荐(0)

导航