# Nicksxs's Blog
learn from zero,技术博客，Nicksxs，史学森

0%

## 34_Search_for_a_Range

Posted on 2016-08-14 Edited on 2020-01-12 In leetcode Views: Disqus:

## question

### 34. Search for a Range

Original Page

Given a sorted array of integers, find the starting and ending position of a given target value.

Your algorithm’s runtime complexity must be in the order of O(log n).

If the target is not found in the array, return `[-1, -1]`.

For example,
Given `[5, 7, 7, 8, 8, 10]` and target value 8,
return `[3, 4]`.

## analysis

一开始就想到了二分查找，但是原来做二分查找的时候一般都是找到确定的那个数就完成了，
这里的情况比较特殊，需要找到整个区间，所以需要两遍查找，并且一个是找到小于target
的最大索引，一个是找到大于target的最大索引，代码参考leetcode discuss,这位仁
兄也做了详细的分析解释。

## code

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29

    class Solution {
    public:
        vector<int> searchRange(vector<int>& nums, int target) {
            vector<int> ret(2, -1);
            int i = 0, j = nums.size() - 1;
            int mid;
            while(i < j){
                mid = (i + j) / 2;
                if(nums[mid] < target) i = mid + 1;
                else j = mid;
            }
            if(nums[i] != target) return ret;
            else {
                ret[0] = i;
                if((i+1) < (nums.size() - 1) && nums[i+1] > target){
                    ret[1] = i;
                    return ret;
                }
            }   //一点小优化
            j = nums.size() - 1;
            while(i < j){
                mid = (i + j) / 2 + 1;
                if(nums[mid] > target) j = mid - 1;
                else i = mid;
            }
            ret[1] = j;
            return ret;
        }
    };

## docker-mysql-cluster

Posted on 2016-08-14 Edited on 2020-01-12 In docker Views: Disqus:

### docker-mysql-cluster

基于docker搭了个mysql集群，稍微记一下，
首先是新建mysql主库容

Read more »

## swoole-websocket-test

Posted on 2016-07-13 Edited on 2020-01-12 In php Views: Disqus:

### 玩一下swoole的websocket

WebSocket是HTML5开始提供的一种在单个TCP连接上进行全双工通讯的协议。WebSocket通信协议于2011年被IETF定为标准RFC 6455，WebSocketAPI被W3C定为标准。
,在web私信，im等应用较多。背景和优缺点可以参看wiki。

### 环境准备

因为swoole官方还不支持windows，所以需要装下linux,之前都是用ubuntu，
这次就试一下centos7，还是满好看的，虽然虚拟机会默认最小安装，需要在安装
时自己选择带gnome的，当然最小安装也是可以的，只是最后需要改下防火墙。
然后是装下PHP，Nginx什么的，我是用Oneinstack，可以按需安装
给做这个的大大点个赞。

Read more »

## invert-binary-tree

Posted on 2015-06-22 Edited on 2020-01-12 In leetcode Views: Disqus:

Invert a binary tree

         4
       /   \
      2     7
     / \   / \
    1   3 6   9

to

         4
       /   \
      7     2
     / \   / \
    9   6 3   1

Trivia:
This problem was inspired by this original tweet by Max Howell:

> Google: 90% of our engineers use the software you wrote (Homebrew),
> but you can’t invert a binary tree on a whiteboard so fuck off.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        TreeNode* invertTree(TreeNode* root) {
            if(root == NULL) return root;
            TreeNode* temp;
            temp = invertTree(root->left);
            root->left = invertTree(root->right);
            root->right = temp;
            return root;
        }
    };

## leetcode no.3

Posted on 2015-04-15 Edited on 2020-01-12 In leetcode Views: Disqus:

*Longest Substring Without Repeating Characters *

Read more »