| packagecn.fansunion.leecode.kit;
 importjava.util.ArrayList;
 importjava.util.Arrays;
 importjava.util.List;
 importjava.util.Set;
 /**
  * 一些常见的工具类
  *
  * @author wen.lei@brgroup.com
  *
  *         2022-1-9
  */
 publicclassKit {
     /**
      * 打印数组
      *
      * @param array
      */
     publicstaticvoidprint(int[] array) {
         if(array == null) {
             System.out.println("The array is null");
             return;
         }
         if(array.length == 0) {
             System.out.println("The array length is 0");
             return;
         }
         for(intindex = 0; index < array.length; index++) {
             System.out.print(array[index]);
             if(index != array.length - 1) {
                 System.out.print(",");
             } else{
                 System.out.println();
             }
         }
     }
     publicstaticint[] setToArray(Set<Integer> set) {
         int[] nums = newint[set.size()];
         intindex = 0;
         for(Integer num : set) {
             nums[index] = num;
             index++;
         }
         returnnums;
     }
     /**
      * 把1个十进制的整数,转换成十进制数字组成的数组,1234->[1, 2, 3, 4]
      *
      * @param n
      * @return
      */
     publicstaticList<Integer> intToDigitList(intn) {
         List<Integer> numList = newArrayList<>();
         while(n >= 10) {
             numList.add(0, n % 10);
             n = n / 10;
         }
         numList.add(0, n);
         returnnumList;
     }
     /**
      * 把1个十进制的整数,转换成十进制数字组成的数组,1234->[4, 3, 2, 1]
      *
      * @param totalSum
      * @return
      */
     publicstaticList<Integer> intToDigitListDesc(intn) {
         // 取模取余
         List<Integer> list = newArrayList<>();
         // >=10
         while(n >= 10) {
             list.add(n % 10);
             n = n / 10;
         }
         list.add(n);
         returnlist;
     }
     publicstaticvoidmain(String[] args) {
         System.out.println(Kit.intToDigitList(1234));
         System.out.println(Kit.intToDigitListDesc(1234));
     }
     /**
      * 把一个十进制的整数数组,转换成int格式的数字
      *
      * @param numArray
      * @return
      */
     publicstaticintdigitListToInt(List<Integer> numList) {
         intnum = 0;
         for(intindex = numList.size() - 1; index >= 0; index--) {
             finalintcurDigit = numList.get(index);
             finalinttime = numList.size() - 1- index;
             num += curDigit * Math.pow(10, time);
         }
         returnnum;
     }
     publicstaticintdigitArrayToInt(int[] numArray) {
         List<Integer> numList = newArrayList<>();
         for(intnum : numArray) {
             numList.add(num);
         }
         returndigitListToInt(numList);
     }
     /**
      * 反转list,[1,2,3,4] -> [4,3,2,1]
      *
      * @param numList
      * @return
      */
     publicstaticList<Integer> reverseList(List<Integer> numList) {
         if(numList == null|| numList.size() == 1) {
             returnnumList;
         }
         List<Integer> reverseList = newArrayList<>();
         for(intindex = numList.size() - 1; index >= 0; index--) {
             reverseList.add(numList.get(index));
         }
         returnreverseList;
     }
     /**
      * 反转list,且删除开头的0;[1,2,3,4] -> [4,3,2,1],<br/>
      * [1,2,3,0] -> [0,3,2,1] -> [3,2,1],[1,2,3,0,0] -> [0,0,3,2,1] -> [3,2,1]<br/>
      * [0]->[0]
      *
      * @param numList
      * @return
      */
     publicstaticList<Integer> reverseListThenRemoveStartZero(List<Integer> numList) {
         List<Integer> reverseList = reverseList(numList);
         List<Integer> list = removeStartZero(reverseList);
         returnlist;
     }
     /**
      * 删除一个list中的0开头的数字。如果只有1个0,保留 <br/>
      * [0,0,3,2,1] -> [3,2,1], [3,2,1] -> [3,2,1],[0]->[0]
      *
      * @param reverseList
      * @return
      */
     privatestaticList<Integer> removeStartZero(List<Integer> reverseList) {
         // 找到第1个非0的index
         intfirstNotZeroIndex = -1;
         for(intindex = 0; index < reverseList.size(); index++) {
             intnum = reverseList.get(index);
             // 非前导0,才保留
             if(num != 0) {
                 firstNotZeroIndex = index;
                 break;
             }
         }
         if(firstNotZeroIndex == -1) {
             returnArrays.asList(0);
         }
         List<Integer> list = reverseList.subList(firstNotZeroIndex, reverseList.size());
         returnlist;
     }
 }
 |