mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-25 11:13:38 +08:00
1. Add build script for Java.
2. Add height limitation for code blocks in extra.css. 3. Fix "节点" to "结点".
This commit is contained in:
@ -117,14 +117,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "Java"
|
||||
|
||||
```java title="array.java"
|
||||
/* 随机返回一个数组元素 */
|
||||
int randomAccess(int[] nums) {
|
||||
// 在区间 [0, nums.length) 中随机抽取一个数字
|
||||
int randomIndex = ThreadLocalRandom.current().
|
||||
nextInt(0, nums.length);
|
||||
int randomNum = nums[randomIndex];
|
||||
return randomNum;
|
||||
}
|
||||
[class]{array}-[func]{randomAccess}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@ -239,17 +232,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "Java"
|
||||
|
||||
```java title="array.java"
|
||||
/* 扩展数组长度 */
|
||||
int[] extend(int[] nums, int enlarge) {
|
||||
// 初始化一个扩展长度后的数组
|
||||
int[] res = new int[nums.length + enlarge];
|
||||
// 将原数组中的所有元素复制到新数组
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
res[i] = nums[i];
|
||||
}
|
||||
// 返回扩展后的新数组
|
||||
return res;
|
||||
}
|
||||
[class]{array}-[func]{extend}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@ -392,23 +375,9 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "Java"
|
||||
|
||||
```java title="array.java"
|
||||
/* 在数组的索引 index 处插入元素 num */
|
||||
void insert(int[] nums, int num, int index) {
|
||||
// 把索引 index 以及之后的所有元素向后移动一位
|
||||
for (int i = nums.length - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// 将 num 赋给 index 处元素
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* 删除索引 index 处元素 */
|
||||
void remove(int[] nums, int index) {
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
for (int i = index; i < nums.length - 1; i++) {
|
||||
nums[i] = nums[i + 1];
|
||||
}
|
||||
}
|
||||
[class]{array}-[func]{insert}
|
||||
|
||||
[class]{array}-[func]{remove}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@ -592,18 +561,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "Java"
|
||||
|
||||
```java title="array.java"
|
||||
/* 遍历数组 */
|
||||
void traverse(int[] nums) {
|
||||
int count = 0;
|
||||
// 通过索引遍历数组
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
count++;
|
||||
}
|
||||
// 直接遍历数组
|
||||
for (int num : nums) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
[class]{array}-[func]{traverse}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@ -743,14 +701,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "Java"
|
||||
|
||||
```java title="array.java"
|
||||
/* 在数组中查找指定元素 */
|
||||
int find(int[] nums, int target) {
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
if (nums[i] == target)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
[class]{array}-[func]{find}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
@ -325,22 +325,9 @@ comments: true
|
||||
=== "Java"
|
||||
|
||||
```java title="linked_list.java"
|
||||
/* 在链表的结点 n0 之后插入结点 P */
|
||||
void insert(ListNode n0, ListNode P) {
|
||||
ListNode n1 = n0.next;
|
||||
n0.next = P;
|
||||
P.next = n1;
|
||||
}
|
||||
|
||||
/* 删除链表的结点 n0 之后的首个结点 */
|
||||
void remove(ListNode n0) {
|
||||
if (n0.next == null)
|
||||
return;
|
||||
// n0 -> P -> n1
|
||||
ListNode P = n0.next;
|
||||
ListNode n1 = P.next;
|
||||
n0.next = n1;
|
||||
}
|
||||
[class]{linked_list}-[func]{insert}
|
||||
|
||||
[class]{linked_list}-[func]{remove}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@ -518,15 +505,7 @@ comments: true
|
||||
=== "Java"
|
||||
|
||||
```java title="linked_list.java"
|
||||
/* 访问链表中索引为 index 的结点 */
|
||||
ListNode access(ListNode head, int index) {
|
||||
for (int i = 0; i < index; i++) {
|
||||
if (head == null)
|
||||
return null;
|
||||
head = head.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
[class]{linked_list}-[func]{access}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@ -655,17 +634,7 @@ comments: true
|
||||
=== "Java"
|
||||
|
||||
```java title="linked_list.java"
|
||||
/* 在链表中查找值为 target 的首个结点 */
|
||||
int find(ListNode head, int target) {
|
||||
int index = 0;
|
||||
while (head != null) {
|
||||
if (head.val == target)
|
||||
return index;
|
||||
head = head.next;
|
||||
index++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
[class]{linked_list}-[func]{find}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
@ -716,92 +716,7 @@ comments: true
|
||||
=== "Java"
|
||||
|
||||
```java title="my_list.java"
|
||||
/* 列表类简易实现 */
|
||||
class MyList {
|
||||
private int[] nums; // 数组(存储列表元素)
|
||||
private int capacity = 10; // 列表容量
|
||||
private int size = 0; // 列表长度(即当前元素数量)
|
||||
private int extendRatio = 2; // 每次列表扩容的倍数
|
||||
|
||||
/* 构造函数 */
|
||||
public MyList() {
|
||||
nums = new int[capacity];
|
||||
}
|
||||
|
||||
/* 获取列表长度(即当前元素数量)*/
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/* 获取列表容量 */
|
||||
public int capacity() {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
/* 访问元素 */
|
||||
public int get(int index) {
|
||||
// 索引如果越界则抛出异常,下同
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("索引越界");
|
||||
return nums[index];
|
||||
}
|
||||
|
||||
/* 更新元素 */
|
||||
public void set(int index, int num) {
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("索引越界");
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
public void add(int num) {
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (size == capacity())
|
||||
extendCapacity();
|
||||
nums[size] = num;
|
||||
// 更新元素数量
|
||||
size++;
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
public void insert(int index, int num) {
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("索引越界");
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (size == capacity())
|
||||
extendCapacity();
|
||||
// 将索引 index 以及之后的元素都向后移动一位
|
||||
for (int j = size - 1; j >= index; j--) {
|
||||
nums[j + 1] = nums[j];
|
||||
}
|
||||
nums[index] = num;
|
||||
// 更新元素数量
|
||||
size++;
|
||||
}
|
||||
|
||||
/* 删除元素 */
|
||||
public int remove(int index) {
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("索引越界");
|
||||
int num = nums[index];
|
||||
// 将索引 index 之后的元素都向前移动一位
|
||||
for (int j = index; j < size - 1; j++) {
|
||||
nums[j] = nums[j + 1];
|
||||
}
|
||||
// 更新元素数量
|
||||
size--;
|
||||
// 返回被删除元素
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 列表扩容 */
|
||||
public void extendCapacity() {
|
||||
// 新建一个长度为 size 的数组,并将原数组拷贝到新数组
|
||||
nums = Arrays.copyOf(nums, capacity() * extendRatio);
|
||||
// 更新列表容量
|
||||
capacity = nums.length;
|
||||
}
|
||||
}
|
||||
[class]{MyList}-[func]{}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
Reference in New Issue
Block a user