package cn.fansunion.leecode.kit; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Set; /** * 一些常见的工具类 * * @author wen.lei@brgroup.com * * 2022-1-9 */ public class Kit /** * 打印数组 * * @param array */ public static void print( 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 ( int index = 0 ; index < array.length; index++) System.out.print(array[index]); if (index != array.length - 1 ) System.out.print( "," ); else
System.out.println();
public static int [] setToArray(Set<Integer> set) int [] nums = new int [set.size()]; int index = 0 ; for (Integer num : set) nums[index] = num; index++;
return nums;
/** * 把1个十进制的整数,转换成十进制数字组成的数组,1234->[1, 2, 3, 4] * * @param n * @return */ public static List<Integer> intToDigitList( int n) List<Integer> numList = new ArrayList<>(); while (n >= 10 ) numList.add( 0 , n % 10 ); n = n / 10 ;
numList.add( 0 , n); return numList;
/** * 把1个十进制的整数,转换成十进制数字组成的数组,1234->[4, 3, 2, 1] * * @param totalSum * @return */ public static List<Integer> intToDigitListDesc( int n) // 取模取余 List<Integer> list = new ArrayList<>(); // >=10 while (n >= 10 ) list.add(n % 10 ); n = n / 10 ;
list.add(n); return list;
public static void main(String[] args) System.out.println(Kit.intToDigitList( 1234 )); System.out.println(Kit.intToDigitListDesc( 1234 ));
/** * 把一个十进制的整数数组,转换成int格式的数字 * * @param numArray * @return */ public static int digitListToInt(List<Integer> numList) int num = 0 ; for ( int index = numList.size() - 1 ; index >= 0 ; index--) final int curDigit = numList.get(index); final int time = numList.size() - 1 - index; num += curDigit * Math.pow( 10 , time);
return num;
public static int digitArrayToInt( int [] numArray) List<Integer> numList = new ArrayList<>(); for ( int num : numArray) numList.add(num);
return digitListToInt(numList);
/** * 反转list,[1,2,3,4] -> [4,3,2,1] * * @param numList * @return */ public static List<Integer> reverseList(List<Integer> numList) if (numList == null || numList.size() == 1 ) return numList;
List<Integer> reverseList = new ArrayList<>(); for ( int index = numList.size() - 1 ; index >= 0 ; index--) reverseList.add(numList.get(index));
return reverseList;
/** * 反转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 */ public static List<Integer> reverseListThenRemoveStartZero(List<Integer> numList) List<Integer> reverseList = reverseList(numList); List<Integer> list = removeStartZero(reverseList); return list;
/** * 删除一个list中的0开头的数字。如果只有1个0,保留 <br/> * [0,0,3,2,1] -> [3,2,1], [3,2,1] -> [3,2,1],[0]->[0] * * @param reverseList * @return */ private static List<Integer> removeStartZero(List<Integer> reverseList) // 找到第1个非0的index int firstNotZeroIndex = - 1 ; for ( int index = 0 ; index < reverseList.size(); index++) int num = reverseList.get(index); // 非前导0,才保留 if (num != 0 ) firstNotZeroIndex = index; break ;
if (firstNotZeroIndex == - 1 ) return Arrays.asList( 0 );
List<Integer> list = reverseList.subList(firstNotZeroIndex, reverseList.size()); return list;
|