Leetcode Linux 读书笔记
2017-10-18

Product of Array Except Self

Problem

Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

Solve it without division and in O(n).

For example, given [1,2,3,4], return [24,12,8,6].

Write a function to compute the next state (after one update) of the board given its current state.

2017-10-12

Game of Life

Problem

According to the Wikipedia’s article: “The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.”

Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):

  1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
  2. Any live cell with two or three live neighbors lives on to the next generation.
  3. Any live cell with more than three live neighbors dies, as if by over-population..
  4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

Write a function to compute the next state (after one update) of the board given its current state.

2017-10-11

Shortest Unsorted Continuous Subarray

Problem

Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.

You need to find the shortest such subarray and output its length.

2017-10-10

Task Scheduler

Problem

Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle.

However, there is a non-negative cooling interval n that means between two same tasks, there must be at least n intervals that CPU are doing different tasks or just be idle.

You need to return the least number of intervals the CPU will take to finish all the given tasks.

2017-09-13

Python Core Programing Notes

  1. 缩进4个空格长度,避免使用制表符

  2. python的赋值语句不会返回值,如y = (x = x+1)是错误的

  3. 交换两个值:x, y = y, x

  4. python不支持重载标识符

2017-09-07

Effective Java Notes

  1. 用静态工厂方法代替构造器

    • 常用的方法名称:valueOf, getInstance, getType…
    • 优势
      • 有方法名称,增强可读性
      • 不必每次在调用的时候都常见一个新对象
2017-08-24

Container With Most Water

Problem

Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container and n is at least 2.

2017-08-22

Two Sum IV - Input is a BST

Problem

Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.

1%