出现这个问题是你分别在两个地方载入一模一样的包,但是两个包的路径不是同一个地方的,例如你写的client,有个地方载入了client.pb.go ,有个地方载入了server.pb.go,client.pb.go和server.pb.go一模一样,就会出现这种报错,解决方案就是同样的包只载入一个即可

2. Add Two Numbers

我的github有源码https://github.com/ttllpp/leetcode

题目

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

解题思路

(2 -> 4 -> 3)是 342

(5 -> 6 -> 4)是 465

(7 -> 0 -> 8)是 807

342 + 465 = 807

所以,题目的本意是,把整数换了一种表达方式后,实现其加法。

设计程序时候,需要处理的点有

  1. 位上的加法,需要处理进位问题,进位有个规律,a/10取整,可以得到当前进位的值,a%10可以得到当前位的值
  2. 如何进入下一位运算
  3. 按位相加结束后,也还需要处理进位问题。

1. Two Sum

我的github有源码https://github.com/ttllpp/leetcode

题目

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].

解题思路1

a + b = target

也可以看成是

a = target - b

然后依次寻找差值a

解题思路2

a + b = target

最暴力的方法,直接两个循环

解题思路3

a + b = target

也可以看成是

a = target - b

map[整数]整数的序号中,可以查询到a的序号。这样就不用嵌套两个for循环了。

golang高性能string转byte

func StringBytes(s string) []byte {
    return *(*[]byte)(unsafe.Pointer(&s))
}
    
func BytesString(b []byte) string {
    return *(*string)(unsafe.Pointer(&b))
}
    
func OriginStringBytes(s string) []byte {
    return []byte(s)
}
    
func OriginBytesString(b []byte) string {
    return string(b)
}

然后分别测试

package main

import (
    "math/rand"
    "testing"
)

var letters = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
var a = randSeqString(10000)
var c = randSeqByte(10000)

func randSeqString(n int) string {
    b := make([]byte, n)
    for i := range b {
        b[i] = letters[rand.Intn(len(letters))]
    }
    return string(b)
}

func randSeqByte(n int) []byte {
    b := make([]byte, n)
    for i := range b {
        b[i] = letters[rand.Intn(len(letters))]
    }
    return b
}

func Benchmark_StringBytes(b *testing.B) {
    for i := 0; i < b.N; i++ { //use b.N for looping
        StringBytes(a)
    }
}

func Benchmark_BytesString(b *testing.B) {
    for i := 0; i < b.N; i++ { //use b.N for looping
        BytesString(c)
    }
}

func Benchmark_OriginStringBytes(b *testing.B) {
    for i := 0; i < b.N; i++ { //use b.N for looping
        OriginStringBytes(a)
    }
}


go test -test.bench=".*"

goos: linux
goarch: amd64
pkg: test/test5
Benchmark_StringBytes-4             2000000000             0.72 ns/op
Benchmark_BytesString-4             2000000000             1.16 ns/op
Benchmark_OriginStringBytes-4         300000          4649 ns/op
PASS
ok      test/test5    5.398s


花点时间整理了一个docker版本的lnmp+memcache+redis
docker nginx+mysql+php+memcached+redis

github地址:https://github.com/ttllpp/dnmp

1.目录结构

/
├── mysqlCnf                mysql
│   └──  my.cnf               mysql配置文件
├── redis                   redis
│   └──  redis.conf          redis配置文件 开启全内存保存,最大64m
├── php-fpm                 php-fpm
│   ├── php.ini                php.ini
│   └── Dockerfile           build文件
├── nginx                   nginx
│   ├── cert                 证书目录
│   ├── fastcgi.conf         fastcgi配置
│   ├── logs                 访问日志目录
│   ├── nginx.conf           nginx配置
│   └── conf.d               nginx配置
│        └──  default.conf    网站配置        
├── mysql                   mysql数据目录
├── php                     php代码目录
└── memcached               memcached 最大64m

2.启动方式

docker-compose up -d

3.php-fpm

php安装扩展为

  • PHP版本7.2.12
  • mysqli
  • swoole1.10.4
  • igbinary2.0.8
  • memcached3.0.4
  • redis4.2.0