小编Aar*_*ron的帖子

三个嵌套for循环的渐近分析

我想计算这个嵌套for循环的θ复杂度:

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < i; j++) {
            for (int k = 0; k < j; k++) {
                // statement
Run Code Online (Sandbox Code Playgroud)

我会说它是n ^ 3,但我不认为这是正确的,因为每个for循环不会从1变为n.我做了一些测试:

n = 5 - > 10

10 - > 120

30 - > 4060

50 - > 19600

所以它必须在n ^ 2和n ^ 3之间.我尝试了总和公式等,但我的结果太高了.虽然n ^ 2 log(n),但那也错了......

complexity-theory big-theta asymptotic-complexity

6
推荐指数
1
解决办法
1177
查看次数

猫鼬对象ID为null

我正在创建一个对象注册(猫鼬模式),我需要IdUser此行中设置它:registrationId: registration._id});

但是,即使是回调函数,它Id仍然是null?当我检查数据库时,“注册”当然有ID,但是没有在回调中。我如何设置IdRegistrationUser

Edit2:更改为最小示例。打印两次null

exports.create = function(req, res) {
  Registration.create(req.body, function(err, registration) {
    if(err) { return handleError(res, err); }

    console.log(registration._id);
    console.log(registration.id);

    return res.json(201, registration);
  });
};
Run Code Online (Sandbox Code Playgroud)

编辑:这是架构(我省略了一些不需要的字段):

'use strict';

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var RegistrationSchema = new Schema({

    isReservation: Boolean,

    //Id of the trip
    tripId: String,

    //socialMutuality
    codeGerechtige: String,
    socialMutualityNumberParent1: String,
    socialMutualityNumberParent2: String,

    //contact
    userId: String,
    firstnameContact: String,
    lastnameContact: String, …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js express

5
推荐指数
2
解决办法
3307
查看次数

如果它们也在下一个单词(sed)中,如何从单词中删除字符?

我正试图找到一种方法来删除第一个单词IF中的所有字符,该字符位于第二个单词中.输入看起来像这样:

电脑成本

结果应该是:"mpuer"因为c,o和t被删除了.这样的多行由返回分隔,2个单词用空格分隔.

我一直在寻找解决方案,但我真的被卡住了.所有帮助表示赞赏.

linux sed

3
推荐指数
1
解决办法
948
查看次数

数组引用不正确?JUnit工作不正常?

我试图用给定的数字移动数组中的所有值.例如,具有1个移位的阵列{1,2,3,4,5}必须变为{5,1,2,3,4}.这是使用JUnit测试的.

JUnit测试是这样的:

    @Test    
    public void shift1(){
        double[] row = {1.0,2.0,3.0,4.0,5.0};
        int amount= 1;
        ArrayOperations.shift(row, amount);
        Assert.assertEquals(5.0, row [0]);
        Assert.assertEquals(1.0, row [1]);
        Assert.assertEquals(2.0, row [2]);
        Assert.assertEquals(3.0, row [3]);
        Assert.assertEquals(4.0, row [4]);      
    }
Run Code Online (Sandbox Code Playgroud)

我的方法是这样的:

public static void shift(double[] row, int amount) {
    double[] newRow= new double[row.length];
    for (int i = 0; i < newRow.length; i++) {
        newRow[(i + amount) % row.length] = row[i];
    }

    row= newRow;
}
Run Code Online (Sandbox Code Playgroud)

现在这个测试因任何未知原因而失败.我在编程课上遇到了这个问题,甚至我的老师也没找到原因.当我调试它时,数组被正确修改,导致{5,1,2,3,4}.但是JUnit失败了...但是这段代码有效:

public static void shift(double[] row, int amount) { …
Run Code Online (Sandbox Code Playgroud)

java arrays junit pass-by-reference pass-by-value

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