Java method beat easy


  • 0
    S
    public static int reverse(int x) {
    	String str=x+"";
    	String[] split = str.split("");
    	boolean flag=true;
    	String[] split2 = new String[split.length];
    	for (int i = split.length-1,j=0; i >= 0; i--,j++) {
    		if (split[i].equals("-")) {
    			flag=false;
    			continue;
    		}
    		split2[j]=split[i];
    		System.out.println(split[i]);
    		
    	}
    	ArrayList<String> strings = new ArrayList<>();
    	if (flag==false) {
    		strings.add("-");
    		for (int i = 0; i < split2.length-1; i++) {
    			strings.add(split2[i]);
    		}
    		
    	}
    	if (flag==true) {
    		for (int i = 0; i < split2.length; i++) {
    			strings.add(split2[i]);
    		}
    	}
    	StringBuffer result=new StringBuffer();
    	for (int i = 0; i < strings.size(); i++) {
    		result.append(strings.get(i));
    	}
    	String string = new String(result);
    	System.out.println(string);
    	if (Long.parseLong(string)>Integer.MAX_VALUE||Long.parseLong(string)<Integer.MIN_VALUE) {
    		return 0;
    	}
    	return (int) Long.parseLong(string);
    }
    public static void main(String[] args) {
    	System.out.println(reverse(1534236469));
    }

Log in to reply
 

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