-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
174 changed files
with
12,349 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/** | ||
* File: array.java | ||
* Created Time: 2022-11-25 | ||
* Author: krahets ([email protected]) | ||
*/ | ||
|
||
package chapter_array_and_linkedlist; | ||
|
||
import java.util.*; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
public class array { | ||
/* Random access to elements */ | ||
static int randomAccess(int[] nums) { | ||
// Randomly select a number in the interval [0, nums.length) | ||
int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length); | ||
// Retrieve and return a random element | ||
int randomNum = nums[randomIndex]; | ||
return randomNum; | ||
} | ||
|
||
/* Extend array length */ | ||
static int[] extend(int[] nums, int enlarge) { | ||
// Initialize an extended length array | ||
int[] res = new int[nums.length + enlarge]; | ||
// Copy all elements from the original array to the new array | ||
for (int i = 0; i < nums.length; i++) { | ||
res[i] = nums[i]; | ||
} | ||
// Return the new array after expansion | ||
return res; | ||
} | ||
|
||
/* Insert element num at `index` */ | ||
static void insert(int[] nums, int num, int index) { | ||
// Move all elements after `index` one position backward | ||
for (int i = nums.length - 1; i > index; i--) { | ||
nums[i] = nums[i - 1]; | ||
} | ||
// Assign num to the element at index | ||
nums[index] = num; | ||
} | ||
|
||
/* Remove the element at `index` */ | ||
static void remove(int[] nums, int index) { | ||
// Move all elements after `index` one position forward | ||
for (int i = index; i < nums.length - 1; i++) { | ||
nums[i] = nums[i + 1]; | ||
} | ||
} | ||
|
||
/* Traverse array */ | ||
static void traverse(int[] nums) { | ||
int count = 0; | ||
// Traverse array by index | ||
for (int i = 0; i < nums.length; i++) { | ||
count += nums[i]; | ||
} | ||
// Traverse array elements | ||
for (int num : nums) { | ||
count += num; | ||
} | ||
} | ||
|
||
/* Search for a specified element in the array */ | ||
static int find(int[] nums, int target) { | ||
for (int i = 0; i < nums.length; i++) { | ||
if (nums[i] == target) | ||
return i; | ||
} | ||
return -1; | ||
} | ||
|
||
/* Driver Code */ | ||
public static void main(String[] args) { | ||
/* Initialize an array */ | ||
int[] arr = new int[5]; | ||
System.out.println("Array arr = " + Arrays.toString(arr)); | ||
int[] nums = { 1, 3, 2, 5, 4 }; | ||
System.out.println("Array nums = " + Arrays.toString(nums)); | ||
|
||
/* Random access */ | ||
int randomNum = randomAccess(nums); | ||
System.out.println("Get a random element from nums = " + randomNum); | ||
|
||
/* Length extension */ | ||
nums = extend(nums, 3); | ||
System.out.println("Extend the array length to 8, resulting in nums = " + Arrays.toString(nums)); | ||
|
||
/* Insert element */ | ||
insert(nums, 6, 3); | ||
System.out.println("Insert the number 6 at index 3, resulting in nums = " + Arrays.toString(nums)); | ||
|
||
/* Remove element */ | ||
remove(nums, 2); | ||
System.out.println("Remove the element at index 2, resulting in nums = " + Arrays.toString(nums)); | ||
|
||
/* Traverse array */ | ||
traverse(nums); | ||
|
||
/* Search for elements */ | ||
int index = find(nums, 3); | ||
System.out.println("Find element 3 in nums, index = " + index); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
en/codes/java/chapter_array_and_linkedlist/linked_list.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
* File: linked_list.java | ||
* Created Time: 2022-11-25 | ||
* Author: krahets ([email protected]) | ||
*/ | ||
|
||
package chapter_array_and_linkedlist; | ||
|
||
import utils.*; | ||
|
||
public class linked_list { | ||
/* Insert node P after node n0 in the linked list */ | ||
static void insert(ListNode n0, ListNode P) { | ||
ListNode n1 = n0.next; | ||
P.next = n1; | ||
n0.next = P; | ||
} | ||
|
||
/* Remove the first node after node n0 in the linked list */ | ||
static void remove(ListNode n0) { | ||
if (n0.next == null) | ||
return; | ||
// n0 -> P -> n1 | ||
ListNode P = n0.next; | ||
ListNode n1 = P.next; | ||
n0.next = n1; | ||
} | ||
|
||
/* Access the node at `index` in the linked list */ | ||
static ListNode access(ListNode head, int index) { | ||
for (int i = 0; i < index; i++) { | ||
if (head == null) | ||
return null; | ||
head = head.next; | ||
} | ||
return head; | ||
} | ||
|
||
/* Search for the first node with value target in the linked list */ | ||
static int find(ListNode head, int target) { | ||
int index = 0; | ||
while (head != null) { | ||
if (head.val == target) | ||
return index; | ||
head = head.next; | ||
index++; | ||
} | ||
return -1; | ||
} | ||
|
||
/* Driver Code */ | ||
public static void main(String[] args) { | ||
/* Initialize linked list */ | ||
// Initialize each node | ||
ListNode n0 = new ListNode(1); | ||
ListNode n1 = new ListNode(3); | ||
ListNode n2 = new ListNode(2); | ||
ListNode n3 = new ListNode(5); | ||
ListNode n4 = new ListNode(4); | ||
// Build references between nodes | ||
n0.next = n1; | ||
n1.next = n2; | ||
n2.next = n3; | ||
n3.next = n4; | ||
System.out.println("The initialized linked list is"); | ||
PrintUtil.printLinkedList(n0); | ||
|
||
/* Insert node */ | ||
insert(n0, new ListNode(0)); | ||
System.out.println("Linked list after inserting the node is"); | ||
PrintUtil.printLinkedList(n0); | ||
|
||
/* Remove node */ | ||
remove(n0); | ||
System.out.println("Linked list after removing the node is"); | ||
PrintUtil.printLinkedList(n0); | ||
|
||
/* Access node */ | ||
ListNode node = access(n0, 3); | ||
System.out.println("The value of the node at index 3 in the linked list = " + node.val); | ||
|
||
/* Search node */ | ||
int index = find(n0, 2); | ||
System.out.println("The index of the node with value 2 in the linked list = " + index); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* File: list.java | ||
* Created Time: 2022-11-25 | ||
* Author: krahets ([email protected]) | ||
*/ | ||
|
||
package chapter_array_and_linkedlist; | ||
|
||
import java.util.*; | ||
|
||
public class list { | ||
public static void main(String[] args) { | ||
/* Initialize list */ | ||
// The array's element type is Integer[], a wrapper class for int | ||
Integer[] numbers = new Integer[] { 1, 3, 2, 5, 4 }; | ||
List<Integer> nums = new ArrayList<>(Arrays.asList(numbers)); | ||
System.out.println("List nums = " + nums); | ||
|
||
/* Access element */ | ||
int num = nums.get(1); | ||
System.out.println("Access the element at index 1, obtained num = " + num); | ||
|
||
/* Update element */ | ||
nums.set(1, 0); | ||
System.out.println("Update the element at index 1 to 0, resulting in nums = " + nums); | ||
|
||
/* Clear list */ | ||
nums.clear(); | ||
System.out.println("After clearing the list, nums = " + nums); | ||
|
||
/* Add element at the end */ | ||
nums.add(1); | ||
nums.add(3); | ||
nums.add(2); | ||
nums.add(5); | ||
nums.add(4); | ||
System.out.println("After adding elements, nums = " + nums); | ||
|
||
/* Insert element in the middle */ | ||
nums.add(3, 6); | ||
System.out.println("Insert the number 6 at index 3, resulting in nums = " + nums); | ||
|
||
/* Remove element */ | ||
nums.remove(3); | ||
System.out.println("Remove the element at index 3, resulting in nums = " + nums); | ||
|
||
/* Traverse the list by index */ | ||
int count = 0; | ||
for (int i = 0; i < nums.size(); i++) { | ||
count += nums.get(i); | ||
} | ||
/* Traverse the list elements */ | ||
for (int x : nums) { | ||
count += x; | ||
} | ||
|
||
/* Concatenate two lists */ | ||
List<Integer> nums1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 })); | ||
nums.addAll(nums1); | ||
System.out.println("Concatenate list nums1 to nums, resulting in nums = " + nums); | ||
|
||
/* Sort list */ | ||
Collections.sort(nums); | ||
System.out.println("After sorting the list, nums = " + nums); | ||
} | ||
} |
Oops, something went wrong.