标签:面试 算法
一个面试题:
java 实现将一个int数组变为一个int整数。例如:int[] arr = new int[]{1,2,3,4,5}; 变为12345
/** * Created by leo on 15/10/27. */ public class Interview { public static void main(String[] args) { System.out.println(parseInt(new int[]{1,2,3,4,5})); System.out.println(Integer.parseInt("12345")); } /** * 12345 * @param arr * @return */ public static final int parseInt(int[] arr){ int length = arr.length; int total = 0; for (int i = 0; i < length; i++){ int num = arr[i]; total = total + calculateWeightNum(num, length - (i + 1)) ; } return total; } private static int calculateWeightNum(int num , int numOfTen){ int result = num; for (int i = 0; numOfTen > 0 && i < numOfTen; i++){ result = result * 10; } return result; } }
本文出自 “java小沙弥” 博客,请务必保留此出处http://leokongwq.blog.51cto.com/1310215/1706710
标签:面试 算法
原文地址:http://leokongwq.blog.51cto.com/1310215/1706710