小编Sab*_*ber的帖子

使用HashMap.computeIfAbsent计算Fibonacci序列时,计算无法完成

我正在尝试使用computeIfAbsent()类中的函数在Fibonacci序列中获得第1到第20个项HashMap.这是代码:

@Test
public void getFibMap() {
    Map<Integer, Long> map = new HashMap<>();
    map.put(1, 1L);
    map.put(2, 1L);
    fib(20, map);
    map.forEach((x, y) -> System.out.printf("key : %d --> value : %d\n", x, 
        y.));
}

public Long fib(Integer n, Map<Integer, Long> map) {
    return map.computeIfAbsent(n, v -> fib(n - 1, map) + fib(n - 2, map));
}
Run Code Online (Sandbox Code Playgroud)

但是当程序结束时,两个数字19和20都会丢失,这些数字应该被计算出来.这是控制台:

key : 1 --> value : 1
key : 2 --> value : 1
key : 3 --> value : 2
key …
Run Code Online (Sandbox Code Playgroud)

java hashmap java-8

9
推荐指数
0
解决办法
180
查看次数

为什么 Rust 中实例方法可以像静态方法一样被调用?

我学习了两种在 Rust 中调用方法的方法。

第一种方式称为静态方法,它不需要实例,可以直接使用结构体调用。

第二种方式称为实例方法,它需要一个对象实例来调用它。

两种方法的区别在于第一个参数是否为self

例子:

struct User {
    name: String,
}


impl User {
    //static method
    fn new(name: String) -> Self {
        User { name }
    }
    //instance method
    fn get_name(&self) -> String {
        self.name
    }
}
Run Code Online (Sandbox Code Playgroud)

但我发现可以这样调用一些方法:

use chrono::Local;
use chrono::TimeZone;
fn main() {
    let time = Local.with_ymd_and_hms(2024, 1, 26, 15, 0, 0).unwrap();
    println!("{}", time.format("%F %T"));
}
Run Code Online (Sandbox Code Playgroud)

我不明白。with_ymd_and_hms定义TimeZone如下:

    fn with_ymd_and_hms(
        &self,
        year: i32,
        month: u32,
        day: u32, …
Run Code Online (Sandbox Code Playgroud)

rust

4
推荐指数
1
解决办法
121
查看次数

如何在函数中返回自定义字符串引用?

我想实现DeRef自定义结构的特征,并返回一个&String. 这是代码块

use std::ops;
use std::path::Path;

fn main() {
    println! ("hello world");
}

struct MyDir {
    path: String,
}

impl ops::Deref for MyDir {
    type Target = String;

    fn deref(&self) -> &String {
        let txt = format! ("Dir[path = {}]", self.path);
        &txt
    }
}
Run Code Online (Sandbox Code Playgroud)

编译器诊断:无法返回对局部变量“txt”的引用。返回对当前函数拥有的数据的引用

我查了stackoverflow上的一些答案,比如(Is there any way to return a reference to a variable created in a function?),他们都说你不能返回对函数拥有的变量的引用,但是然后我看到了一个代码片段std::path::Path

[stable(feature = "rust1", since = "1.0.0")]
impl ops::Deref for PathBuf { …
Run Code Online (Sandbox Code Playgroud)

rust

2
推荐指数
1
解决办法
59
查看次数

为什么此哈希表中的元素未按预期排序

我最近开始学习数据结构.我写了一个哈希表根据这本书,使用二次探测法.这是代码:

class QuadraticProbingHashTable<E> implements HashTable<E> {
    private static final int DEFAULT_TABLE_SIZE = 11;

    private HashEntry<E>[] array;

    private int currentSize;

    public QuadraticProbingHashTable() {
        this(DEFAULT_TABLE_SIZE);
    }

    public QuadraticProbingHashTable(int size) {
        allocateArray(size);
        makeEmpty();
    }

    @SuppressWarnings("unchecked")
    private void allocateArray(int size) {
        array = new HashEntry[nextPrime(size)];
    }

    @Override
    public int size() {
        return currentSize;
    }

    @Override
    public boolean isEmpty() {
        return currentSize == 0;
    }

    @Override
    public void clear() {
        makeEmpty();
    }

    private void makeEmpty() {
        currentSize = 0;
        for (int i = 0; i …
Run Code Online (Sandbox Code Playgroud)

java hashtable data-structures

0
推荐指数
1
解决办法
73
查看次数

这是一次递归吗?

我与朋友争吵,因为我不认为fib_2()是递归,但他说这是因为它自称.
我不认为这是因为fib_2()没有一个返回结果用作另一个的参数fib_2().我认为fib_2()是相同的fib_3(),它是一个迭代,而不是递归.

那么它是递归还是不是?

public class Demo {
    public static void main(String[] args) {
        System.out.printf("fib_1 -> %d\n", fib_1(10));
        System.out.printf("fib_2 -> %d\n", fff(10));
        System.out.printf("fib_3 -> %d\n", fib_3(10));
    }

    //This is recursion
    public static int fib_1(int n) {
        if (n == 1 || n == 2)
            return 1;
        return fib_1(n - 1) + fib_1(n - 2);
    }

    //Is this recursion or not ?
    public static int fff(int n) {
        int a = 1, …
Run Code Online (Sandbox Code Playgroud)

java iteration recursion

-4
推荐指数
1
解决办法
85
查看次数