标签:poi public dex spec alc hat ted des har
code 不难,multi dimension array 的求 summation。 之前准备地里的LinkedIn高频题一个没碰到。。。 /** Suppose you are given a class that implements a k-dimensional array * interface and you want to perform an operation that requires you to * iterate over all elements of the array using its indices. To be * specific, let‘s assume we want to calculate the sum of all elements in * the array. The interface only provides you a get(int[]) method which 鏉ユ簮涓€浜?.涓夊垎鍦拌鍧?. * allows one to fetch the element at that location given the indices along * each dimension. * * For e.g, suppose we are dealing with 4D arrays, given [2, 1, 3, 0], the * class will provide you array[2][1][3][0]. * * Write a function that given an instance of the k-D array class and size . 1point 3acres 璁哄潧 * of its dimensions, calculates the sum of all elements. * * @param instance of MultiDimArray class that implements a k-D array of * ints which provides a method x.get(int[]) to get the element * located at the indices in the array. from: 1point3acres.com/bbs * @param array of ints stating the size of each dimension of the k-D array. 鍥磋鎴戜滑@1point 3 acres * @return an int which is the sum of all elements in the k-D array * * example: Given object m that holds a 2x2x3 array * a=[[[3, 2, 2], [1, 5, 0]], [[2, 0, 1], [1, 1, -2]]] (Only for illustration * purposes. This need not be the internal implementation of our k-D array) * the function call arraySum(m, [2, 2, 3]) should return 16 * (=3+2+2+1+5+2+1+1+1-2) */ /* DO NOT IMPLEMENT */ public interface MultiDimArray { int get(vector<int> indices); } public int arraySum (MultiDimArrayImpl m, int [] dimensions) { }
public List<int[]> getAllIndex (int[] dimensions) { List<int[]> result = new ArrayList<int[]>(); int[] index = new int[dimensions.length]; helper(result, index, 0, dimensions); return result;. } private void helper(List<int[]> result, int[] index, int depth, int[] dimensions) { if (depth == dimensions.length) {-google 1point3acres result.add(index.clone()); return; } int currentDimension = dimensions[depth]; for (int i=0; i<currentDimension; i++) { index[depth] = i;-google 1point3acres helper(result, index, depth+1, dimensions); } }
标签:poi public dex spec alc hat ted des har
原文地址:http://www.cnblogs.com/apanda009/p/7964875.html