mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-10 16:00:36 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -16,9 +16,9 @@ public class space_complexity {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Constant order */
|
||||
/* Constant complexity */
|
||||
static void constant(int n) {
|
||||
// Constants, variables, objects occupy O(1) space
|
||||
// Constants, variables, and objects occupy O(1) space
|
||||
final int a = 0;
|
||||
int b = 0;
|
||||
int[] nums = new int[10000];
|
||||
@@ -27,41 +27,41 @@ public class space_complexity {
|
||||
for (int i = 0; i < n; i++) {
|
||||
int c = 0;
|
||||
}
|
||||
// Functions in the loop occupy O(1) space
|
||||
// Function calls in the loop occupy O(1) space
|
||||
for (int i = 0; i < n; i++) {
|
||||
function();
|
||||
}
|
||||
}
|
||||
|
||||
/* Linear order */
|
||||
/* Linear complexity */
|
||||
static void linear(int n) {
|
||||
// Array of length n uses O(n) space
|
||||
// An array of length n uses O(n) space
|
||||
int[] nums = new int[n];
|
||||
// A list of length n occupies O(n) space
|
||||
List<ListNode> nodes = new ArrayList<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
nodes.add(new ListNode(i));
|
||||
}
|
||||
// A hash table of length n occupies O(n) space
|
||||
// A hash table with n entries occupies O(n) space
|
||||
Map<Integer, String> map = new HashMap<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
map.put(i, String.valueOf(i));
|
||||
}
|
||||
}
|
||||
|
||||
/* Linear order (recursive implementation) */
|
||||
/* Linear complexity (recursive implementation) */
|
||||
static void linearRecur(int n) {
|
||||
System.out.println("Recursion n = " + n);
|
||||
System.out.println("Recursive call: n = " + n);
|
||||
if (n == 1)
|
||||
return;
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
|
||||
/* Exponential order */
|
||||
/* Quadratic complexity */
|
||||
static void quadratic(int n) {
|
||||
// Matrix uses O(n^2) space
|
||||
// The matrix uses O(n^2) space
|
||||
int[][] numMatrix = new int[n][n];
|
||||
// 2D list uses O(n^2) space
|
||||
// The two-dimensional list occupies O(n^2) space
|
||||
List<List<Integer>> numList = new ArrayList<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
List<Integer> tmp = new ArrayList<>();
|
||||
@@ -72,17 +72,17 @@ public class space_complexity {
|
||||
}
|
||||
}
|
||||
|
||||
/* Quadratic order (recursive implementation) */
|
||||
/* Quadratic complexity (recursive implementation) */
|
||||
static int quadraticRecur(int n) {
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
// Array nums has length n, n-1, ..., 2, 1
|
||||
// The lengths of array nums are n, n-1, ..., 2, 1
|
||||
int[] nums = new int[n];
|
||||
System.out.println("In recursion n = " + n + ", nums length = " + nums.length);
|
||||
System.out.println("Recursive call: n = " + n + " with nums length = " + nums.length);
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Exponential complexity (building a perfect binary tree) */
|
||||
static TreeNode buildTree(int n) {
|
||||
if (n == 0)
|
||||
return null;
|
||||
@@ -92,18 +92,18 @@ public class space_complexity {
|
||||
return root;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
public static void main(String[] args) {
|
||||
int n = 5;
|
||||
// Constant order
|
||||
// Constant complexity
|
||||
constant(n);
|
||||
// Linear order
|
||||
// Linear complexity
|
||||
linear(n);
|
||||
linearRecur(n);
|
||||
// Exponential order
|
||||
// Quadratic complexity
|
||||
quadratic(n);
|
||||
quadraticRecur(n);
|
||||
// Exponential order
|
||||
// Exponential complexity
|
||||
TreeNode root = buildTree(n);
|
||||
PrintUtil.printTree(root);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user