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

今天参加华为社招笔试,题目是九宫格输入模拟,作为算法渣渣实在找不到错误了,死不瞑目,求大神指点,测试用例通过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;
    }
}


#华为笔试#
全部评论
力扣上17题原题?
点赞 回复 分享
发布于 2020-03-14 16:04
一看你写这么多 if 判断,两层for循环,明显就是 执行超时。 所有公司的 编程题 都要找技巧的,经验总结,很多题目用暴力法都可以解决,但通过率不超过 30%,因为遇到复杂的测试用例就执行时间超时。华为的题目要求算低了,还有 33.3%
点赞 回复 分享
发布于 2020-03-21 22:44
问下华为笔试能用IDE吗
点赞 回复 分享
发布于 2020-04-14 18:19
我也挂了
点赞 回复 分享
发布于 2020-04-30 10:35
没看懂题目意思,22/222为什么输出应该是abc?而不是bc?没看到题目,帮着讲一下?
点赞 回复 分享
发布于 2020-10-29 20:29

相关推荐

11-01 08:48
门头沟学院 C++
伤心的候选人在吵架:佬你不要的,能不能拿户口本证明过户给我。。球球了
点赞 评论 收藏
分享
点赞 7 评论
分享
牛客网
牛客企业服务