Java 创建回文数字列表

Java 创建回文数字列表,java,algorithm,palindrome,Java,Algorithm,Palindrome,我正在尝试创建一个回文数字的列表,它由两个3位数字的乘积组成,但返回[]。我做错了什么 编辑:最初我无法添加isPalindrome函数,因为stackoverflow会抱怨“我的帖子代码”我的isPalindrome函数有什么问题吗 public class Solution { //list of digit numbers of a number ArrayList<Long> digits = new ArrayList<>(); //list of p

我正在尝试创建一个回文数字的
列表
,它由两个3位数字的乘积组成,但返回[]。我做错了什么


编辑:最初我无法添加isPalindrome函数,因为stackoverflow会抱怨“我的帖子代码”我的isPalindrome函数有什么问题吗

public class Solution {




//list of digit numbers of a number
ArrayList<Long> digits = new ArrayList<>();

//list of palindrome numbers
public ArrayList<Long> pal = new ArrayList<>();


// checks if the given number is a palindrome
boolean isPalindrome(long num) {


// creates list of digit numbers of a number
// ex. 12345 -> [5,4,3,2,1]
while(num > 0) {
  long lastdigit = num % 10;
  digits.add(lastdigit);
  num = num / 10;
}

//checks if the number is a palindrome by checking the first and last index

// when the number of digits is even
if(digits.size() % 2 == 0) {
  while(digits.size() > 0) {
    int last = digits.size() - 1;

    if (digits.get(0) == digits.get(last)) {
      digits.remove(last);
      digits.remove(0);
    }
    else {
      return false;
      }

    }
return true;

  }

// when the number of digits is odd
else  while(digits.size() > 1) {

  int last = digits.size() - 1;
  if (digits.get(0) == digits.get(last)) {
    digits.remove(last);
    digits.remove(0);
  }
  else {
    return false;
    }

  }

  return true;

}


    ArrayList<Long> findPal() {

        for (long i = 100; i <= 999; i++) {
            for (long j = 100; j <= 999; j++) {
                Long product = i * j;

                if (isPalindrome(product)) {
                    pal.add(product);
                }
            }
        }
        return pal;
    }
    public static void main(String[] args) {
        Solution sol = new Solution();
        System.out.println(sol.isPalindrome((long)121)); //true
        System.out.println(sol.isPalindrome((long)12345)); // false
        System.out.println(sol.findPal()); //[]
    }
}
公共类解决方案{
//数字的数字列表
ArrayList位数=新的ArrayList();
//回文数列表
public ArrayList pal=new ArrayList();
//检查给定的数字是否为回文
布尔值isAlindrome(长数值){
//创建数字的数字列表
//例12345->[5,4,3,2,1]
while(num>0){
长lastdigit=num%10;
数字。添加(最后一个数字);
num=num/10;
}
//通过检查第一个和最后一个索引来检查数字是否为回文
//当位数为偶数时
if(digits.size()%2==0){
while(digits.size()>0){
int last=digits.size()-1;
if(digits.get(0)=digits.get(last)){
数字。删除(最后一个);
数字。删除(0);
}
否则{
返回false;
}
}
返回true;
}
//当位数为奇数时
else while(digits.size()>1){
int last=digits.size()-1;
if(digits.get(0)=digits.get(last)){
数字。删除(最后一个);
数字。删除(0);
}
否则{
返回false;
}
}
返回true;
}
ArrayList findPal(){

对于(long i=100;i问题最有可能出现在
isPalindrome
方法中,您尚未向我们展示该方法

还有几个其他问题:

  • pal
    字段实际上应该是
    findPal
    方法中的一个局部变量。(提示:如果调用
    findPal()
    两次会发生什么情况?一旦在
    isAlindrome
    中发现错误,请尝试……)

  • 您创建的回文数字列表很可能包含重复的数字。例如,如果104 x 521是回文数字,则521 x 104也是

  • 编辑:

    在isPalindrome()函数中

    在Else块中,您需要清空数字列表。这是您没有清空它的主要问题

    我只是使用你的代码,刚刚创建了我的isAlindrome函数,一切正常

    看看这个代码

    public class Solution {
    
        ArrayList<Long> digits = new ArrayList<>();
    
        ArrayList<Long> pal = new ArrayList<>();
    
        ArrayList<Long> findPal() {
    
            for (long i = 100; i <= 999; i++) {
                for (long j = 100; j <= 999; j++) {
                    Long product = i * j;
    
                    if (isPalindrome(product)) {
                        pal.add(product);
                    }
                }
            }
            return pal;
        }
    
        public static void main(String[] args) {
            Solution sol = new Solution();
            System.out.println(sol.isPalindrome((long) 121)); // true
            System.out.println(sol.isPalindrome((long) 12345)); // false
            System.out.println(sol.findPal()); // []
        }
    
        private boolean isPalindrome(Long longValue) {
    
            Long temp = longValue;
            String tempLong = "";
            while (temp != 0) {
                tempLong = tempLong + temp % 10 + "";
                temp = temp / 10;
            }
    
            return Long.parseLong(tempLong) == longValue;
        }
    }
    
    公共类解决方案{
    ArrayList位数=新的ArrayList();
    ArrayList pal=新的ArrayList();
    ArrayList findPal(){
    
    对于(长i=100;i您必须有一个函数,在从循环中传递数字后,它应将数字分隔为一个数字,如果反向也是真的,则应返回真

    例如: 121 - >121%10 - >1 然后121/10->12 类似地,继续这样做,直到其模数不为零 然后将最大的数字与(第1位的len)相乘,如 1*100+2*10+1 如果它是相同的,那么它是回文的
    我知道它很长,但它是最简单的事情!

    sol.isAlindrome(12321L)
    的结果是什么?isAlindrome方法在哪里?什么是
    数字
    用于?可能的重复我无法添加isAlindrome函数最初导致stackoverflow会抱怨“我的帖子主要是代码”我的isAlindrome函数有什么问题吗?@ken24ny-根据Naresh的回答,我认为答案一定是肯定的。我可以建议您阅读以下内容吗:实际上,您的
    isAlindrome
    方法的问题似乎与您在
    findPal
    中的
    pal
    字段中犯的错误相同。我无法添加d最初导致stackoverflow的isPalindrome函数会抱怨“我的帖子主要是代码”我的isPalindrome函数有什么问题吗?如果是非回文的,您没有清空digits arraylist。这是主要问题
    public class Solution {
    
        ArrayList<Long> digits = new ArrayList<>();
    
        ArrayList<Long> pal = new ArrayList<>();
    
        ArrayList<Long> findPal() {
    
            for (long i = 100; i <= 999; i++) {
                for (long j = 100; j <= 999; j++) {
                    Long product = i * j;
    
                    if (isPalindrome(product)) {
                        pal.add(product);
                    }
                }
            }
            return pal;
        }
    
        public static void main(String[] args) {
            Solution sol = new Solution();
            System.out.println(sol.isPalindrome((long) 121)); // true
            System.out.println(sol.isPalindrome((long) 12345)); // false
            System.out.println(sol.findPal()); // []
        }
    
        private boolean isPalindrome(Long longValue) {
    
            Long temp = longValue;
            String tempLong = "";
            while (temp != 0) {
                tempLong = tempLong + temp % 10 + "";
                temp = temp / 10;
            }
    
            return Long.parseLong(tempLong) == longValue;
        }
    }