学习笔记10.18

剑指Offer

  • 1.用n个2*1小矩形无重叠覆盖一个2*n的大矩形,总共有多少种方法。

如果第一次竖着摆放,总共f(n-1)种;如果第一个横着摆放,总共f(n-2)种。所以f(n)=f(n-1)+f(n-2),即斐波那契数列。

  • 2.输入一个整数,输出二进制表示中1的个数。

用位运算,整数&1即二进制表示最低位,依次右移位运算。

  • 3.给定一个double类型的浮点数base和int类型的整数exponent,求base的exponent次方。

循环求幂:最简单的实现,效率不高O(N)。

简单快速幂:

比如求5^999。把999换成二进制为1111100111。5^999=5*5^2*5^4……。

class Solution {
public:
    double Power(double base, int exponent) {
        double res = 1.0;
        int ex = abs(exponent);
        while(ex)
        {
            if(ex&1) res *= base;
            base *= base;
            ex = ex>>1;
        }
        return exponent>0?res:1/res;
    }
};
  • 4.给定一个整数数组,把所有的奇数放到前面,偶数放到后面,不能改变相对顺序。

冒泡排序:从序列头部开始,两两比较,根据大小交换位置,直到把最大(小)数据元素放到队尾,下一次排序前n-1个元素,直到排序完成。不过复杂度比较高,最差是n的平方。

class Solution {
public:
    void reOrderArray(vector &array) {
 
         
        for (int i = 0; i < array.size();i++)
        {
            for (int j = array.size() - 1; j>i;j--)
            {
                if (array[j] % 2 == 1 && array[j - 1]%2 == 0) //前偶后奇交换
                {
                    swap(array[j], array[j-1]);
                }
            }
        }
    }
};

还是用一个比较简单的方法,先循环一遍,把偶数保存在一个数组里,然后再加回去。这里主要用到了vector的erase函数,参数是迭代器,返回值是被删除元素的1下一个迭代器。

class Solution {
public:
    void reOrderArray(vector &array) 
    {
        vector res;
        vector::iterator pos1 = array.begin();
        while(pos1!=array.end())
        {
            if(*pos1%2==0)
            {
                res.push_back(*pos1);
                pos1 = array.erase(pos1);
            }
            else pos1++;
        }
        for(pos1 = res.begin(); pos1 != res.end(); pos1++)
            array.push_back(*pos1);
    }
};

Redis

继续看昨天没看完的跳跃表源码。

插入节点

zskiplistNode *zslInsert(zskiplist *zsl, double score, robj *obj) {
    zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
    unsigned int rank[ZSKIPLIST_MAXLEVEL];
    int i, level;

    redisAssert(!isnan(score));

    // 在各个层查找节点的插入位置
    // T_wrost = O(N^2), T_avg = O(N log N)
    x = zsl->header;
    for (i = zsl->level-1; i >= 0; i--) {

        /* store rank that is crossed to reach the insert position */
        // 如果 i 不是 zsl->level-1 层
        // 那么 i 层的起始 rank 值为 i+1 层的 rank 值
        // 各个层的 rank 值一层层累积
        // 最终 rank[0] 的值加一就是新节点的前置节点的排位
        // rank[0] 会在后面成为计算 span 值和 rank 值的基础
        rank[i] = i == (zsl->level-1) ? 0 : rank[i+1];

        // 沿着前进指针遍历跳跃表
        // T_wrost = O(N^2), T_avg = O(N log N)
        while (x->level[i].forward &&
            (x->level[i].forward->score < score ||
                // 比对分值
                (x->level[i].forward->score == score &&
                // 比对成员, T = O(N)
                compareStringObjects(x->level[i].forward->obj,obj) < 0))) {

            // 记录沿途跨越了多少个节点
            rank[i] += x->level[i].span;

            // 移动至下一指针
            x = x->level[i].forward;
        }
        // 记录将要和新节点相连接的节点
        update[i] = x;
    }

    /* we assume the key is not already inside, since we allow duplicated
     * scores, and the re-insertion of score and redis object should never
     * happen since the caller of zslInsert() should test in the hash table
     * if the element is already inside or not. 
     *
     * zslInsert() 的调用者会确保同分值且同成员的元素不会出现,
     * 所以这里不需要进一步进行检查,可以直接创建新元素。
     */

    // 获取一个随机值作为新节点的层数
    // T = O(N)
    level = zslRandomLevel();

    // 如果新节点的层数比表中其他节点的层数都要大
    // 那么初始化表头节点中未使用的层,并将它们记录到 update 数组中
    // 将来也指向新节点
    if (level > zsl->level) {

        // 初始化未使用层
        // T = O(1)
        for (i = zsl->level; i < level; i++) {
            rank[i] = 0;
            update[i] = zsl->header;
            update[i]->level[i].span = zsl->length;
        }

        // 更新表中节点最大层数
        zsl->level = level;
    }

    // 创建新节点
    x = zslCreateNode(level,score,obj);

    // 将前面记录的指针指向新节点,并做相应的设置
    // T = O(1)
    for (i = 0; i < level; i++) {
        
        // 设置新节点的 forward 指针
        x->level[i].forward = update[i]->level[i].forward;
        
        // 将沿途记录的各个节点的 forward 指针指向新节点
        update[i]->level[i].forward = x;

        /* update span covered by update[i] as x is inserted here */
        // 计算新节点跨越的节点数量
        x->level[i].span = update[i]->level[i].span - (rank[0] - rank[i]);

        // 更新新节点插入之后,沿途节点的 span 值
        // 其中的 +1 计算的是新节点
        update[i]->level[i].span = (rank[0] - rank[i]) + 1;
    }

    /* increment span for untouched levels */
    // 未接触的节点的 span 值也需要增一,这些节点直接从表头指向新节点
    // T = O(1)
    for (i = level; i < zsl->level; i++) {
        update[i]->level[i].span++;
    }

    // 设置新节点的后退指针
    x->backward = (update[0] == zsl->header) ? NULL : update[0];
    if (x->level[0].forward)
        x->level[0].forward->backward = x;
    else
        zsl->tail = x;

    // 跳跃表的节点计数增一
    zsl->length++;

    return x;
}

 

你可能感兴趣的:(学习笔记10.18)