网易互娱 第一题,总是通过率为0,求大神看看代码

import java.util.Scanner;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = Integer.parseInt(in.nextLine());
ArrayList<Integer> lists = new ArrayList<Integer>();
for(int i=0;i<T;i++){
int a = toBinary(Integer.parseInt(in.nextLine()));
lists.add(a);
}
for(int i=0;i<T;i++){
if(isPalindrome(lists.get(i))){
System.out.println("YES");
}
else{
System.out.println("NO");
}
}
}
//十进制转二进制
public static int toBinary(int num){
String result = Integer.toBinaryString(num);
int numInt = Integer.parseInt(result);
return numInt;
}
//判断是否回文字
public static boolean isPalindrome(int x){
String str = String.valueOf(x);
final StringBuilder builder = new StringBuilder(str);
if(builder.reverse().toString().equals(str)){
return true;
}
return false;
}
}



在自己电脑试可以运行,在考试的环境就总是不成功。求大神指教
#笔试题目##网易互娱#
全部评论
String string = Integer.toBinaryString(a) 然后判断string回文就行~
点赞 回复 分享
发布于 2019-09-07 22:21
你转成二进制的时候转成int类型,不会溢出吗..
点赞 回复 分享
发布于 2019-09-07 22:16
下面是我的代码: import java.util.Scanner; public class Main {     public static void main(String[] args) {         Scanner in = new Scanner(System.in);         int t = in .nextInt();         int flag = 0;         int[] test = new  int[t];         while(in.hasNext()){             test[flag] = in.nextInt();             flag++;             if (flag==t)break;         }         for (int i = 0; i < t; i++){             String bo = huiwen(test[i]);             System.out.println(bo);         }     }     public static String huiwen(int in) {         String flag = "true";         StringBuilder str = change(in);         String sm = str.toString();         String st = str.reverse().toString();         for (int i = 0; i< st.length();i++){             if(sm.charAt(i) != st.charAt(i)) {                 flag = "false";                 break;             }         }         return flag;     }     public static StringBuilder change(int i) {         StringBuilder str = new StringBuilder();         while(i!=0){             str.insert(0, i % 2);             i = i/2;         }         return str;     } }
点赞 回复 分享
发布于 2019-09-07 22:12
by the way ,能私信讲一下其他笔试题吗? 我错过今天的笔试了
点赞 回复 分享
发布于 2019-09-07 22:11
  if(builder.reverse().toString().equals(str)) 这句出问题了,永真句
点赞 回复 分享
发布于 2019-09-07 22:10
点赞 回复 分享
发布于 2019-09-07 21:01
求大神帮帮小弟
点赞 回复 分享
发布于 2019-09-07 21:01

相关推荐

投递智元机器人等公司10个岗位
点赞 评论 收藏
分享
11-13 10:17
门头沟学院 Java
昨天面美团,jvm,juc问的好深啊,感觉小林coding不太够喔,牛油们有没有什么推荐的八股网站嘛🕒&nbsp;岗位/面试时间👥&nbsp;面试题目🤔&nbsp;面试感受
明天不下雨了:小林Coding:https://xiaolincoding.com/ 全栈哥:https://www.pdai.tech/ Guide哥:https://javaguide.cn/ 秀哥:https://interviewguide.cn/ 沉默王二:https://javabetter.cn/home.html 磊哥:https://www.javacn.site/interview/basic/ 小傅哥:https://bugstack.cn/ 源码哥:https://doocs.github.io/source-code-hunter/#/ 各大厂的公众号技术文章和一些经典的书籍
面试太紧张了怎么办?
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务