华为机试题及参考答案

本文提供了几道华为公司面试中常见的编程题目,包括字符串处理、回文判断、数组操作、大数相加等多个方面。通过这些题目,可以考察候选人在算法和基本编程技巧上的能力。例如,有一道题目要求对输入字符串的每个字符ASCII值加5,如果超过'z'则转为'a'。另一道题目要求找到字符串中重复最长的子串。还有涉及大数相加、二维数组操作、字符串数字排序等题目,充分展示了编程面试的多元性和挑战性。
摘要由CSDN通过智能技术生成

/*

 * 程序实现目标:输入一个字符串,将其各个字符对应的ASCII值加5后,输出结果。

       程序要求:该字符串只包含小写字母,若其值加5后的字符值大于'z',将其转换成从a开始的字符。

 */

public class Main001 {

    public static void main(String[] args) {

       Scanner scanner=new Scanner(System.in);

       String str="afdsfdsxz";

       char[] indata=str.toCharArray();

       addAccii(indata);

    } 

    public static void addAccii(char[] indata){

       for(int i=0;i<indata.length;i++){

           if((indata[i]+5)>122){

              indata[i]= 'a';

           }else{

              indata[i]= (char)(indata[i]+5);

           }

           System.out.print(indata[i]);

       }     

    }

}

/*

 * 功能描述:判断一个字符串中是否只含有相同的子字符串(子串长度>=2

        输入:abab

        返回:true

        输入:abcd

        返回:false

 */

public class Main002 {

static void main(String[] args) {

       String str = "ertyertyabcdabcdsdf464132132146464165479lhkjhnkjgibkjbjljdslfsajdlasdf4asdf5a7e";

        int count = 2;

        List<String> maxLengthList = newArrayList<String>();

        maxLengthList = checkString(str,count, maxLengthList);

        System.out.println(maxLengthList);

        StringBuilder sbBuilder = new StringBuilder();

        if (maxLengthList.isEmpty()) {

            System.out.println("无重复并且长度大于2的子串!");

        } else {

            int maxLength = maxLengthList.get(0).length();

            sbBuilder.append(maxLengthList.get(0)).append(",");

            for (int i = 1; i < maxLengthList.size(); i++) {

                if (maxLengthList.get(i).length() == maxLength){

                   sbBuilder.append(maxLengthList.get(i)).append(",");

                }

            }

            System.out.println("重复最长子串为:"

                    +sbBuilder.toString().substring(0,

                           sbBuilder.toString().length() - 1));

        }       

       String str1 = "abab";

        int startindex = 0;

        int count2 = 2;

        System.out.println(isHave(str.substring(0,count2), str1, startindex,

                count));

    }   

     public static List<String>checkString(String str, int count,

               List<String> maxLengthList) {

            if (count >str.length() / 2 + 1) {

                return maxLengthList;

            }

            for (int i = 0; i <str.length() - count; i++) {

               List<String> strList = new ArrayList<String>();

                String regex = str.substring(i,i + count) + "+?";

                Pattern pattern= Pattern.compile(regex);

                Matcher matcher= pattern.matcher(str);

                while (matcher.find()) {

                   strList.add(matcher.group());

                }

                if (strList.size()> 1

                       && !maxLengthList.contains(str.substring(i, i + count))) {

                   maxLengthList.add(str.substring(i, i + count));

                }

            }

            count++;

            if (count >str.length()) {

                return maxLengthList;

            }

            checkString(str,count, maxLengthList);

            Collections.sort(maxLengthList,new Comparator<Object>() {

                @Override

                publicint compare(Object o1,Object o2) {

                    int length1 =o1.toString().length();

                    int length2 =o2.toString().length();

                    return length1 - length2< 0 ? 1 : (length1 - length2 == 0 ? o1

                           .toString().compareTo(o2.toString()) : -1);

                }

            }

);

            return maxLengthList;

         }

