Week 15 of Arts | longest palindrome subsequence of leetcode | from Uber’s go programming specification



Arts is an activity launched by Chen Hao in the geek time column. The aim is to keep learning through sharing.

Each person writes arts once a week: algorithm is an algorithm problem, review is to read an English article, technique / tips is to share a small technology, share is to share a point of view.

This week’s content

This week’s arts you’ll see:

  1. The longest palindrome subsequence of leetcode 516
  2. Golang programming specification from Uber


This week’s algorithm is leetcode 516. Longest palindromic subsequence, the longest palindromic subsequence

The biggest difference between palindrome sequences and interrogative strings is thatsequenceIt can be discontinuous, butstrandIt must be continuous

So the difference between the longest palindrome string in question 5 and this one is more extensive

//Yes, I copied the answer https://leetcode-cn.com/problems/longest-palindromic-subsequence/solution/zi-xu-lie-wen-ti-tong-yong-si-lu-zui-chang-hui-wen/
//DP [i] [J] represents the length of palindrome subsequence in the range from I to J
//Base case: DP [i] [i] means that a character must be a palindrome subsequence
func longestPalindromeSubseq(s string) int {
    l := len(s)
    dp := make([][]int, l)
    for i := range dp {
        dp[i] = make([]int, l)
    for i := 0; i < l; i++ {
        dp[i][i] = 1

    for i := l - 1; i >= 0; i-- {
        for j := i + 1; j < l; j++ {
            if s[i] == s[j] {
                dp[i][j] = dp[i+1][j-1] + 2
            } else {
                dp[i][j] = max(dp[i+1][j], dp[i][j-1])

    return dp[0][l-1]

func max(a, b int) int {
    if a > b {
        return a
    return b

Review article recommendation

The recommended English article this week is Uber’s internal golang programming specification

This specification includes some programming skills and conventions of golang, as well as some more superficial specifications such as code format. Here are some points I think are more important

  1. usegolintandgo vetCommand
  2. The implementation type of interface is judged by the compiler
  3. Pay attention to the type and encapsulation of errors
  4. If you use atomic operation but feel that the official package is too leaky, you can try Uber’s encapsulation of atomic operation
  5. Use less global variables
  6. Use less anonymous composition, because anonymous composition has to expose the encapsulation details of anonymous properties
  7. Try to use init () less to do initialization work
  8. Using strconv instead of sprintf to do string type conversion
  9. Less type conversion from string to byte
  10. When using make to create slices and maps, it is best to specify capacity
  11. Specify package in lowercase and singular
  12. It is better to use underscores for global variables in packages that are not exposed to the public_ start.
  13. useif len(s) == 0instead ofif s == nilTo determine whether slice is empty
  14. Less use of bare parameters (literal parameters?)
  15. Use the original string wrapped with anti single quotation marks to escape special symbols
  16. Use the way of printing table to build unit test data
  17. Use a variable length function array as an optional parameter for the configuration type

Tip programming skills

There is no technique this week

Share flash

There was no light this week

Reading list of the week

  • Design and implementation of 6.5 scheduler in go language

    1. Three roles of go Ctrip scheduling GMP
      The G structure contains stack related information, as well as stackguard0, defer and – Panic linked list related to preemptive scheduling. It also includes the threads occupied by the current g, sched and goal related data of the scheduler.
      The more important fields contained in M are G0 and currg, where G0 represents the G holding the scheduling stack, and he will deeply participate in the scheduling of the coroutine. Curg is the user g running on the current thread, These are the only two goroutines that the operating system threads are concerned with. In addition, there are processor P running code, processor nextp temporarily stored, and processor oldp that uses threads before executing system calls
      The number of P is set by gomaxprocs. At most, only gomaxprocs active operating system threads can run normally. P is the middle layer between threads and goroutine, and it is also responsible for scheduling waiting queues on threads. It can switch in time when goroutine performs some I / O operations, so as to improve the utilization of threads_PidleRepresents that the G queue on P is empty,_PrunningRepresents that the user code is being executed,_PgcstopP is stopped by GC, etc
  • Implementation of deadlock auto detection deadlock in golang
    A tool for deadlock detection is to encapsulate the lock of go’s sync library, and then detect whether deadlock is possible before each lock