Why some cases can't pass? Could anyone help me out?


  • 0
    class Solution {
        public int[] searchRange(int[] nums, int target) {
            if (nums.length == 0) {
                return new int[]{-1,-1};
            }
            
            int start, end, mid;
            int []bound = new int[2];
            
            //search for the left bound
            start = 0;
            end = nums.length - 1;
            while (start + 1 < end) {
                mid = start + (end - start) / 2;
                if (mid == target) {
                    end = mid;
                }
                if (mid < target) {
                    start = mid;
                }
                if (mid > target) {
                    end = mid;
                }
            }
            
            if(nums[start] == target) {
                bound[0] = start;
            }
            else if(nums[end] == target) {
                bound[0] = end;
            }
            else {
                bound[0] = bound[1] = -1;
            }
            
            //search for the right bound
            start = 0;
            end = nums.length - 1;
            while (start + 1 < end) {
                mid = start + (end - start) / 2;
                if (mid == target) {
                    end = mid;
                }
                if (mid < target) {
                    start = mid;
                }
                if (mid > target) {
                    end = mid;
                }
            }
            
            if(nums[end] == target) {
                bound[1] = end;
            }
            else if(nums[start] == target) {
                bound[1] = start;
            }
            else {
                bound[0] = bound[1] = -1;
            }
            
            return bound;
        }
    }
    
    public class MainClass {
        public static int[] stringToIntegerArray(String input) {
            input = input.trim();
            input = input.substring(1, input.length() - 1);
            if (input.length() == 0) {
              return new int[0];
            }
        
            String[] parts = input.split(",");
            int[] output = new int[parts.length];
            for(int index = 0; index < parts.length; index++) {
                String part = parts[index].trim();
                output[index] = Integer.parseInt(part);
            }
            return output;
        }
        
        public static String integerArrayToString(int[] nums, int length) {
            if (length == 0) {
                return "[]";
            }
        
            String result = "";
            for(int index = 0; index < length; index++) {
                int number = nums[index];
                result += Integer.toString(number) + ", ";
            }
            return "[" + result.substring(0, result.length() - 2) + "]";
        }
        
        public static String integerArrayToString(int[] nums) {
            return integerArrayToString(nums, nums.length);
        }
        
        public static void main(String[] args) throws IOException {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            String line;
            while ((line = in.readLine()) != null) {
                int[] nums = stringToIntegerArray(line);
                line = in.readLine();
                int target = Integer.parseInt(line);
                
                int[] ret = new Solution().searchRange(nums, target);
                
                String out = integerArrayToString(ret);
                
                System.out.print(out);
            }
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.