mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-02 10:34:35 +00:00
translation: Add Python and Java code for EN version (#1345)
* Add the intial translation of code of all the languages * test * revert * Remove * Add Python and Java code for EN version
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
/**
|
||||
* File: array.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* File: linked_list.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* File: list.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
/**
|
||||
* File: my_list.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_array_and_linkedlist;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* List class */
|
||||
class MyList {
|
||||
private int[] arr; // Array (stores list elements)
|
||||
private int capacity = 10; // List capacity
|
||||
private int size = 0; // List length (current number of elements)
|
||||
private int extendRatio = 2; // Multiple for each list expansion
|
||||
|
||||
/* Constructor */
|
||||
public MyList() {
|
||||
arr = new int[capacity];
|
||||
}
|
||||
|
||||
/* Get list length (current number of elements) */
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/* Get list capacity */
|
||||
public int capacity() {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
/* Access element */
|
||||
public int get(int index) {
|
||||
// If the index is out of bounds, throw an exception, as below
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("Index out of bounds");
|
||||
return arr[index];
|
||||
}
|
||||
|
||||
/* Update element */
|
||||
public void set(int index, int num) {
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("Index out of bounds");
|
||||
arr[index] = num;
|
||||
}
|
||||
|
||||
/* Add element at the end */
|
||||
public void add(int num) {
|
||||
// When the number of elements exceeds capacity, trigger the expansion mechanism
|
||||
if (size == capacity())
|
||||
extendCapacity();
|
||||
arr[size] = num;
|
||||
// Update the number of elements
|
||||
size++;
|
||||
}
|
||||
|
||||
/* Insert element in the middle */
|
||||
public void insert(int index, int num) {
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("Index out of bounds");
|
||||
// When the number of elements exceeds capacity, trigger the expansion mechanism
|
||||
if (size == capacity())
|
||||
extendCapacity();
|
||||
// Move all elements after `index` one position backward
|
||||
for (int j = size - 1; j >= index; j--) {
|
||||
arr[j + 1] = arr[j];
|
||||
}
|
||||
arr[index] = num;
|
||||
// Update the number of elements
|
||||
size++;
|
||||
}
|
||||
|
||||
/* Remove element */
|
||||
public int remove(int index) {
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("Index out of bounds");
|
||||
int num = arr[index];
|
||||
// Move all elements after `index` one position forward
|
||||
for (int j = index; j < size - 1; j++) {
|
||||
arr[j] = arr[j + 1];
|
||||
}
|
||||
// Update the number of elements
|
||||
size--;
|
||||
// Return the removed element
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Extend list */
|
||||
public void extendCapacity() {
|
||||
// Create a new array with a length multiple of the original array by extendRatio, and copy the original array to the new array
|
||||
arr = Arrays.copyOf(arr, capacity() * extendRatio);
|
||||
// Update list capacity
|
||||
capacity = arr.length;
|
||||
}
|
||||
|
||||
/* Convert the list to an array */
|
||||
public int[] toArray() {
|
||||
int size = size();
|
||||
// Only convert elements within valid length range
|
||||
int[] arr = new int[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
arr[i] = get(i);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
|
||||
public class my_list {
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
/* Initialize list */
|
||||
MyList nums = new MyList();
|
||||
/* Add element at the end */
|
||||
nums.add(1);
|
||||
nums.add(3);
|
||||
nums.add(2);
|
||||
nums.add(5);
|
||||
nums.add(4);
|
||||
System.out.println("List nums = " + Arrays.toString(nums.toArray()) +
|
||||
", capacity = " + nums.capacity() + ", length = " + nums.size());
|
||||
|
||||
/* Insert element in the middle */
|
||||
nums.insert(3, 6);
|
||||
System.out.println("Insert the number 6 at index 3, resulting in nums = " + Arrays.toString(nums.toArray()));
|
||||
|
||||
/* Remove element */
|
||||
nums.remove(3);
|
||||
System.out.println("Remove the element at index 3, resulting in nums = " + Arrays.toString(nums.toArray()));
|
||||
|
||||
/* 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 = " + Arrays.toString(nums.toArray()));
|
||||
|
||||
/* Test expansion mechanism */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
// At i = 5, the list length will exceed the list capacity, triggering the expansion mechanism at this time
|
||||
nums.add(i);
|
||||
}
|
||||
System.out.println("After extending, list nums = " + Arrays.toString(nums.toArray()) +
|
||||
", capacity = " + nums.capacity() + ", length = " + nums.size());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user