488-快乐数

3.21  做的第一个容易级别的题

不知道终止的条件用什么来判断比较好,就暴力的弄了个100,

应该会有更好的方法的吧。有机会一定要研究研究。

但是不管怎么说,最后达到了 A 的效果。

完成了日常的一道题。我很开心。


学到了这么几点吧:

1. char 与int 之间的转换,可以使用 s - 48,也可以使用 s - ‘0’ 的方法。

2.字符串和整数之间的转换,使用valueOf这个方法

3. 提取字符串中的某一个字符,可以使用 charAt 这个方法


并且,将字符串与整型之间的转换,整理如下:

1. 将字符串转换为整型:

a). int i = Integer.parseInt([String]);

  b). int i = Integer.valueOf(str).intValue();

  2 如何将整数 int 转换成字串 String ?

2.将整型转化为字符串

  1.) String s = String.valueOf(i);

  2.) String s = Integer.toString(i);

  3.) String s = "" + i;


public class Solution {
    /**
     * @param n an integer
     * @return true if this is a happy number or false
     */
    public boolean isHappy(int n) {
        int temp = n;
        int num = 0;
        do {
        	String s = String.valueOf(temp);
            int count = s.length();
            temp = 0;
        	for(int i = 0; i < count; i++){
        		char x = s.charAt(i);
        		int h = (int)x-48;
        		temp += (int)Math.pow(h, 2);
        	}
        	if(temp ==1){
        		return true;
        	}
        	num ++;
        	//System.out.print(num + "  ");
        	//temp = (int)s.indexOf(count-1);// Write your code here
        }while(num < 100);
        return false;
    }
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值