     public static boolean isHave(Stringstr1, String str,int startindex,

                int count) {

            boolean isHave =false;

            ++startindex;

            if (count >str.length() / 2 + 1) {

                return isHave;

            }

            List<String>strList = new ArrayList<String>();

            String regex = str1+ "+?";

            Pattern pattern =Pattern.compile(regex);

            Matcher matcher =pattern.matcher(str);

            while (matcher.find()) {

               strList.add(matcher.group(0));

            }

            if (strList.size()> 1) {

                isHave = true;

            } else if(!str.endsWith(str1)) {

                int endIndex =startindex + count;

                if (endIndex >str.length()) {

                    return isHave;

                }

                isHave = isHave(str.substring(startindex,endIndex), str,

                       startindex, count);

            } else {

                ++count;

                isHave = isHave(str.substring(0,count), str, startindex, count);

            }

            return isHave;

        }

     }

 

/*

 * 3、手动输入一个存储整数的数组,要求输出数组里面的2个最大值。

实例:

输入:1,2,5,9,84,3,2

输出:84,9

 *  */

public class Main003 {

    public static void main(String[] args) {

       Main003 test = new Main003();

       int[] arr = {1,45,98,6,4};

       test.getMaxValue(arr); 

    }

    public void getMaxValue(int[] arr){

       int temp=0;

       if(arr.length<1){

           System.out.println("数组元素个数至少为2");

       }

       else{

       for(int i=0;i<arr.length-1;i++){

           for(int j=0;j<arr.length-1-i;j++){

             if(arr[i]>arr[i+1]){

              temp = arr[i];

              arr[i]=arr[i+1];

              arr[i+1]=temp;

             }

           }

       }

       System.out.print(arr[arr.length-1]+""+arr[arr.length-2]);

    }

    }

}

/*

 * 题目描述:

    有这样一类数字,他们顺着看和倒着看是相同的数,例如:121,656,2332等,

这样的数字就称为:回文数字。编写一个函数,判断某数字是否是回文数字。

要求实现方法:

    public String isPalindrome(String strIn);

【输入】strIn:整数,以字符串表示;

【返回】true:是回文数字;

        false: 不是回文数字;

【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出

示例:

    输入:strIn = “121”

返回:”true

 */

public class Main004 {

    public static void main(String[] args) {

       String inputValue = JOptionPane.showInputDialog("请输入一个整数");

         long inputValueLong =Long.parseLong(inputValue);

         long temp =inputValueLong;

         long reverseLong = 0L;

         while(inputValueLong !=0)

         {

          reverseLong =reverseLong*10+inputValueLong%10;

          inputValueLong =inputValueLong/10;

         }

         if(reverseLong ==temp)

          System.out.println("你输入的是回文数");

         else

          System.out.println("你输入的不是回文数");

    }

}

 

/*

 * 6.手动输入一个字符串,仅限小写字母,统计并输出每个字符在字符串中出现的次数,并输出。

 *  提示(可以用Map)

         实例:

         输入:aaabbbccc

          输出: a 3

          b 3

          c 3

 */

public class Main006 {

    public static void main(String[] args) { 

       statTimes("dfsdfgerfgtewthgat");

    }

     static void statTimes(Stringparam)

      {

         if(param ==nullreturn;   

       //hashset保存不重复的值因此

       HashSet<Character>hSet = new HashSet<Character>();      

       char[] cs =param.toCharArray();      

       for (char c : cs)

        hSet.add(c);      

       ArrayList<Character>list = new ArrayList<Character>(hSet);     

       int n =hSet.size(); //有多少种字符   

       int[] times =new int[n]; //保存每种字符的出现次数

       for (char c : cs)  //进行统计

        times[list.indexOf(c)]++;

       for (int i = 0; i < n;i++)

        System.out.println("字符 " + list.get(i) +"出现了" + times[i] +"次。");

       //打印结果     

      }

}

/*

 * 要求实现方法public String addTwoBigNumber(String s1,string s2)

        大数相加,注意处理异常

 */

public class Main007 {

    String result,s1,s2;

    int s1Len, s2Len;

    public  Main007(Stringstr1,String str2)

    {

       s1=str1;

       s2=str2;

    }   

