用友7.25笔试算法第二题求解
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 数字键按从小到大输出 * @param str string字符串 包含英文26个字母和@!./标点符号的字符串 * @return int整型二维数组 */ public static int[][] count(String str) { // write code here HashMap<String,int[]> map = new HashMap<>(); map.put("@",new int[]{1,1}); map.put("!",new int[]{1,2}); map.put(".",new int[]{1,3}); map.put("/",new int[]{1,4}); map.put("a",new int[]{2,1}); map.put("b",new int[]{2,2}); map.put("c",new int[]{2,3}); map.put("d",new int[]{3,1}); map.put("e",new int[]{3,2}); map.put("f",new int[]{3,3}); map.put("g",new int[]{4,1}); map.put("h",new int[]{4,2}); map.put("i",new int[]{4,3}); map.put("j",new int[]{5,1}); map.put("k",new int[]{5,2}); map.put("l",new int[]{5,3}); map.put("m",new int[]{6,1}); map.put("n",new int[]{6,2}); map.put("o",new int[]{6,3}); map.put("p",new int[]{7,1}); map.put("q",new int[]{7,2}); map.put("r",new int[]{7,3}); map.put("s",new int[]{7,4}); map.put("t",new int[]{8,1}); map.put("u",new int[]{8,2}); map.put("v",new int[]{8,3}); map.put("w",new int[]{9,1}); map.put("x",new int[]{9,2}); map.put("y",new int[]{9,3}); map.put("z",new int[]{9,4}); int n = str.length(); ArrayList<int[]> res = new ArrayList<>(); for (int i = 0; i < n; i++) { String cur = str.charAt(i)+""; int[] key = map.get(cur); boolean flag = true; for(int j= 0 ;j<res.size() ;j++ ){ if(res.get(j)[0] == key[0]){ res.get(j)[1]+=key[1]; System.out.println(res.get(j)[1]); flag = false; break; } } if(flag){ res.add(key); } } int[][] ans = new int[res.size()][]; for(int i =0 ; i< res.size() ; i++){ ans[i] = res.get(i); } Arrays.sort(ans, new Comparator<int[]>() { @Override public int compare(int[] o1, int[] o2) { return o1[0]-o2[0]; } }); return ans; }}
#用友#