LeetCode #704. Binary Search

Time Complexity: O(log n)

Space Complexity: O(1)

class Solution {
public int search(int[] nums, int target) {
int low = 0;
int high = nums.length -1;

while (low <= high) {
int mid = l…


This content originally appeared on DEV Community and was authored by Giuseppe

Time Complexity: O(log n)

Space Complexity: O(1)

class Solution {
    public int search(int[] nums, int target) {
        int low = 0;
        int high = nums.length -1;

        while (low <= high) {
            int mid = low + (high -low) / 2;
            if (target == nums[mid]) 
                return mid;
            else if (target < nums[mid])
                high = mid -1;
            else
                low = mid + 1;
        }
        return -1;
    } 
}


This content originally appeared on DEV Community and was authored by Giuseppe


Print Share Comment Cite Upload Translate Updates
APA

Giuseppe | Sciencx (2025-09-25T07:16:11+00:00) LeetCode #704. Binary Search. Retrieved from https://www.scien.cx/2025/09/25/leetcode-704-binary-search/

MLA
" » LeetCode #704. Binary Search." Giuseppe | Sciencx - Thursday September 25, 2025, https://www.scien.cx/2025/09/25/leetcode-704-binary-search/
HARVARD
Giuseppe | Sciencx Thursday September 25, 2025 » LeetCode #704. Binary Search., viewed ,<https://www.scien.cx/2025/09/25/leetcode-704-binary-search/>
VANCOUVER
Giuseppe | Sciencx - » LeetCode #704. Binary Search. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/09/25/leetcode-704-binary-search/
CHICAGO
" » LeetCode #704. Binary Search." Giuseppe | Sciencx - Accessed . https://www.scien.cx/2025/09/25/leetcode-704-binary-search/
IEEE
" » LeetCode #704. Binary Search." Giuseppe | Sciencx [Online]. Available: https://www.scien.cx/2025/09/25/leetcode-704-binary-search/. [Accessed: ]
rf:citation
» LeetCode #704. Binary Search | Giuseppe | Sciencx | https://www.scien.cx/2025/09/25/leetcode-704-binary-search/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.