0%

## summary-ranges-228

Posted on 2016-10-12 In leetcode Views: Disqus:

#### problem

Given a sorted integer array without duplicates, return the summary of its ranges.

For example, given `[0,1,2,4,5,7]`, return `["0->2","4->5","7"]`.

#### 题解

每一个区间的起点`nums[i]`加上`j`是否等于`nums[i+j]`
参考

#### Code

    class Solution {
    public:
        vector<string> summaryRanges(vector<int>& nums) {
            int i = 0, j = 1, n;
            vector<string> res;
            n = nums.size();
            while(i < n){
                j = 1;
                while(j < n && nums[i+j] - nums[i] == j) j++;
                res.push_back(j <= 1 ? to_string(nums[i]) : to_string(nums[i]) + "->" + to_string(nums[i + j - 1]));
                i += j;
            }
            return res;
        }
    };

## minimum-size-subarray-sum-209

Posted on 2016-10-11 In leetcode Views: Disqus:

### problem

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn’t one, return 0 instead.

For example, given the array `[2,3,1,2,4,3]` and `s = 7`,
the subarray `[4,3]` has the minimal length under the problem constraint.

#### 题解

参考，滑动窗口，跟之前Data Structure课上的online算法有点像，链接

#### Code

    class Solution {
    public:
        int minSubArrayLen(int s, vector<int>& nums) {
            int len = nums.size();
            if(len == 0) return 0;
            int minlen = INT_MAX;
            int sum = 0;

            int left = 0;
            int right = -1;
            while(right < len)
            {
                while(sum < s && right < len)
                    sum += nums[++right];
                if(sum >= s)
                {
                    minlen = minlen < right - left + 1 ? minlen : right - left + 1;
                    sum -= nums[left++];
                }
            }
            return minlen > len ? 0 : minlen;
        }
    };

## binary-watch

Posted on 2016-09-29 In leetcode Views: Disqus:

### problem

A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).

Each LED represents a zero or one, with the least significant bit on the right.

For example, the above binary watch reads “3:25”.

Given a non-negative integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent.

#### Example:

    Input: n = 1
    Return: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"]

#### Note:

- The order of output does not matter.
- The hour must not contain a leading zero, for example “01:00” is not valid, it should be “1:00”.
- The minute must be consist of two digits and may contain a leading zero, for example “10:2” is not valid, it should be “10:02”.

### 题解

又是参（chao）考（xi）别人的代码，嗯，就是这么不要脸，链接

#### Code

    class Solution {
    public:
        vector<string> readBinaryWatch(int num) {
            vector<string> res;
            for (int h = 0; h < 12; ++h) {
                for (int m = 0; m < 60; ++m) {
                    if (bitset<10>((h << 6) + m).count() == num) {
                        res.push_back(to_string(h) + (m < 10 ? ":0" : ":") + to_string(m));
                    }
                }
            }
            return res;
        }
    };

## 34_Search_for_a_Range

Posted on 2016-08-14 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

    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 In docker Views: Disqus:

### docker-mysql-cluster

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

Read more »