LeetCode: Search for a Range

题目描述

去尝试了以下 LeetCode,随机选了一题:Search for a Range.
题目很简单,就是在一个已排好序的整型数组里找到指定的串,时间复杂度要求 O(log n)。
比如给定一个数组 [5, 7, 7, 8, 8, 10]和指定的值 8,返回 [3, 4]
原题地址: https://leetcode.com/problems/search-for-a-range

题目解答

好久不写算法类的代码,写出来的代码又臭又长

public class Solution {
    public int[] searchRange(int[] nums, int target) {
        int length = nums.length;
        if (length == 0) {
            return new int[]{-1, -1};
        }
        int left = 0;
        int right = length - 1;
        return doSearchRange(nums, target, left, right);
    }

    private int[] doSearchRange(int[] nums, int target, int left, int right) {
        int lastLeft = left;
        int lastRight = right;
        int i = (left + right) / 2;
        while (true) {
            if (nums[i] < target) {
                left = i;
                lastLeft = i;
                i = (left + right) / 2;
                if (i == left) {
                    if (nums[right] == target) {
                        return new int[]{right, right};
                    } else {
                        return new int[]{-1, -1};
                    }
                }
            } else if (nums[i] > target) {
                right = i;
                lastRight = i;
                i = (left + right) / 2;
                if (i == right) {
                    if (nums[left] == target) {
                        return new int[]{left, left};
                    } else {
                        return new int[]{-1, -1};
                    }
                }
            } else if (nums[i] == target) {
                int leftMargin = searchLeftMargin(nums, target, lastLeft, i);
                int rightMargin = searchRightMargin(nums, target, i, lastRight);
                return new int[]{leftMargin, rightMargin};
            }
        }
    }

    private int searchRightMargin(int[] nums, int target, int left, int right) {
        if (left == right) {
            return left;
        } else {
            int i = (left + right) / 2;
            if (i == left) {
                if (nums[right] == target) {
                    return right;
                } else {
                    return left;
                }
            }
            if (nums[i] == target) {
                return searchRightMargin(nums, target, i, right);
            } else {
                return searchRightMargin(nums, target, left, i);
            }
        }
    }

    private int searchLeftMargin(int[] nums, int target, int left, int right) {
        if (left == right) {
            return right;
        } else {
            int i = (left + right) / 2;
            if (i == left) {
                if (nums[left] == target) {
                    return left;
                } else {
                    return right;
                }
            }
            if (nums[i] == target) {
                return searchLeftMargin(nums, target, left, i);
            } else {
                return searchLeftMargin(nums, target, i, right);
            }
        }
    }
}

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注