首页 > 代码库 > Colorful Number
Colorful Number
Problem
A number can be broken into different sub-sequence parts. Suppose, a number 3245 can be broken into parts like 3 2 4 5 32 24 45 324 245. And this number is a colorful number, since product of every digit of a sub-sequence are different. That is, 3 2 4 5 (3*2)=6 (2*4)=8 (4*5)=20 (3*2*4)= 24 (2*4*5)= 40
But 326 is not a colorful number as it generates 3 2 6 (3*2)=6 (2*6)=12.
You have to write a function that tells if the given number is a colorful number or not.
Solution
1 public static boolean colorfulNumber(int num) { 2 //if(num == null) return false; 3 4 //convert num to arraylist 5 List<Integer> al = new ArrayList<Integer>(); 6 while(num > 0) { 7 al.add(num%10); 8 num /= 10; 9 }10 System.out.println(al);11 12 //store res into hashset13 Set<Integer> hs = new HashSet<Integer>();14 for(int i=1; i<al.size(); i++) {15 for(int j=0; j<al.size()-i+1; j++) {16 int pro = al.get(j);17 for(int k=j+1; k<j+i; k++) {18 pro = pro * al.get(k);19 }20 System.out.println(pro);21 if(hs.contains(pro)) return false;22 else hs.add(pro);23 }24 }25 System.out.println(hs);26 return true;27 }
Colorful Number
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。