Преглед на файлове

Merge branch 'master' of ssh://111.230.181.241:1111/ikachan/leetcode-go

dengxinyi преди 6 години
родител
ревизия
0fee183af9

+ 53 - 0
oj/algorithms/binarytree/traversal.go

@@ -0,0 +1,53 @@
+package main
+
+import "fmt"
+
+type node struct {
+	val   int
+	left  *node
+	right *node
+}
+
+func main() {
+	n8 := node{8, nil, nil}
+	n7 := node{7, nil, nil}
+	n6 := node{6, &n7, &n8}
+	n5 := node{5, nil, nil}
+	n4 := node{4, nil, &n6}
+	n3 := node{3, nil, &n5}
+	n2 := node{2, &n4, nil}
+	n1 := node{1, &n2, &n3}
+	fmt.Println("preorder: ")
+	preorder(&n1)
+	fmt.Println("\ninorder: ")
+	inorder(&n1)
+	fmt.Println("\npostorder: ")
+	postorder(&n1)
+}
+
+func preorder(root *node) {
+	if root == nil {
+		return
+	}
+	fmt.Printf("%d ", root.val)
+	preorder(root.left)
+	preorder(root.right)
+}
+
+func inorder(root *node) {
+	if root == nil {
+		return
+	}
+	inorder(root.left)
+	fmt.Printf("%d ", root.val)
+	inorder(root.right)
+}
+
+func postorder(root *node) {
+	if root == nil {
+		return
+	}
+	postorder(root.left)
+	postorder(root.right)
+	fmt.Printf("%d ", root.val)
+}

+ 17 - 0
oj/algorithms/unionfind/.project

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>unionfind</name>
+	<comment>Project unionfind created by Buildship.</comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
+	</natures>
+</projectDescription>

+ 16 - 0
oj/bytedance-2019-spring/no1/coin.go

@@ -0,0 +1,16 @@
+package main
+
+import "fmt"
+
+func main() {
+	var N int
+	fmt.Scan(&N)
+	charge := 1024 - N
+	cnt, coin := 0, 64
+	for i := 0; i < 4; i++ {
+		cnt += charge / coin
+		charge %= coin
+		coin /= 4
+	}
+	fmt.Println(cnt)
+}

+ 34 - 0
oj/bytedance-2019-spring/no2/typo.go

@@ -0,0 +1,34 @@
+package main
+
+import "fmt"
+
+func main() {
+	var N int
+	fmt.Scan(&N)
+	for cid := 0; cid < N; cid++ {
+		var s string
+		fmt.Scan(&s)
+		str := []rune(s)
+		for i := 0; i < len(str)-2; i++ {
+			if aaa(str, i) {
+				str = append(str[:i], str[i+1:]...)
+				i--
+			} else if aabb(str, i) {
+				str = append(str[:i+3], str[i+4:]...)
+				i--
+			}
+		}
+		fmt.Println(string(str))
+	}
+}
+
+func aaa(str []rune, i int) bool {
+	return str[i] == str[i+1] && str[i+1] == str[i+2]
+}
+
+func aabb(str []rune, i int) bool {
+	if len(str) <= i+3 {
+		return false
+	}
+	return str[i] == str[i+1] && str[i+1] != str[i+2] && str[i+2] == str[i+3]
+}

BIN
oj/bytedance-2019-spring/no3/Main.class


+ 36 - 0
oj/bytedance-2019-spring/no3/Main.java

@@ -0,0 +1,36 @@
+import java.util.Scanner;
+
+public class Main {
+    public static void main(String[] args) {
+        Scanner sc = new Scanner(System.in);
+        int N = sc.nextInt();
+        for (int cid = 0; cid < N; cid++) {
+            int n = sc.nextInt();
+            int[] a = new int[n];
+            int[] reward = new int[n];
+            for (int i = 0; i < n; i++) {
+                a[i] = sc.nextInt();
+            }
+            for (int i = 0, idx = 0; i < 2 * n; i++) {
+                int pre = (idx + n - 1) % n;
+                if (a[pre] < a[idx] && reward[idx] <= reward[pre]) {
+                    reward[idx] = reward[pre] + 1;
+                }
+                idx = (idx + 1) % n;
+            }
+            for (int i = 0, idx = 0; i < 2 * n; i++) {
+                int nxt = (idx + 1) % n;
+                if (a[nxt] < a[idx] && reward[idx] <= reward[nxt]) {
+                    reward[idx] = reward[nxt] + 1;
+                }
+                idx = (idx + n - 1) % n;
+            }
+            int sum = 0;
+            for (int i = 0; i < n; i++) {
+                sum += reward[i];
+            }
+            System.out.println(sum + n);
+        }
+        sc.close();
+    }
+}

+ 36 - 0
oj/bytedance-2019-spring/no3/reward.go

