Skip to content
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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

Conversation

xinhendedamao1
Copy link
Collaborator

No description provided.

var searchRange = function(nums, target) {
let num = [-1,-1]
let a = 0
if(nums.indexOf(target) === -1) return num
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这一步没必要,indexOf 实际上也是遍历查找,严重影响性能。

Copy link
Collaborator Author

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--) {
Copy link
Owner

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有这种语法么?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果这种方法的话,这里少了一个退出条件, i <= j。可以避免一些无效比较。

Comment on lines +47 to +48
flag1 = true
flag2 = true
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

仅个人感觉..这里的flag是不是初始值用 false,找到后更新为 true 好一些..

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用true是因为false就可以跳出来了

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

js的非比较影响性能

var longestCommonPrefix = function(strs) {
let i = 1
let item = ''
if(strs.length>0){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strs.length === 0 可以直接返回

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1

let i = 1
let item = ''
if(strs.length>0){
let len = strs[0].split('').length
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

字符串可以直接取length

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1

Comment on lines +29 to +51
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
}
}
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码写的太麻烦了,就从第一个元素的第一个字母开始,遍历数组,判断是否所有的元素的第一个元素都一样,再取第二个。。。。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1

let i = 1
let item = ''
if(strs.length>0){
let len = strs[0].split('').length
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

字符串可以直接取length

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants