我正在为面试练习解决问题,而我似乎无法找出以下问题的时间和空间复杂性的答案:
给定两个已排序的链接列表,按排序顺序将它们合并到第三个列表中.我们假设我们使用降序排序.
我遇到的答案之一,显然不是最有效的答案,是以下递归解决方案:
Node mergeLists(Node head1, Node head2) {
if (head1 == null) {
return head2;
} else if (head2 == null) {
return head1;
}
Node newHead = null;
if(head1.data < head2.data) {
newHead = head1;
newHead.next = mergeLists(head1.next, head2);
} else {
newHead = head2;
newHead.next = mergeLists(head1, head2.next);
}
return newHead;
}
Run Code Online (Sandbox Code Playgroud)
现在,当我分析这个功能的复杂性时,我遇到了一个问题.我不知道这是否是O(M + N)或O(M) + O(N).我只是无法得到一个直观的答案.这似乎是合乎逻辑,我认为这个功能的运行时间和空间复杂度都O(N) + O(M)还是O(max(N,M))因为其更大的价值将推动渐近曲线(或递归调用和堆栈帧作品).
总结一下:
在大哦符号中,和之间的区别是什么?O(N+M)O(N) + O(M)有没有?如果它们不同,我会很感激,如果有人可以提供两者的简单例子.
我正在一个大型的Javascript代码库中工作,目前乱七八糟的代码依赖于流量控制的异常
function getChecklistUrl() {
try {
return dataLayerObject.config.checklist;
} catch (e) {
try {
console.error('dataLayer', e);
} catch (ignore) {}
}
}
Run Code Online (Sandbox Code Playgroud)
我可能偏爱条件逻辑,例如同一函数的这种实现
function getChecklistUrl() {
if(typeof dataLayerObject == 'object' &&
'config' in dataLayerObject &&
typeof dataLayerObject.config == 'object' &&
'checklist' in dataLayerObject.config &&
typeof dataLayerObject.config.checklist == 'object') {
return dataLayerObject.config.checklist;
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
虽然后者感觉很长,但是当然可以编写辅助函数来减少这种检查的样板.
那么前者是Javascript的惯用语吗?后来是脆弱的(跨越浏览器/场景)并且更好地留给try/ catch反正?或者前者只是懒惰的证据?
编辑
这些物体被认为是"普通"物体,var obj = {}所以我不相信我在这里关心原型链.
我有一个有一些动作的余烬控制器.在这些操作中,我希望能够调用另一个函数重新组合功能,但我不想通过它this.send('someAction'),我只是想直接调用它,除了控制器调用该函数之外没有任何东西.
actions: {
my_btn_click: function(){
this.set('somthing', 'something else');
//functionA call here, not this.send('something');
}
},
//declare functionA here ? doesnt work.
Run Code Online (Sandbox Code Playgroud) 我想看看我的网站如何应对缓慢的连接.
有没有办法限制快递以慢速或延迟响应?我知道我可以setTimeout在路径上实现调用,但静态资产呢?
当我添加name属性Object.prototype和引用时Object.prototype,我收到以下错误:
TypeError: Cannot read property '0' of undefined"
Run Code Online (Sandbox Code Playgroud)
但我可以读Object.prototype.name.这个name属性是特别的Object.prototype吗?为什么会出现此错误?
该代码已在Mac OS X上的Node v6.9.5环境中执行.有谁知道如何解决这个问题?
$ node
> Object.prototype
{}
> Object.prototype.value = 'foo';
'foo'
> Object.prototype.name = 'bar';
'bar'
> Object.prototype
TypeError: Cannot read property '0' of undefined
> Object.prototype.name
'bar'
> Object.prototype.value
'foo'
> delete Object.prototype.name
true
> Object.prototype
{ value: 'foo' }
> Object.prototype.name = 'bar';
'bar'
> Object.prototype
TypeError: Cannot read property '0' of undefined
> …Run Code Online (Sandbox Code Playgroud) Is there a way to send an object to an API using axios?
This the code I use:
axios.get('/api/phones/create/', {
parameters: {
phone: this.phone
}
})
.then(response => {
console.log(response.data)
})
.catch(function (error) {
console.log(error)
})
Run Code Online (Sandbox Code Playgroud)
on the php side, I have the following:
public function create($phone)
{
return $phone;
}
Run Code Online (Sandbox Code Playgroud)
I get the following error:
GET http://crm2.dev/api/phones/create 500 (Internal Server Error)
dispatchXhrRequest @ app.6007af59798a7b58ff81.js:256
xhrAdapter @ app.6007af59798a7b58ff81.js:93
dispatchRequest @ app.6007af59798a7b58ff81.js:662
app.6007af59798a7b58ff81.js:2266 Error: Request failed with status code 500
at …Run Code Online (Sandbox Code Playgroud) 我在使用promises编写异步函数时遇到问题
function requestsPlot(plot, info) {
return new Promise(function(resolve, reject) {
var plotObject = fieldsObject[plot]
var sqr = new Promise(function(resolve1, reject) {
debugger;
get(createSQRurl(plotObject.polygon))
.then(function(result) {
plotObject.quality = sqrHtmlParsing(result);
resolve1();
});
});
var soilType = new Promise(function(resolve2, reject) {
get(createSoilTypeUrl(plotObject.polygon))
.then(function(result) {
plotObject.soilType = soilTypeHtmlParsing(result);
resolve2();
});
});
var distance = new Promise(function(resolve3, reject) {
var start = turf.centerOfMass(plotObject.polygon).geometry.coordinates;
var end = info.homeCoords;
get('http://router.project-osrm.org/route/v1/driving/' + start + ';' + end + '?overview=false')
.then(function(result) {
var parsed = JSON.parse(result);
if (parsed.code == …Run Code Online (Sandbox Code Playgroud) 我有这个对象数组,在其中我有另一个对象数组,如何得到:
[
{ id: "5a60626f1d41c80c8d3f8a85" },
{ id: "5a6062661d41c80c8b2f0413" },
{ id: "5a60626f1d41c80c8d3f8a83" },
{ id: "5a60626f1d41c80c8d3f8a84" }
];
Run Code Online (Sandbox Code Playgroud)
从:
[
{
id: 1,
country: [
{
id: "5a60626f1d41c80c8d3f8a85"
},
{
id: "5a6062661d41c80c8b2f0413"
}
]
},
{
id: 2,
country: [
{
id: "5a60626f1d41c80c8d3f8a83"
},
{
id: "5a60626f1d41c80c8d3f8a84"
}
]
}
];
Run Code Online (Sandbox Code Playgroud)
不使用a forEach和temp变量?
当我做的时候:
(data || []).map(o=>{
return o.country.map(o2=>({id: o2.id}))
})
Run Code Online (Sandbox Code Playgroud)
我得到了相同的结构.
当前情况: 我试图找到一种方法,从组件的"didInsertElement"方法中创建特定模型的记录,并将所述数据添加到数据存储.
当前情况的原因:我将事件侦听器附加到组件HBS文件中的按钮.其中一个按钮将触发两个级联但轻微的ajax调用.这些ajax调用中的最后一个响应将决定我是否应该创建记录.
我遇到的问题:当我尝试从组件中访问这样的商店时:
var myStore = this.store
Run Code Online (Sandbox Code Playgroud)
我得到一个未定义的对象.对于那些问你,我已经在我的组件中添加了以下行:
store: Ember.inject.service()
Run Code Online (Sandbox Code Playgroud)
我已经安装了Ember-Data.
解决方案:我从大量研究中发现,与商店的互动最好通过主要途径完成.但是,如何将路径中的事件侦听器附加到组件的jquery小部件?
是否有任何解决方案不需要我将所有事件监听器代码从组件移动到路由,从而撤消ember应该提供的模块化?
警告:未经处理的水域中的总菜鸟涉水.开始.在Mac终端:
package.json This is most likely not a problem with npm itself.
npm ERR! package.json npm can't find a package.json file in your current directory.
Please include the following file with any support request:
npm ERR! /Users/stickupartist/portfolio/npm-debug.log
stickup-artists-macbook-pro:portfolio stickupartist$ npm init
This utility will walk you through creating a package.json file.
Run Code Online (Sandbox Code Playgroud)
提到了什么用途?
接下来:
Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.
Name: (portfolio)
Run Code Online (Sandbox Code Playgroud)
我键入:
npm install <portfolio> --save …Run Code Online (Sandbox Code Playgroud) javascript ×7
node.js ×3
ecmascript-6 ×2
ember.js ×2
ajax ×1
arrays ×1
axios ×1
big-o ×1
ember-data ×1
es6-promise ×1
express ×1
flow-control ×1
jquery ×1
json ×1
npm ×1
npm-install ×1
promise ×1