My java solution


  • 0
    C
    import java.util.*;
    public class Solution {
    public boolean isHappy(int n) {
        Hashtable<Integer,Integer> table = new Hashtable();
        while(n != 1)
        {
    	ArrayList<Integer> list = new ArrayList<Integer>();
    	int sum=0;
    	while(n != 0)
    	{
    		list.add(n%10);
    		n=n/10;			
    	}
    	
    	Iterator<Integer> iterator = list.iterator();
    	while(iterator.hasNext())
    	{	
    		int next = iterator.next();
    		sum+=next*next;
    	}
    	
    	if(sum == 1)
    		return true;
    	
    	Integer put = table.put(sum,sum);
    	if(put != null)
    	return false;
    	
    	n=sum;
        }
    	return true;
    }
    }

Log in to reply
 

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