若一个数的首位和末位相等,则定义这个数为“好数”。
例如:1231、4512394是好数,而12345、768740则不是好数。
请你编写一个函数,判断是不是好数。如果是好数则返回true,否则返回false。
1231
true
首位和末位都是1,相等。
4
true
首位和末位都是4,相等。
100
false
首位是1,末位是0,不相等。
class Solution: def judge(self , x ): # write code here return str(x)[0] == str(x)[-1]也可以先取余得到尾数,再不断除以10得到头数,判断是否相等
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 判断x是不是好数 * @param x int整型 待判断的数 * @return bool布尔型 */ public boolean judge (int x) { // write code here int tail = x % 10; int head = x / 10; while(head >= 10) head /= 10; return head > 0? head == tail: true; } }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 判断x是不是好数 * @param x int整型 待判断的数 * @return bool布尔型 */ bool judge(int x) { // write code here if(x==0) return true; if(x<0) return false; int last = x%10; int pre; while(x) { pre = x%10; x/=10; } return last == pre; } };
public boolean judge(int x) { // 末位 int last = x % 10; double temp = x; while (temp >= 1) { temp /= 10; } // 首位 int start = (int) (temp * 10); return start == last; } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 判断x是不是好数 * @param x int整型 待判断的数 * @return bool布尔型 */ public boolean judge (int x) { // write code here char[] c = String.valueOf(x).toCharArray(); if(c[0]==c[c.length-1])return true; return false; } }