Polish the chapter of array and linkedlist

This commit is contained in:
krahets
2023-08-17 05:13:19 +08:00
parent 0858ab91c0
commit c310edb672
26 changed files with 287 additions and 236 deletions

View File

@ -6,7 +6,7 @@
#include "../utils/common.h"
/* 随机返回一个数组元素 */
/* 随机访问元素 */
int randomAccess(int *nums, int size) {
// 在区间 [0, size) 中随机抽取一个数字
int randomIndex = rand() % size;

View File

@ -14,7 +14,7 @@ extern "C" {
/* 链表节点结构体 */
struct ListNode {
int val; // 节点值
struct ListNode *next; // 指向下一节点的指针(引用
struct ListNode *next; // 指向下一节点的引用
};
// typedef 作用是为一种数据类型定义一个新名字

View File

@ -6,7 +6,7 @@
#include "../utils/common.hpp"
/* 随机返回一个数组元素 */
/* 随机访问元素 */
int randomAccess(int *nums, int size) {
// 在区间 [0, size) 中随机抽取一个数字
int randomIndex = rand() % size;

View File

@ -5,7 +5,7 @@
namespace hello_algo.chapter_array_and_linkedlist;
public class array {
/* 随机返回一个数组元素 */
/* 随机访问元素 */
public static int randomAccess(int[] nums) {
Random random = new();
// 在区间 [0, nums.Length) 中随机抽取一个数字

View File

@ -9,8 +9,8 @@ namespace hello_algo.chapter_stack_and_queue;
/* 双向链表节点 */
public class ListNode {
public int val; // 节点值
public ListNode? next; // 后继节点引用(指针)
public ListNode? prev; // 前驱节点引用(指针)
public ListNode? next; // 后继节点引用
public ListNode? prev; // 前驱节点引用
public ListNode(int val) {
this.val = val;

View File

@ -6,7 +6,7 @@
import 'dart:math';
/* 随机返回一个数组元素 */
/* 随机访问元素 */
int randomAccess(List nums) {
// 在区间 [0, nums.length) 中随机抽取一个数字
int randomIndex = Random().nextInt(nums.length);

View File

@ -7,8 +7,8 @@
/* 双向链表节点 */
class ListNode {
int val; // 节点值
ListNode? next; // 后继节点引用(指针)
ListNode? prev; // 前驱节点引用(指针)
ListNode? next; // 后继节点引用
ListNode? prev; // 前驱节点引用
ListNode(this.val, {this.next, this.prev});
}

View File

@ -8,7 +8,7 @@ import (
"math/rand"
)
/* 随机返回一个数组元素 */
/* 随机访问元素 */
func randomAccess(nums []int) (randomNum int) {
// 在区间 [0, nums.length) 中随机抽取一个数字
randomIndex := rand.Intn(len(nums))

View File

@ -10,7 +10,7 @@ import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
public class array {
/* 随机返回一个数组元素 */
/* 随机访问元素 */
static int randomAccess(int[] nums) {
// 在区间 [0, nums.length) 中随机抽取一个数字
int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);

View File

@ -11,8 +11,8 @@ import java.util.*;
/* 双向链表节点 */
class ListNode {
int val; // 节点值
ListNode next; // 后继节点引用(指针)
ListNode prev; // 前驱节点引用(指针)
ListNode next; // 后继节点引用
ListNode prev; // 前驱节点引用
ListNode(int val) {
this.val = val;

View File

@ -4,7 +4,7 @@
* Author: IsChristina (christinaxia77@foxmail.com)
*/
/* 随机返回一个数组元素 */
/* 随机访问元素 */
function randomAccess(nums) {
// 在区间 [0, nums.length) 中随机抽取一个数字
const random_index = Math.floor(Math.random() * nums.length);

View File

@ -11,8 +11,8 @@ class ListNode:
def __init__(self, val: int):
"""构造方法"""
self.val: int = val
self.next: ListNode | None = None # 后继节点引用(指针)
self.prev: ListNode | None = None # 前驱节点引用(指针)
self.next: ListNode | None = None # 后继节点引用
self.prev: ListNode | None = None # 前驱节点引用
class LinkedListDeque:

View File

@ -8,7 +8,7 @@ include!("../include/include.rs");
use rand::Rng;
/* 随机返回一个数组元素 */
/* 随机访问元素 */
fn random_access(nums: &[i32]) -> i32 {
// 在区间 [0, nums.len()) 中随机抽取一个数字
let random_index = rand::thread_rng().gen_range(0..nums.len());

View File

@ -12,8 +12,8 @@ use std::cell::RefCell;
/* 双向链表节点 */
pub struct ListNode<T> {
pub val: T, // 节点值
pub next: Option<Rc<RefCell<ListNode<T>>>>, // 后继节点引用(指针
pub prev: Option<Rc<RefCell<ListNode<T>>>>, // 前驱节点引用(指针
pub next: Option<Rc<RefCell<ListNode<T>>>>, // 后继节点指针
pub prev: Option<Rc<RefCell<ListNode<T>>>>, // 前驱节点指针
}
impl<T> ListNode<T> {

View File

@ -4,7 +4,7 @@
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
/* 访 */
func randomAccess(nums: [Int]) -> Int {
// [0, nums.count)
let randomIndex = nums.indices.randomElement()!

View File

@ -7,8 +7,8 @@
/* */
class ListNode {
var val: Int //
var next: ListNode? //
weak var prev: ListNode? //
var next: ListNode? //
weak var prev: ListNode? //
init(val: Int) {
self.val = val

View File

@ -4,7 +4,7 @@
* Author: Justin (xiefahit@gmail.com)
*/
/* 随机返回一个数组元素 */
/* 随机访问元素 */
function randomAccess(nums: number[]): number {
// 在区间 [0, nums.length) 中随机抽取一个数字
const random_index = Math.floor(Math.random() * nums.length);

View File

@ -5,7 +5,7 @@
const std = @import("std");
const inc = @import("include");
// 随机返回一个数组元素
// 随机访问元素
pub fn randomAccess(nums: []i32) i32 {
// 在区间 [0, nums.len) 中随机抽取一个整数
var randomIndex = std.crypto.random.intRangeLessThan(usize, 0, nums.len);

View File

@ -11,8 +11,8 @@ pub fn ListNode(comptime T: type) type {
const Self = @This();
val: T = undefined, // 节点值
next: ?*Self = null, // 后继节点引用(指针
prev: ?*Self = null, // 前驱节点引用(指针
next: ?*Self = null, // 后继节点指针
prev: ?*Self = null, // 前驱节点指针
// Initialize a list node with specific value
pub fn init(self: *Self, x: i32) void {