My Java solution easy understand


  • 1
    Z

    public class Solution {
    public boolean isHappy(int n) {
    if(n==1){
    return true;
    }
    if(n<=0){
    return false;
    }

    	Stack sta = new Stack<Integer>();
    	int sum=0;
    	do{
    		if(sum==0)
    		{}
    		else {
    		{n=sum;
    		 sum=0;
    		}
    		}
    	while(n>=10){
    		int x = n%10;
    		sum += (x*x);
    		n/=10;
    		
    	}
    	sum+=Math.pow(n,2);
    	
    	}while(sum >= 10);
    	if(sum ==1)
    	{return true;}
    	else
    	return false;
    	
    
    }
    

    }


  • 1
    R

    It looks unclear to me. Also, I didn't see where the stack is used. Can you elaborate on the logic?


Log in to reply
 

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