My solution by java


  • 0
    S

    public void summaryRanges() {
    int[] nums = {1,3};
    List<String> strList = new ArrayList<>();
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(nums[0]);
    String left = nums[0] + "";
    String right = "";
    for (int i = 1; i < nums.length; i++) {
    if (nums[i] - nums[i - 1] == 1) {
    right = nums[i] + "";
    } else {
    if (right.equals("")) {
    strList.add(left + "");
    } else {
    strList.add(left + "->" + right);
    }
    left = nums[i] + "";
    right = "";
    }
    }
    if (right.equals("")) {
    strList.add(left + "");
    } else {
    strList.add(left + "->" + right);
    }
    System.out.println(strList);
    }


Log in to reply
 

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