随笔分类 -  算法

摘要:在介绍加权轮询算法(WeightedRound-Robin)之前,首先介绍一下轮询算法(Round-Robin)。 一:轮询算法(Round-Robin) 轮询算法是最简单的一种负载均衡算法。它的原理是把来自用户的请求轮流分配给内部的服务器:从服务器1开始,直到服务器N,然后重新开始循环。 算法的优 阅读全文
posted @ 2016-07-31 09:30 gqtc 阅读(896) 评论(0) 推荐(0)
摘要:负载均衡的那些算法们by:简单的老王 上周发了问卷,想了解一下大家对老王有没有什么建议,然后好多朋友都投了票,想了解编程技术和服务器架构的干货,所以接下来会先聊聊编程和架构相关的算法,然后大概在6月下旬会跟大家聊聊面试那些事儿(老王到目前大约参加了几百次的面试,可以从面试官的角度来聊聊不一样的面试)... 阅读全文
posted @ 2016-07-31 09:20 gqtc 阅读(230) 评论(0) 推荐(0)
摘要:Redis的sentinel模式使用了Hiredis代码,Hiredis是redis数据库一个轻量级的C语言客户端库。它实现的向Redis发送命令的API函数redisCommand,使用方法类似于printf。因此只要熟悉redis命令,就可以很容易的使用该函数将redis命令字... 阅读全文
posted @ 2016-05-08 09:55 gqtc 阅读(1754) 评论(0) 推荐(0)
摘要:dict.c中的dictScan函数,用来遍历字典,迭代其中的每个元素。该函数使用的算法非常精妙!!!所以必须记录一下。 遍历一个稳定的字典,当然不是什么难事,但Redis中的字典因为有rehash的过程,使字典可能扩展,也可能缩小。这就带来了问题,如果在两次遍历中... 阅读全文
posted @ 2016-01-17 20:46 gqtc 阅读(302) 评论(0) 推荐(0)
摘要:一个无符号的整数,如果需要翻转其二进制位,可以采用下面的方法,以32位整数为例:unsigned int v; // 32-bit word to reverse bit order// swap odd and even bitsv = ((v >> 1) & 0x55555555... 阅读全文
posted @ 2016-01-17 20:00 gqtc 阅读(441) 评论(0) 推荐(1)
摘要:Libev中在管理定时器时,使用了堆这种结构,而且除了常见的最小2叉堆之外,它还实现了更高效的4叉堆。 之所以要实现4叉堆,是因为普通2叉堆的缓存效率较低,所谓缓存效率低,也就是说对CPU缓存的利用率比较低,说白了,就是违背了局部性原理。这是因为在2叉堆中,对元素的... 阅读全文
posted @ 2015-10-17 13:15 gqtc 阅读(383) 评论(0) 推荐(0)
摘要:Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are s... 阅读全文
posted @ 2015-09-16 13:06 gqtc 阅读(87) 评论(0) 推荐(0)
摘要:Given an array with n objects colored red,white or blue, sort them so that objects of the same color are adjacent, with the colors in the orde... 阅读全文
posted @ 2015-09-15 13:04 gqtc 阅读(107) 评论(0) 推荐(0)
摘要:Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow up:Did you use extra space?A strai... 阅读全文
posted @ 2015-09-14 12:46 gqtc 阅读(211) 评论(0) 推荐(0)
摘要:Implement int sqrt(int x). Compute and return the square root of x. 1:二分查找 思路:要实现一个sqrt函数,可以使用二分法,首先确定一个范围[begin, end],这个范围的中间数m... 阅读全文
posted @ 2015-09-12 10:53 gqtc 阅读(403) 评论(0) 推荐(0)
摘要:Given an array of words and a length L, format the text such that each line has exactly L characters and is fully(left and right) justified. ... 阅读全文
posted @ 2015-09-10 22:15 gqtc 阅读(134) 评论(0) 推荐(0)
摘要:Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along it... 阅读全文
posted @ 2015-09-10 21:05 gqtc 阅读(194) 评论(0) 推荐(0)
摘要:A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or... 阅读全文
posted @ 2015-08-23 21:28 gqtc 阅读(144) 评论(0) 推荐(0)
摘要:The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the fo... 阅读全文
posted @ 2015-08-23 13:16 gqtc 阅读(134) 评论(0) 推荐(0)
摘要:Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the interval... 阅读全文
posted @ 2015-08-23 10:51 gqtc 阅读(151) 评论(0) 推荐(0)
摘要:Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3], [2,6], [8,10], [15,18],return [1,6], [8,10], [... 阅读全文
posted @ 2015-08-22 17:01 gqtc 阅读(125) 评论(0) 推荐(0)
摘要:Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For example,Given n = 3,You should return ... 阅读全文
posted @ 2015-08-22 15:43 gqtc 阅读(110) 评论(0) 推荐(0)
摘要:The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer... 阅读全文
posted @ 2015-08-22 15:25 gqtc 阅读(141) 评论(0) 推荐(0)
摘要:Given an array of non-negative integers, you are initially positioned at the firstindex of the array. Each element in the array represent... 阅读全文
posted @ 2015-08-16 16:12 gqtc 阅读(117) 评论(0) 推荐(0)
摘要:'?' Matches any single character.'*' Matches any sequence of characters(including the empty sequence). The matching should cover the entireinput strin... 阅读全文
posted @ 2015-08-15 11:01 gqtc 阅读(120) 评论(0) 推荐(0)