Add Java codes, and license.
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
package chapter_computational_complexity.space_complexity;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
public class space_complexity_types {
|
||||
/* 函数 */
|
||||
static int function() {
|
||||
// do something
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 常数阶 */
|
||||
static void constant(int n) {
|
||||
// 常量、变量、对象占用 O(1) 空间
|
||||
final int a = 0;
|
||||
int b = 0;
|
||||
int[] nums = new int[10000];
|
||||
ListNode node = new ListNode(0);
|
||||
// 循环中的变量占用 O(1) 空间
|
||||
for (int i = 0; i < n; i++) {
|
||||
int c = 0;
|
||||
}
|
||||
// 循环中的函数占用 O(1) 空间
|
||||
for (int i = 0; i < n; i++) {
|
||||
function();
|
||||
}
|
||||
}
|
||||
|
||||
/* 线性阶 */
|
||||
static void linear(int n) {
|
||||
// 长度为 n 的数组占用 O(n) 空间
|
||||
int[] nums = new int[n];
|
||||
// 长度为 n 的列表占用 O(n) 空间
|
||||
List<ListNode> nodes = new ArrayList<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
nodes.add(new ListNode(i));
|
||||
}
|
||||
// 长度为 n 的哈希表占用 O(n) 空间
|
||||
Map<Integer, String> map = new HashMap<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
map.put(i, String.valueOf(i));
|
||||
}
|
||||
}
|
||||
|
||||
/* 线性阶(递归实现) */
|
||||
static void linearRecur(int n) {
|
||||
System.out.println("递归 n = " + n);
|
||||
if (n == 1) return;
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 平方阶 */
|
||||
static void quadratic(int n) {
|
||||
// 矩阵占用 O(n^2) 空间
|
||||
int numMatrix[][] = new int[n][n];
|
||||
// 二维列表占用 O(n^2) 空间
|
||||
List<List<Integer>> numList = new ArrayList<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
List<Integer> tmp = new ArrayList<>();
|
||||
for (int j = 0; j < n; j++) {
|
||||
tmp.add(0);
|
||||
}
|
||||
numList.add(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
/* 平方阶(递归实现) */
|
||||
static int quadraticRecur(int n) {
|
||||
if (n <= 0) return 0;
|
||||
int[] nums = new int[n];
|
||||
System.out.println("递归 n = " + n + " 中的 nums 长度 = " + nums.length);
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 指数阶(建立满二叉树) */
|
||||
static TreeNode buildTree(int n) {
|
||||
if (n == 0) return null;
|
||||
TreeNode root = new TreeNode(0);
|
||||
root.left = buildTree(n - 1);
|
||||
root.right = buildTree(n - 1);
|
||||
return root;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
int n = 5;
|
||||
// 常数阶
|
||||
constant(n);
|
||||
// 线性阶
|
||||
linear(n);
|
||||
linearRecur(n);
|
||||
// 平方阶
|
||||
quadratic(n);
|
||||
quadraticRecur(n);
|
||||
// 指数阶
|
||||
TreeNode tree = buildTree(n);
|
||||
PrintUtil.printTree(tree);
|
||||
}
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
package chapter_computational_complexity.space_time_tradeoff;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
class solution_brute_force {
|
||||
public int[] twoSum(int[] nums, int target) {
|
||||
int size = nums.length;
|
||||
for (int i = 0; i < size - 1; i++) {
|
||||
for (int j = i + 1; j < size; j++) {
|
||||
if (nums[i] + nums[j] == target)
|
||||
return new int[] { i, j };
|
||||
}
|
||||
}
|
||||
return new int[0];
|
||||
}
|
||||
}
|
||||
|
||||
class solution_hash_map {
|
||||
public int[] twoSum(int[] nums, int target) {
|
||||
int size = nums.length;
|
||||
Map<Integer, Integer> dic = new HashMap<>();
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (dic.containsKey(target - nums[i])) {
|
||||
return new int[] { dic.get(target - nums[i]), i };
|
||||
}
|
||||
dic.put(nums[i], i);
|
||||
}
|
||||
return new int[0];
|
||||
}
|
||||
}
|
||||
|
||||
public class leetcode_two_sum {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] nums = { 2,7,11,15 };
|
||||
int target = 9;
|
||||
|
||||
// ====== Driver Code ======
|
||||
solution_brute_force slt1 = new solution_brute_force();
|
||||
int[] res = slt1.twoSum(nums, target);
|
||||
System.out.println(Arrays.toString(res));
|
||||
|
||||
solution_hash_map slt2 = new solution_hash_map();
|
||||
res = slt2.twoSum(nums, target);
|
||||
System.out.println(Arrays.toString(res));
|
||||
}
|
||||
}
|
@@ -0,0 +1,149 @@
|
||||
package chapter_computational_complexity.time_complexity;
|
||||
|
||||
public class time_complexity_types {
|
||||
/* 常数阶 */
|
||||
static int constant(int n) {
|
||||
int count = 0;
|
||||
int size = 100000;
|
||||
for (int i = 0; i < size; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 线性阶 */
|
||||
static int linear(int n) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 线性阶(遍历数组) */
|
||||
static int arrayTraversal(int[] nums) {
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成正比
|
||||
for (int num : nums) {
|
||||
// System.out.println(num);
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 平方阶 */
|
||||
static int quadratic(int n) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 平方阶(冒泡排序) */
|
||||
static void bubbleSort(int[] nums) {
|
||||
int n = nums.length;
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
for (int j = 0; j < n - 1 - i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// 交换 nums[j] 和 nums[j + 1]
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 指数阶(循环实现) */
|
||||
static int exponential(int n) {
|
||||
int count = 0, base = 1;
|
||||
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < base; j++) {
|
||||
count++;
|
||||
}
|
||||
base *= 2;
|
||||
}
|
||||
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 指数阶(递归实现) */
|
||||
static int expRecur(int n) {
|
||||
if (n == 1) return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
|
||||
/* 对数阶(循环实现) */
|
||||
static int logarithmic(float n) {
|
||||
int count = 0;
|
||||
while (n > 1) {
|
||||
n = n / 2;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 对数阶(递归实现) */
|
||||
static int logRecur(float n) {
|
||||
if (n <= 1) return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
}
|
||||
|
||||
/* 线性对数阶 */
|
||||
static int linearLogRecur(float n) {
|
||||
if (n <= 1) return 1;
|
||||
int count = linearLogRecur(n / 2) +
|
||||
linearLogRecur(n / 2);
|
||||
for (int i = 0; i < n; i++) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 阶乘阶(递归实现) */
|
||||
static int factorialRecur(int n) {
|
||||
if (n == 0) return 1;
|
||||
int count = 0;
|
||||
// 从 1 个分裂出 n 个
|
||||
for (int i = 0; i < n; i++) {
|
||||
count += factorialRecur(n - 1);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
|
||||
int n = 8;
|
||||
System.out.println("输入数据大小 n = " + n);
|
||||
|
||||
int count = constant(n);
|
||||
System.out.println("常数阶的计算操作数量 = " + count);
|
||||
|
||||
count = linear(n);
|
||||
System.out.println("线性阶的计算操作数量 = " + count);
|
||||
count = arrayTraversal(new int[n]);
|
||||
System.out.println("线性阶(遍历数组)的计算操作数量 = " + count);
|
||||
|
||||
count = quadratic(n);
|
||||
System.out.println("平方阶的计算操作数量 = " + count);
|
||||
|
||||
count = exponential(n);
|
||||
System.out.println("指数阶(循环实现)的计算操作数量 = " + count);
|
||||
count = expRecur(n);
|
||||
System.out.println("指数阶(递归实现)的计算操作数量 = " + count);
|
||||
|
||||
count = logarithmic((float) n);
|
||||
System.out.println("对数阶(循环实现)的计算操作数量 = " + count);
|
||||
count = logRecur((float) n);
|
||||
System.out.println("对数阶(递归实现)的计算操作数量 = " + count);
|
||||
|
||||
count = linearLogRecur((float) n);
|
||||
System.out.println("线性对数阶(递归实现)的计算操作数量 = " + count);
|
||||
|
||||
count = factorialRecur(n);
|
||||
System.out.println("阶乘阶(递归实现)的计算操作数量 = " + count);
|
||||
}
|
||||
}
|
@@ -0,0 +1,42 @@
|
||||
package chapter_computational_complexity.time_complexity;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class worst_best_time_complexity {
|
||||
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
|
||||
static int[] randomNumbers(int n) {
|
||||
Integer[] nums = new Integer[n];
|
||||
// 生成数组 nums = { 1, 2, 3, ..., n }
|
||||
for (int i = 0; i < n; i++) {
|
||||
nums[i] = i + 1;
|
||||
}
|
||||
// 随机打乱数组元素
|
||||
Collections.shuffle(Arrays.asList(nums));
|
||||
// Integer[] -> int[]
|
||||
int[] res = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
res[i] = nums[i];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
static int findOne(int[] nums) {
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
int n = 100;
|
||||
int[] nums = randomNumbers(n);
|
||||
int index = findOne(nums);
|
||||
System.out.println("\n数组 [ 1, 2, ..., n ] 被打乱后 = " + Arrays.toString(nums));
|
||||
System.out.println("数字 1 的索引为 " + index);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user