Share my Java 1ms understandable solution


  • 0
    public List<String> summaryRanges(int[] nums) {
    	List<String> list = new ArrayList<String>();
    	StringBuilder sb = new StringBuilder();
    	boolean flag = false;
    	
    	for(int i=0;i<nums.length;i++){
    		if(sb.length()==0){
        		sb.append(nums[i]);	
        		flag = true;
    		}else{
    			flag = false;
    		}
    		if(i==nums.length-1 || Math.abs(nums[i+1]-nums[i])>1){
        		list.add(flag?sb.toString():sb.append("->").append(nums[i]).toString());
        		sb.setLength(0);
    		}
    	}
    	return list;
    }

Log in to reply
 

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