首页 > 华为笔试-九宫格输入模拟
头像
柳南
编辑于 2020-03-14 16:43
+ 关注

华为笔试-九宫格输入模拟

今天参加华为社招笔试,题目是九宫格输入模拟,作为算法渣渣实在找不到错误了,死不瞑目,求大神指点,测试用例通过33.3%。

package com.search.remark.utils;

import java.util.*;

public class Main {
    static String[] one = {",", "."};
    static String[] two = {"a", "b", "c"};
    static String[] thr = {"d", "e", "f"};
    static String[] fou = {"g", "h", "i"};
    static String[] fiv = {"j", "k", "l"};
    static String[] six = {"m", "n", "o"};
    static String[] sev = {"p", "q", "r", "s"};
    static String[] ent = {"t", "u", "v"};
    static String[] nig = {"w", "x", "y", "z"};
    static String zer = " ";

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            String n = sc.nextLine();
            String[] c = n.split("#");
            StringBuffer re = new StringBuffer();
            for (int i = 0; i < c.length; i++) {
                if (i % 2 == 1) {
                    get(re, c[i]);
                } else {
                    re.append(c[i]);
                }
            }

            System.out.println(re.toString());
        }
    }

    private static StringBuffer get(StringBuffer re, String c) {
        String[] s = c.split("/");
        int x = -1;
        for (String a : s) {
            char[] ch = a.toCharArray();
            for (int m = 0; m < ch.length; m++) {
                if (String.valueOf(ch[m]).equals("1")) {
                    x++;
                    if (m == ch.length-1 || !String.valueOf(ch[m + 1]).equals("1")) {
                        if(x>one.length-1) x = x - one.length;
                        re.append(one[x]);
                        x = -1;
                    }
                }
                if (String.valueOf(ch[m]).equals("2")) {
                    x++;
                    if (m == ch.length-1 || !String.valueOf(ch[m + 1]).equals("2")) {
                        if(x>two.length-1) x = x - two.length;
                        re.append(two[x]);
                        x = -1;
                    }

                }
                if (String.valueOf(ch[m]).equals("3")) {
                    x++;
                    if (m == ch.length-1 || !String.valueOf(ch[m + 1]).equals("3")) {
                        if(x>thr.length-1) x = x - thr.length;
                        re.append(thr[x]);
                        x = -1;
                    }

                }
                if (String.valueOf(ch[m]).equals("4")) {
                    x++;
                    if (m == ch.length-1 || !String.valueOf(ch[m + 1]).equals("4")) {
                        if(x>fou.length-1) x = x - fou.length;
                        re.append(fou[x]);
                        x = -1;
                    }

                }
                if (String.valueOf(ch[m]).equals("5")) {
                    x++;
                    if (m == ch.length-1 || !String.valueOf(ch[m + 1]).equals("5")) {
                        if(x>fiv.length-1) x = x - fiv.length;
                        re.append(fiv[x]);
                        x = -1;
                    }

                }
                if (String.valueOf(ch[m]).equals("6")) {
                    x++;
                    if (m == ch.length-1 || !String.valueOf(ch[m + 1]).equals("6")) {
                        if(x>six.length-1) x = x - six.length;
                        re.append(six[x]);
                        x = -1;
                    }

                }
                if (String.valueOf(ch[m]).equals("7")) {
                    x++;
                    if (m == ch.length-1 || !String.valueOf(ch[m + 1]).equals("7")) {
                        if(x>sev.length-1) x = x - sev.length;
                        re.append(sev[x]);
                        x = -1;
                    }

                }
                if (String.valueOf(ch[m]).equals("8")) {
                    x++;
                    if (m == ch.length-1 || !String.valueOf(ch[m + 1]).equals("8")) {
                        if(x>ent.length-1) x = x - ent.length;
                        re.append(ent[x]);
                        x = -1;
                    }

                }
                if (String.valueOf(ch[m]).equals("9")) {
                    x++;
                    if (m == ch.length-1 || !String.valueOf(ch[m + 1]).equals("9")) {
                        if(x>nig.length-1) x = x - nig.length;
                        re.append(nig[x]);
                        x = -1;
                    }

                }
                if (String.valueOf(ch[m]).equals("0")) {
                    re.append(zer);
                }
            }
        }
        return re;
    }
}


全部评论

(5) 回帖
加载中...
话题 回帖

相关热帖

近期热帖

近期精华帖

热门推荐