fork 自 sduonline/sc-resources
增加淘宝群内容,修改部分文件组织
此提交包含在:
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>01-复杂度</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
@@ -0,0 +1,11 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
未顯示二進位檔案。
未顯示二進位檔案。
未顯示二進位檔案。
@@ -0,0 +1,162 @@
|
||||
package com.mj;
|
||||
|
||||
public class Main {
|
||||
|
||||
/* 0 1 2 3 4 5
|
||||
* 0 1 1 2 3 5 8 13 ....
|
||||
*/
|
||||
|
||||
// O(2^n)
|
||||
public static int fib1(int n) {
|
||||
if (n <= 1) return n;
|
||||
return fib1(n - 1) + fib1(n - 2);
|
||||
}
|
||||
|
||||
// O(n)
|
||||
public static int fib2(int n) {
|
||||
if (n <= 1) return n;
|
||||
|
||||
int first = 0;
|
||||
int second = 1;
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
int sum = first + second;
|
||||
first = second;
|
||||
second = sum;
|
||||
}
|
||||
return second;
|
||||
}
|
||||
|
||||
public static int fib3(int n) {
|
||||
if (n <= 1) return n;
|
||||
|
||||
int first = 0;
|
||||
int second = 1;
|
||||
while (n-- > 1) {
|
||||
second += first;
|
||||
first = second - first;
|
||||
}
|
||||
return second;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
int n = 12;
|
||||
|
||||
System.out.println(fib2(n));
|
||||
System.out.println(fib3(n));
|
||||
|
||||
// TimeTool.check("fib1", new Task() {
|
||||
// public void execute() {
|
||||
// System.out.println(fib1(n));
|
||||
// }
|
||||
// });
|
||||
//
|
||||
// TimeTool.check("fib2", new Task() {
|
||||
// public void execute() {
|
||||
// System.out.println(fib2(n));
|
||||
// }
|
||||
// });
|
||||
}
|
||||
|
||||
public static void test1(int n) {
|
||||
// 汇编指令
|
||||
|
||||
// 1
|
||||
if (n > 10) {
|
||||
System.out.println("n > 10");
|
||||
} else if (n > 5) { // 2
|
||||
System.out.println("n > 5");
|
||||
} else {
|
||||
System.out.println("n <= 5");
|
||||
}
|
||||
|
||||
// 1 + 4 + 4 + 4
|
||||
for (int i = 0; i < 4; i++) {
|
||||
System.out.println("test");
|
||||
}
|
||||
|
||||
// 140000
|
||||
// O(1)
|
||||
// O(1)
|
||||
}
|
||||
|
||||
public static void test2(int n) {
|
||||
// O(n)
|
||||
// 1 + 3n
|
||||
for (int i = 0; i < n; i++) {
|
||||
System.out.println("test");
|
||||
}
|
||||
}
|
||||
|
||||
public static void test3(int n) {
|
||||
// 1 + 2n + n * (1 + 3n)
|
||||
// 1 + 2n + n + 3n^2
|
||||
// 3n^2 + 3n + 1
|
||||
// O(n^2)
|
||||
|
||||
// O(n)
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
System.out.println("test");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void test4(int n) {
|
||||
// 1 + 2n + n * (1 + 45)
|
||||
// 1 + 2n + 46n
|
||||
// 48n + 1
|
||||
// O(n)
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < 15; j++) {
|
||||
System.out.println("test");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void test5(int n) {
|
||||
// 8 = 2^3
|
||||
// 16 = 2^4
|
||||
|
||||
// 3 = log2(8)
|
||||
// 4 = log2(16)
|
||||
|
||||
// 执行次数 = log2(n)
|
||||
// O(logn)
|
||||
while ((n = n / 2) > 0) {
|
||||
System.out.println("test");
|
||||
}
|
||||
}
|
||||
|
||||
public static void test6(int n) {
|
||||
// log5(n)
|
||||
// O(logn)
|
||||
while ((n = n / 5) > 0) {
|
||||
System.out.println("test");
|
||||
}
|
||||
}
|
||||
|
||||
public static void test7(int n) {
|
||||
// 1 + 2*log2(n) + log2(n) * (1 + 3n)
|
||||
|
||||
// 1 + 3*log2(n) + 2 * nlog2(n)
|
||||
// O(nlogn)
|
||||
for (int i = 1; i < n; i = i * 2) {
|
||||
// 1 + 3n
|
||||
for (int j = 0; j < n; j++) {
|
||||
System.out.println("test");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void test10(int n) {
|
||||
// O(n)
|
||||
int a = 10;
|
||||
int b = 20;
|
||||
int c = a + b;
|
||||
int[] array = new int[n];
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
System.out.println(array[i] + c);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
package com.mj;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class Times {
|
||||
private static final SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss.SSS");
|
||||
|
||||
public interface Task {
|
||||
void execute();
|
||||
}
|
||||
|
||||
public static void test(String title, Task task) {
|
||||
if (task == null) return;
|
||||
title = (title == null) ? "" : ("【" + title + "】");
|
||||
System.out.println(title);
|
||||
System.out.println("开始:" + fmt.format(new Date()));
|
||||
long begin = System.currentTimeMillis();
|
||||
task.execute();
|
||||
long end = System.currentTimeMillis();
|
||||
System.out.println("结束:" + fmt.format(new Date()));
|
||||
double delta = (end - begin) / 1000.0;
|
||||
System.out.println("耗时:" + delta + "秒");
|
||||
System.out.println("-------------------------------------");
|
||||
}
|
||||
}
|
新增問題並參考
封鎖使用者