Skip to content

Instantly share code, notes, and snippets.

@frankchang0125
frankchang0125 / 338_counting_bits_16.csv
Last active September 19, 2018 16:05
338. Counting Bits
Decimal Binary Num of 1s
0 0 0
1 01 1
2 10 1
3 11 2
4 100 1
5 101 2
6 110 2
7 111 3
8 1000 1
@frankchang0125
frankchang0125 / 338_counting_bits_5.csv
Last active September 19, 2018 15:50
338. Counting Bits
Decimal Binary Num of 1s
0 0 0
1 01 1
2 10 1
3 11 2
4 100 1
5 101 2
@frankchang0125
frankchang0125 / 338_counting_bits.go
Last active September 19, 2018 15:36
338. Counting Bits
func countBits(num int) []int {
results := make([]int, num+1)
bitsMap := []int{0, 1, 1, 2}
base := 4
for i := 0; i < 4; i++ {
if i > num {
return results
}
@frankchang0125
frankchang0125 / 27_remove_element.go
Created September 18, 2018 16:03
27. Remove Element
func removeElement(nums []int, val int) int {
n := 0
for i, num := range nums {
if num != val {
nums[n] = nums[i]
n++
}
}
return n
}
@frankchang0125
frankchang0125 / 14_longest_common_prefix.go
Created September 18, 2018 14:48
14. Longest Common Prefix
func longestCommonPrefix(strs []string) string {
prefix := ""
if len(strs) == 0 {
return prefix
}
i := 0
for {
@frankchang0125
frankchang0125 / 13_roman_to_integer.go
Last active September 9, 2018 11:59
13. Roman to Integer
func romanToInt(s string) int {
sum := 0
var prev string
for i := len(s) - 1; i >= 0; i-- {
c := string(s[i])
switch c {
case "I":
if prev == "V" || prev == "X" {
sum -= 1
@frankchang0125
frankchang0125 / 665_decreasing_array.go
Last active September 9, 2018 11:17
665. Non-decreasing Array
func checkPossibility(nums []int) bool {
const MaxUint = ^uint(0)
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1
chance := true
max := MinInt
for i := 0; i < len(nums)-1; i++ {
if nums[i] > nums[i+1] {
if chance {
@frankchang0125
frankchang0125 / 707_design_linked_list.go
Created September 1, 2018 07:40
707. Design Linked List
type MyLinkedList struct {
head *Node
}
type Node struct {
next *Node
val int
}
@frankchang0125
frankchang0125 / docker
Last active September 11, 2018 08:43
Docker useful commands
# Docker connect to host machine:
1. Use `--net="host"` in docker `run` command, then connet with `127.0.0.1`
2. Use: `host.docker.internal`
# Docker's localhost IP:
172.17.0.1
# Run docker in interactive Bash session
$ docker run -it <image> /bin/bash
@frankchang0125
frankchang0125 / waitAllPromisesCompleted.js
Last active June 26, 2017 09:19
Wait for all promises to completed, even rejected promises.
const first = new Promise((resolve, reject) => {
setTimeout(() => resolve("first"), 1000);
});
const second = new Promise((resolve, reject) => {
setTimeout(() => reject("second error"), 2000);
});
const third = new Promise((resolve, reject) => {
setTimeout(() => resolve("third"), 3000);