@@ -0,0 +1,36 @@
+package main
+
+import "fmt"
+
+func main() {
+	var N int
+	fmt.Scan(&N)
+	for cid := 0; cid < N; cid++ {
+		var n int
+		fmt.Scan(&n)
+		a := make([]int, n)
+		for i := range a {
+			fmt.Scan(&a[i])
+		}
+		reward := make([]int, n)
+		for i, idx := 0, 0; i < n+2; i++ {
+			pre := (idx + n - 1) % n
+			if a[pre] < a[idx] && reward[idx] <= reward[pre] {
+				reward[idx] = reward[pre] + 1
+			}
+			idx = (idx + 1) % n
+		}
+		for i, idx := 0, 0; i < n+2; i++ {
+			nxt := (idx + 1) % n
+			if a[nxt] < a[idx] && reward[idx] <= reward[nxt] {
+				reward[idx] = reward[nxt] + 1
+			}
+			idx = (idx + n - 1) % n
+		}
+		sum := 0
+		for _, r := range reward {
+			sum += r
+		}
+		fmt.Println(sum + n)
+	}
+}

+ 39 - 0
oj/bytedance-2019-spring/no4/rope.go

@@ -0,0 +1,39 @@
+package main
+
+import (
+	"fmt"
+)
+
+const eps float64 = 0.0001
+
+func main() {
+	var N, M int
+	fmt.Scan(&N, &M)
+	L := make([]int, N)
+	max := 0
+	for i := range L {
+		fmt.Scan(&L[i])
+		if max < L[i] {
+			max = L[i]
+		}
+	}
+	beg, end := 0.0, float64(max)
+	for eps < end-beg { // Loop for 100 (or so) times is also valid.
+		mid := (beg + end) / 2.0
+		if solve(L, M, mid) {
+			beg = mid
+		} else {
+			end = mid
+		}
+	}
+	fmt.Printf("%.2f", beg)
+}
+
+func solve(L []int, M int, l float64) bool {
+	cnt := 0
+	for i := range L {
+		length := float64(L[i])
+		cnt += int(length / l)
+	}
+	return M <= cnt
+}

BIN
oj/microsoft-2017/no1/Main.class


+ 27 - 0
oj/microsoft-2017/no1/Main.java

@@ -0,0 +1,27 @@
+import java.util.Scanner;
+
+public class Main {
+    public static void main(String[] args) {
+        Scanner scanner = new Scanner(System.in);
+        int P = scanner.nextInt();
+        int Q = scanner.nextInt();
+        int N = scanner.nextInt();
+        double res = 0.0;
+        int p1 = 0;
+        // E(X + Y) = E(X) + E(Y)
+        // Getting the ith item is independent!
+        for (int i = 0; i < N; i++) {
+            if (8 <= i) p1 = 0;
+            else p1 = P / (int) Math.pow(2, i);
+            double p2 = 1.0;
+            for (int j = 1; j <= 100; j++) {
+                res += p1 / 100.0 * p2 * j;
+                if (p1 == 100) break;
+                p2 *= 1.0 - p1 / 100.0;
+                p1 = p1 + Q > 100 ? 100 : p1 + Q;
+            }
+        }
+        System.out.printf("%.2f", res);
+        scanner.close();
+    }
+}

BIN
oj/microsoft-2017/no2/Main.class


+ 32 - 0
oj/microsoft-2017/no2/Main.java

@@ -0,0 +1,32 @@
+import java.util.Scanner;
+
+public class Main {
+    public static void main(String[] args) {
+        Scanner sc = new Scanner(System.in);
+        int N = sc.nextInt(); // Num of nodes
+        int M = sc.nextInt(); // Num of depth
+        int K = sc.nextInt(); // Num of leaves
+        int[] A = new int[M]; // Num of nodes at depth i
+        for (int i = 0; i < M; i++) {
+            A[i] = sc.nextInt();
+        }
+        int[][] num = new int[M][];
+        for (int i = 0; i < M; i++) {
+            num[i] = new int[A[i]];
+            for (int j = 0; j < A[i]; j++) {
+                num[i][j] = sc.nextInt();
+            }
+        }
+        int[] L = new int[K];
+        for (int i = 0; i < K; i++) {
+            L[i] = sc.nextInt();
+        }
+        int[][] D = new int[K][K];
+        for (int i = 0; i < K; i++) {
+            for (int j = 0; j < K; j++) {
+                D[i][j] = sc.nextInt();
+            }
+        }
+        sc.close();
+    }
+}

+ 11 - 0
oj/microsoft-2017/no2/test.txt

@@ -0,0 +1,11 @@
+8 3 5
+1 3 4
+1
+2 3 4
+5 6 7 8
+3 5 6 7 8
+0 3 3 3 3
+3 0 2 4 4
+3 2 0 4 4
+3 4 4 0 2
+3 4 4 2 0