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

There are two errors in '/javascript/16_binary/binary-find.js' #477

Open
Sunzhuoyi opened this issue Jun 10, 2020 · 0 comments
Open

There are two errors in '/javascript/16_binary/binary-find.js' #477

Sunzhuoyi opened this issue Jun 10, 2020 · 0 comments

Comments

@Sunzhuoyi
Copy link

Sunzhuoyi commented Jun 10, 2020

1

// 查找最后一个小于等于给定值的元素
const biaryFindLastSmall = (sortedArr, target) => {
    if (sortedArr.length === 0) return -1
    let low = 0
    let high = sortedArr.length - 1
    while (low <= high) {
        const mid = Math.floor((low + high) / 2)
        if (target < sortedArr[mid]) {
            high = mid - 1
        } else {
           // Error:
            if (mid === sortedArr.length - 1 || sortedArr[mid + 1] >= target) return mid
           // Right:
            if (mid === sortedArr.length - 1 || sortedArr[mid + 1] > target) return mid
            low = mid + 1
        }
    }
    return -1
}

2
binary not biary

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

No branches or pull requests

1 participant