Skip to content

Latest commit

 

History

History
50 lines (30 loc) · 989 Bytes

228 Summary Ranges c68e55b9e43744e3aba59d361b093367.md

File metadata and controls

50 lines (30 loc) · 989 Bytes

228. Summary Ranges

Summary Ranges - LeetCode

package Exception;

import java.util.ArrayList;
import java.util.List;

public class main {

    public static List<String> summaryRanges(int[] nums) {
        List<String> stringList = new ArrayList<>();
        int fromIndex = 0;
        int toIndex = nums.length;

        for (int i = 0; i <= nums.length - 1; i++) {
            fromIndex = i;

            while (i != nums.length - 1 && nums[i] + 1 == nums[i + 1]) {
                i++;
            }

            toIndex = i;

            if (fromIndex == toIndex) {
                stringList.add("" + nums[fromIndex]);

            } else {
                stringList.add(nums[fromIndex] + "->" + nums[toIndex]);

            }

        }

        return stringList;

    }

    public static void main(String[] args) {

        System.out.println(summaryRanges(new int[]{0, 1, 2, 4, 5, 7}));

    }

   

}