2019年4月

最终效果如下

2019-04-02T02:54:35.png
2019-04-02T02:57:55.png

代码如下

const baseURL = 'http://wxlc.wezoz.com/' // your base url
async function request(api, method, header, params) {
    console.group(api)
    console.log('%c' + method + ' REQUEST:', "color:green")
    console.log({
        'api': api,
        'header': header,
        'params': params
    })
    return await new Promise((resolve, reject) => {
        uni.request({
            url: baseURL + api,
            method: method,
            header: header,
            data: params,
            success: (result) => {
                console.log('%c' + method + ' SUCCESS RETURN DATA:', "color:green")
                console.log(result)
                console.groupEnd(api)
                resolve(result.data);
            },
            fail: (e) => {
                reject(e);
            }
        })
    })
}
async function post(api, params, success) {
    const POST_METHOD = 'POST'
    const POST_HEADER = {
        'content-type': 'application/x-www-form-urlencoded',
        'token': wx.getStorageSync('token')
    }
    return await request(api, POST_METHOD, POST_HEADER, params)
}
async function get(api, params, success) {
    const GET_METHOD = 'GET'
    const GET_HEADER = {
        'content-type': 'application/x-www-form-urlencoded',
        'token': wx.getStorageSync('token')
    }
    return await request(api, GET_METHOD, GET_HEADER, params)
}
module.exports = {
    POST: post,
    GET: get
}

所用到的一些consloe骚操作

console内使用CSS样式

consloe里面使用%c进行css样式格式化输出实例

console.log("%c Hello World", "color:green")

上述语句会输出绿色的Hello world, 效果如下。

2019-04-02T03:25:00.png

然后你就可以根据你的css功底进行创造啦

console内获取程序运行时间

一般大家看到这个题目,第一时间想到的绝对是获取startTime,endTime再求差对不?
其实console提供一个内嵌的console.time()和console.timeEnd(),代码如下
2019-04-02T03:30:19.png

console分组输出

有得时候,大家打开控制台看见一堆输出是不是很头疼?如何在茫茫数据中找到自己想要的数据呢?你可以使用ground分组数据,让console变得清晰明了。
效果如下
2019-04-02T02:57:55.png

结语

我这里只是写了一些我自己常用到的一些功能,其实console对象还有很多函数。想玩一下的可以自己去尝试以下,发挥自己的想象力就会发现一些很有意思的玩法

leetcode(1): two sum

Description

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

0. My solution(Brute Force)

var twoSum = function(nums, target) {
    for(let i = 0; i < nums.length; i++) {
        for(let j = 0; j < nums.length; j++) {
            if(nums[i] + nums[j] === target && i != j) {
                return [i, j]
            }
        }
    }
};

· Time complexity: O(n^2), For each element, I try to find its complement by looping through the rest of array which takes O(n)*O(n) time. Therefore, the time complexity is O(n^2).

· Space complexity : O(1).

1. Improve

var twoSum = function(nums, target) {
    for(let i = 0; i < nums.length; i++) {
        for(let j = i + 1; j < nums.length; j++) {
            if(nums[i] + nums[j] === target) {
                return [i, j]
            }
        }
    }
};

2. Improve again

var twoSum = function(nums, target) {
    for(var i = 0; i< nums.length; i++){
        var complement = target - nums[i];
        var found = nums.indexOf(complement, i + 1);
        if(found !== -1){
            return [i, found];
        }
    }
    return [0, 0];
};

3. Improve again

var twoSum = function(nums, target) {
    if (nums.length === 2) return [0, 1];
    const len = nums.length;
    let hashTable = {};
    for(let i = 0; i < len; i++){
        // Add a new obj to the hashTable where key = nums[i] and value = i
        hashTable[nums[i]] = i;
    }
    
    for(let i = 0; i < len; i++) {
        let complement = target - nums[i];
        let found = hashTable[complement]; // Determine whether the complement exist in the hashTable
        if(found !== undefined && found != i) return [i, found];
    }
    return [0,0];
}

B站视频地址