mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-06 22:34:18 +08:00
Format the Java codes with the Reat Hat extension.
This commit is contained in:
@ -8,7 +8,6 @@ package chapter_computational_complexity;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
||||
public class leetcode_two_sum {
|
||||
/* 方法一:暴力枚举 */
|
||||
static int[] twoSumBruteForce(int[] nums, int target) {
|
||||
@ -40,9 +39,9 @@ public class leetcode_two_sum {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] nums = { 2,7,11,15 };
|
||||
int[] nums = { 2, 7, 11, 15 };
|
||||
int target = 9;
|
||||
|
||||
|
||||
// ====== Driver Code ======
|
||||
// 方法一
|
||||
int[] res = twoSumBruteForce(nums, target);
|
||||
|
@ -15,7 +15,7 @@ public class space_complexity {
|
||||
// do something
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* 常数阶 */
|
||||
static void constant(int n) {
|
||||
// 常量、变量、对象占用 O(1) 空间
|
||||
@ -52,7 +52,8 @@ public class space_complexity {
|
||||
/* 线性阶(递归实现) */
|
||||
static void linearRecur(int n) {
|
||||
System.out.println("递归 n = " + n);
|
||||
if (n == 1) return;
|
||||
if (n == 1)
|
||||
return;
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
|
||||
@ -73,7 +74,8 @@ public class space_complexity {
|
||||
|
||||
/* 平方阶(递归实现) */
|
||||
static int quadraticRecur(int n) {
|
||||
if (n <= 0) return 0;
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
// 数组 nums 长度为 n, n-1, ..., 2, 1
|
||||
int[] nums = new int[n];
|
||||
System.out.println("递归 n = " + n + " 中的 nums 长度 = " + nums.length);
|
||||
@ -82,7 +84,8 @@ public class space_complexity {
|
||||
|
||||
/* 指数阶(建立满二叉树) */
|
||||
static TreeNode buildTree(int n) {
|
||||
if (n == 0) return null;
|
||||
if (n == 0)
|
||||
return null;
|
||||
TreeNode root = new TreeNode(0);
|
||||
root.left = buildTree(n - 1);
|
||||
root.right = buildTree(n - 1);
|
||||
|
@ -23,7 +23,7 @@ public class time_complexity {
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
/* 线性阶(遍历数组) */
|
||||
static int arrayTraversal(int[] nums) {
|
||||
int count = 0;
|
||||
@ -48,7 +48,7 @@ public class time_complexity {
|
||||
|
||||
/* 平方阶(冒泡排序) */
|
||||
static int bubbleSort(int[] nums) {
|
||||
int count = 0; // 计数器
|
||||
int count = 0; // 计数器
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for (int i = nums.length - 1; i > 0; i--) {
|
||||
// 内循环:冒泡操作
|
||||
@ -58,7 +58,7 @@ public class time_complexity {
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
count += 3; // 元素交换包含 3 个单元操作
|
||||
count += 3; // 元素交换包含 3 个单元操作
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -81,7 +81,8 @@ public class time_complexity {
|
||||
|
||||
/* 指数阶(递归实现) */
|
||||
static int expRecur(int n) {
|
||||
if (n == 1) return 1;
|
||||
if (n == 1)
|
||||
return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
|
||||
@ -97,15 +98,17 @@ public class time_complexity {
|
||||
|
||||
/* 对数阶(递归实现) */
|
||||
static int logRecur(float n) {
|
||||
if (n <= 1) return 0;
|
||||
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);
|
||||
if (n <= 1)
|
||||
return 1;
|
||||
int count = linearLogRecur(n / 2) +
|
||||
linearLogRecur(n / 2);
|
||||
for (int i = 0; i < n; i++) {
|
||||
count++;
|
||||
}
|
||||
@ -114,7 +117,8 @@ public class time_complexity {
|
||||
|
||||
/* 阶乘阶(递归实现) */
|
||||
static int factorialRecur(int n) {
|
||||
if (n == 0) return 1;
|
||||
if (n == 0)
|
||||
return 1;
|
||||
int count = 0;
|
||||
// 从 1 个分裂出 n 个
|
||||
for (int i = 0; i < n; i++) {
|
||||
@ -141,7 +145,7 @@ public class time_complexity {
|
||||
System.out.println("平方阶的计算操作数量 = " + count);
|
||||
int[] nums = new int[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
nums[i] = n - i; // [n,n-1,...,2,1]
|
||||
nums[i] = n - i; // [n,n-1,...,2,1]
|
||||
count = bubbleSort(nums);
|
||||
System.out.println("平方阶(冒泡排序)的计算操作数量 = " + count);
|
||||
|
||||
|
@ -36,7 +36,7 @@ public class worst_best_time_complexity {
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
|
Reference in New Issue
Block a user