我想计算这个嵌套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),但那也错了......
我正在创建一个对象注册(猫鼬模式),我需要Id
在User
此行中设置它:registrationId: registration._id});
但是,即使是回调函数,它Id
仍然是null
?当我检查数据库时,“注册”当然有ID,但是没有在回调中。我如何设置Id
的Registration
在User
?
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) 我正试图找到一种方法来删除第一个单词IF中的所有字符,该字符位于第二个单词中.输入看起来像这样:
电脑成本
结果应该是:"mpuer"因为c,o和t被删除了.这样的多行由返回分隔,2个单词用空格分隔.
我一直在寻找解决方案,但我真的被卡住了.所有帮助表示赞赏.
我试图用给定的数字移动数组中的所有值.例如,具有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)