# Notes for Leetcode

## 1.Binary Number Representation

x = 0b1111; // which is equals to x = 15; and is useful when use the number for masking.


## 2.Rotate Function

### Original Question

Given an array of integers $A$ and let $n$ to be its length.

Assume $B_k$ to be an array obtained by rotating the array $A$ $k$ positions clock-wise, we define a "rotation function" $F$ on $A$ as follow: $$F(k) = 0 * B_k[0] + 1 * B_k[1] + \cdots + (n-1) * B_k[n-1].$$ Calculate the maximum value of $F(0), F(1), \cdots, F(n-1)$. Example:

A = [4, 3, 2, 6]
F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25
F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16
F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23
F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26


So the maximum value of $F(0), F(1), F(2), F(3)$ is $F(3) = 26$.

### 思路(来源)

public int maxRotateFunction(int[] A) {
if (A.length <= 1) {
return 0;
}
int max = Integer.MIN_VALUE;
int sum = 0;
int ai = 0;
for (int i = 0; i < A.length; i++) {
sum += A[i];
ai += i * A[i];
}
max = ai;
for (int i = A.length - 1; i > 0; i--) {
ai += sum - A.length * A[i];
max = Math.max(max, ai);
}
return max;
}


## 3. Pascal's Triangle(杨辉三角)

### 问题

Given an index $k(k=1,2,3,\cdots)$ , return the $k^{th}$ row of the Pascal's triangle.

For example, given k = 3,

Return [1,3,3,1].

### 杨辉三角的性质

1. 第$n$行有$n$项。
2. 第$n$行数字和为$2^n-1$。
3. 第$n$行的第$m$的数字为$C_{n}^{m-1}$(二项式系数)。

### 组合计算

$C_{n}^{m}=\frac{A_{n}^{m}}{n!}=\frac{n(n-1)(n-2)\cdots(n-m+1)}{m!}$

public int combine(int m, int n) {
long res = 1;
for (int i = 1; i <= n; i++) {
res = res * (m - i + 1) / i;
}
return (int) res;
}

public List<Integer> getRow(int rowIndex) {
List<Integer> result = new ArrayList(rowIndex);
for (int i = 0; i <= rowIndex; i++) {
result.add(combine(rowIndex, i));
}
return result;
}
}


### 4.Convert Char to Numeric

Character.getNumericValue('5');
int number = '5' - '0';


### 7. LinkedHashSet

LinkedHashSet<Integer> set = new LinkedHashSet();
set.add(1);
set.add(2);
set.add(3);
System.out.println(set);
//:~output [1, 2, 3]


### 11. TreeSet celling and floor

   /**
* Returns the least key greater than or equal to the given key,
* or {@code null} if there is no such key.
*
* @param key the key
* @return the least key greater than or equal to {@code key},
*         or {@code null} if there is no such key
* @throws ClassCastException if the specified key cannot be compared
*         with the keys currently in the map
* @throws NullPointerException if the specified key is null
*         and this map does not permit null keys
*/
K ceilingKey(K key);

/**
* Returns the greatest key less than or equal to the given key,
* or {@code null} if there is no such key.
*
* @param key the key
* @return the greatest key less than or equal to {@code key},
*         or {@code null} if there is no such key
* @throws ClassCastException if the specified key cannot be compared
*         with the keys currently in the map
* @throws NullPointerException if the specified key is null
*         and this map does not permit null keys
*/
K floorKey(K key);