前言:
目前大家对“java常见算法面试题思想”可能比较着重,我们都需要了解一些“java常见算法面试题思想”的相关资讯。那么小编在网摘上收集了一些关于“java常见算法面试题思想””的相关资讯,希望朋友们能喜欢,大家一起来了解一下吧!曾经被某公司面试,发了一道英文算法面试题,让用java写出算法。
Given an integer array containing digits from [0, 9], the task is to print all possible letter combinations that the numbers could represent. A mapping of digit to letters (just like on the telephone buttons) is being followed. Note that 0 and 1 do not map to any letters
int[] sizeTable = {0, 99}; private void checkInput(int[] inputArray) throws Exception { if(0 == inputArray.length) { throw new Exception("input one null array, please check"); } } public List<String> convertDigitToTetters(int[] inputArray) { List<List<String>> lists = new ArrayList<List<String>>(); List<String> resultList = new ArrayList<>(); try { checkInput(inputArray); List<String> subList = null; for(int x : inputArray) { subList = digitSpilt(x); lists.add(subList); } for (List<String> list : lists) { if (resultList.isEmpty()) { resultList = list; } else { resultList = resultList.stream().flatMap(item -> list.stream().map(item2 -> item + item2)).collect(Collectors.toList()); } } } catch (Exception e) { e.printStackTrace(); }finally { return resultList; } } public List<String> digitSpilt(int digit) throws Exception { int max = Arrays.stream(sizeTable).max().getAsInt(); int min = Arrays.stream(sizeTable).min().getAsInt(); if (digit < min || digit > max) { throw new Exception("the Value out of range, please check it again."); } List<Character> list = String.valueOf(digit).chars().mapToObj(item -> (char)item).collect(Collectors.toList()); List<String> tettleList = new ArrayList<String>(); list.stream().forEach(x -> { String[] array = DigitEnum.getValue(Character.getNumericValue(x)).split(", "); tettleList.addAll(Arrays.asList(array)); }); return tettleList; }
public enum DigitEnum { ONE(1, ""), TWO(2, "a, b, c"), THREE(3, "d, e, f"), FOUR(4, "g, h, i"), FIVE(5, "j, k, l"), SIX(6, "m, n, o"), SEVEN(7, "p, q, r, s"), EIGHT(8, "t, u, v"), NINE(9, "w, x, y, z"), ZERO(0, ""); private int key; private String value; DigitEnum(int key, String value) { this.value = value; this.key = key; } public String getValue() { return value; } public int getKey() { return key; } public static String getValue(int key) { for (DigitEnum d : DigitEnum.values()) { if (d.getKey() == key) { return d.getValue(); } } return null; } }
测试用例:
@Test public void test1() { DigitToTetters digitToTetters = new DigitToTetters(); int[] arr = { 2, 3 }; String[] strarra = { "ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf" }; List<String> result = digitToTetters.convertDigitToTetters(arr); Assert.assertArrayEquals(strarra, result.toArray()); } @Test public void test2() { DigitToTetters digitToTetters = new DigitToTetters(); int[] arr = { 9 }; String[] strarra = { "w", "x", "y", "z" }; List<String> result = digitToTetters.convertDigitToTetters(arr); Assert.assertArrayEquals(strarra, result.toArray()); } @Test public void test3() { DigitToTetters digitToTetters = new DigitToTetters(); int[] arr = { 1, 0 }; String[] strarra = {""}; List<String> result = digitToTetters.convertDigitToTetters(arr); Assert.assertArrayEquals(strarra, result.toArray()); } @Test public void test4() { DigitToTetters digitToTetters = new DigitToTetters(); int[] arr = { 1, 9 }; String[] strarra = { "w", "x", "y", "z" }; List<String> result = digitToTetters.convertDigitToTetters(arr); Assert.assertArrayEquals( strarra, result.toArray()); } @Test public void test5() { DigitToTetters digitToTetters = new DigitToTetters(); int[] arr = { 1, 299 }; List<String> result = digitToTetters.convertDigitToTetters(arr); boolean isFail = result.isEmpty(); Assert.assertTrue(isFail); }
标签: #java常见算法面试题思想