随笔分类 - 算法
摘要:在介绍加权轮询算法(WeightedRound-Robin)之前,首先介绍一下轮询算法(Round-Robin)。 一:轮询算法(Round-Robin) 轮询算法是最简单的一种负载均衡算法。它的原理是把来自用户的请求轮流分配给内部的服务器:从服务器1开始,直到服务器N,然后重新开始循环。 算法的优
阅读全文
摘要:负载均衡的那些算法们by:简单的老王 上周发了问卷,想了解一下大家对老王有没有什么建议,然后好多朋友都投了票,想了解编程技术和服务器架构的干货,所以接下来会先聊聊编程和架构相关的算法,然后大概在6月下旬会跟大家聊聊面试那些事儿(老王到目前大约参加了几百次的面试,可以从面试官的角度来聊聊不一样的面试)...
阅读全文
摘要:Redis的sentinel模式使用了Hiredis代码,Hiredis是redis数据库一个轻量级的C语言客户端库。它实现的向Redis发送命令的API函数redisCommand,使用方法类似于printf。因此只要熟悉redis命令,就可以很容易的使用该函数将redis命令字...
阅读全文
摘要:dict.c中的dictScan函数,用来遍历字典,迭代其中的每个元素。该函数使用的算法非常精妙!!!所以必须记录一下。 遍历一个稳定的字典,当然不是什么难事,但Redis中的字典因为有rehash的过程,使字典可能扩展,也可能缩小。这就带来了问题,如果在两次遍历中...
阅读全文
摘要:一个无符号的整数,如果需要翻转其二进制位,可以采用下面的方法,以32位整数为例:unsigned int v; // 32-bit word to reverse bit order// swap odd and even bitsv = ((v >> 1) & 0x55555555...
阅读全文
摘要:Libev中在管理定时器时,使用了堆这种结构,而且除了常见的最小2叉堆之外,它还实现了更高效的4叉堆。 之所以要实现4叉堆,是因为普通2叉堆的缓存效率较低,所谓缓存效率低,也就是说对CPU缓存的利用率比较低,说白了,就是违背了局部性原理。这是因为在2叉堆中,对元素的...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要:Implement int sqrt(int x). Compute and return the square root of x. 1:二分查找 思路:要实现一个sqrt函数,可以使用二分法,首先确定一个范围[begin, end],这个范围的中间数m...
阅读全文
摘要: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. ...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要:Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the interval...
阅读全文
摘要: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], [...
阅读全文
摘要: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 ...
阅读全文
摘要: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...
阅读全文
摘要:Given an array of non-negative integers, you are initially positioned at the firstindex of the array. Each element in the array represent...
阅读全文
摘要:'?' Matches any single character.'*' Matches any sequence of characters(including the empty sequence). The matching should cover the entireinput strin...
阅读全文