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;
}
}
978

被折叠的 条评论
为什么被折叠?