    /**

     * @param args

     */

108题中有部分题目重合,因此么有收录在压缩文件中。 华为试 ├─001 字符串最后一个单词长度 │ └─Source ├─002 计算字符个数 │ └─Source ├─003 明明的随数 │ └─Source ├─004 字符串分隔 │ └─Source ├─005 进制转换 │ └─Source ├─006 质数因子 │ └─Source ├─007 取近似值 │ └─Source ├─008 合并表记录 │ └─Source ├─009 提取不重复的整数 │ └─Source ├─010 字符个数统计 │ └─Source ├─011 数字颠倒 │ └─Source ├─012 字符串反转 │ └─Source ├─013 句子逆序 │ └─Source ├─014 字典序排序 │ └─Source ├─015 求int型正整数在内存中存储是1的个数 │ └─Source ├─016 购物单 │ ├─Debug │ ├─Source │ │ └─Debug │ ├─Source - 时间优先 │ │ └─Debug │ └─Source - 空间优先 │ ├─Debug │ └─Release ├─017 坐标移动 ├─018 识别IP地址分类统计 │ └─Source │ └─Debug ├─019 错误记录 ├─020 密码验证合格程序 ├─021 密码破解 ├─023 删除字符串中出现次数最少字符 │ └─Source │ └─Debug ├─024 合唱队 │ └─Source │ ├─Debug │ └─Release ├─025 数据分类处理 │ └─Source │ └─Debug ├─026 查找兄弟单词 │ └─Source │ └─Debug ├─027 素数伴侣 │ └─Source │ └─Debug ├─028 字符串合并处理 │ └─Source │ └─Debug ├─030 密码截取(查找最长回文字符串) ├─031 蛇形矩阵 │ └─Source │ └─Debug ├─033 判断IP是否属于同一子网 │ └─Source │ └─Debug ├─034 称砝码 │ └─Source │ └─Debug ├─035 学英语 │ └─Source │ └─Debug ├─036 迷宫问题 │ └─Source │ └─Debug ├─037 数独问题 │ └─Debug ├─038 名字漂亮度 │ └─Source │ └─Debug ├─039 字符串截取 │ └─Source │ └─Debug ├─040 单链表删除数据 │ └─Source │ └─Debug ├─041 多线程 │ └─Source │ ├─Backup │ ├─Debug │ │ └─041.tlog │ └─Release │ └─041.tlog ├─042 表达式计算 │ └─Source │ └─Debug ├─043 计算字符串距离 │ └─Source │ └─Debug ├─044 杨辉三角形变形 ├─046 挑7 ├─047 完全数 │ └─Debug ├─048 高精度加法 ├─049 输出n个数中最小的k个 │ └─Debug ├─050 找出字符串只出现一次的字符 │ └─Debug ├─051 组成一个偶数最接近的2个质数 │ └─Debug ├─052 M个苹果放入N个盘子 ├─053 查找整数二进制中1的个数 ├─054 DNA子串 ├─055 MP3光标位置 │ └─Source │ └─Debug ├─056 查找2个字符串最大相同子串 │ └─Debug ├─057 配置文件恢复 │ └─Source │ └─Debug ├─058 24点计算 │ └─Debug ├─059 成绩排序 ├─060 矩阵相乘 ├─061 矩阵乘法次数计算 ├─062 字符串通配符 │ └─Debug ├─066 命令行解析 │ └─Source │ └─Debug ├─067 最大相同子串长度 │ └─Debug ├─068 火车编号进站 │ └─Debug ├─072 数组合并 ├─074 埃及分数 │ └─Source │ └─Debug ├─076 密码截取 │ └─Source ├─077 求最大连续bit数 ├─078 密码强度 ├─079 扑克牌大小 │ └─Source │ └─Debug ├─081 合法IP ├─082 棋盘格子走法 ├─083 在字符串中找出连续最长数字串 ├─084 int数组分组,两组和相等 │ └─Source │ └─Debug ├─086 人民币转换 │ └─Source │ └─Debug ├─087 表示数字 ├─090 自动售货系统 │ └─Source │ └─Debug └─091 24点输出 └─Debug
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值