-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
190927+191001 #20
base: master
Are you sure you want to change the base?
190927+191001 #20
Conversation
var searchRange = function(nums, target) { | ||
let num = [-1,-1] | ||
let a = 0 | ||
if(nums.indexOf(target) === -1) return num |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这一步没必要,indexOf 实际上也是遍历查找,严重影响性能。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1
last = null, | ||
flag1 = true | ||
flag2 = true | ||
for(var i = 0,j = nums.length-1;i <nums.length,j >= 0;i++,j--) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i <nums.length,j >= 0
这个是什么意思? js有这种语法么?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果这种方法的话,这里少了一个退出条件, i <= j。可以避免一些无效比较。
flag1 = true | ||
flag2 = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
仅个人感觉..这里的flag是不是初始值用 false,找到后更新为 true 好一些..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
用true是因为false就可以跳出来了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
js的非比较影响性能
leetcode/bsy/简单/14-190929-最长公共前缀.js
Outdated
var longestCommonPrefix = function(strs) { | ||
let i = 1 | ||
let item = '' | ||
if(strs.length>0){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strs.length === 0 可以直接返回
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1
leetcode/bsy/简单/14-190929-最长公共前缀.js
Outdated
let i = 1 | ||
let item = '' | ||
if(strs.length>0){ | ||
let len = strs[0].split('').length |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
字符串可以直接取length
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1
leetcode/bsy/简单/14-190929-最长公共前缀.js
Outdated
if(strs.length === 1){ | ||
return strs[0] | ||
}else if(len === 1){ | ||
for(let j = 1; j < strlen; j++){ | ||
if (strs[j].search(strs[0]) !== 0) { | ||
return item = '' | ||
}else { | ||
item = strs[0] | ||
} | ||
} | ||
}else { | ||
for(i;i<=len;i++){ | ||
let main = strs[0].substr(0,i) | ||
for(let j = 1; j < strlen; j++){ | ||
if (strs[j].search(main) !== 0) { | ||
return item.substr(0,i-1) | ||
}else { | ||
item = main | ||
} | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
代码写的太麻烦了,就从第一个元素的第一个字母开始,遍历数组,判断是否所有的元素的第一个元素都一样,再取第二个。。。。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1
leetcode/bsy/简单/14-190929-最长公共前缀.js
Outdated
let i = 1 | ||
let item = '' | ||
if(strs.length>0){ | ||
let len = strs[0].split('').length |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
字符串可以直接取length
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1
No description provided.