欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

华为18.9.5校招笔试题AK

程序员文章站 2022-07-13 14:20:48
...

26进制加法(一)

'a'-'z'代表十进制的0-25,求26进制加法。例如 'z'+'bc'= 'cb'

博主思路:

  • 首先将长度不同的字符串高位补'a'
  • 从低位开始将字符转换为10进制相加
  • 计算进位
  • 将得到的字符串高位去'a'
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            String a = in.next();
            String b = in.next();
            System.out.println(add(a, b));
        }
    }

    private static String add(String a, String b) {
        int lenA = a.length();
        int lenB = b.length();
        //结果
        String res = "";

        //高位补齐
        if (lenA > lenB) {
            for (int i = 0; i < (lenA - lenB); i++) {
                b = 'a' + b;
            }
            lenB = lenA;
        } else if (lenA < lenB) {
            for (int i = 0; i < (lenB - lenA); i++) {
                a = 'a' + a;
            }
            lenA = lenB;
        }
        //进位
        int carry = 0;
        //循环计算累加进位
        for (int i = lenA - 1; i >= 0; i--) {
            int tempres = (a.charAt(i) - 'a') + (b.charAt(i) - 'a') + carry;
            if (tempres < 26) {
                //s
                res = (char) (tempres + 'a') + res;
                carry = 0;
            } else {
                tempres = tempres - 26;
                res = (char) (tempres + 'a') + res;
                carry = 1;
            }
        }
        //首位如果有进位补'b'
        if (carry == 1) {
            res = 'b' + res;
        }
        //消除高位补位'a'
        StringBuffer sb = new StringBuffer(res);
        while (sb.charAt(0) == 'a' && sb.length() > 1) {
            sb.deleteCharAt(0);
        }
        return sb.toString();
    }
}

两个字符串比较是否包含(二)

没啥好说的,用桶思想5分钟撸完。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String a =sc.nextLine();
        String b=sc.nextLine();
        System.out.println(compareStrings(a, b));
    }

    public static boolean compareStrings(String a, String b) {
        int[] table = new int[26];
        int lenA = a.length();
        int lenB = b.length();
        for (int i = 0; i < lenA; i++) {
            table[a.charAt(i) - 'A']++;
        }
        for (int i = 0; i < lenB; i++) {
            table[b.charAt(i) - 'A']--;
            if(table[b.charAt(i) - 'A'] < 0) return false;
        }
        return true;
    }
}

字符串解压

一段字符串a2b3,代表的是aabbb;abc3解压后是abcabc,给一个字符串求解压后的结果,解压时要求重叠次数少的在前,如果重叠次数相同则按照字典序排序。

思路:

  • 首先用正则表达式将字符串分为一个纯字符的字符串数组和一个纯数组的字符串数组,代表着当前字符串重叠的次数。
  • 封装一个Node类,自定义比较器,按照先按照重复次数排序,再按照字典序排序。
import java.util.*;
public class Main {
    //自定义数据结构
    static class Node {
        //次数
        int times;
        String value;

        Node(String str,int times) {
            this.times = times;
            value = str;
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            ArrayList<Node> res = new ArrayList<>();
            String input = in.nextLine();
            String[] numberTemp = input.split("[a-z]+");
            String[] charsTemp = input.split("[0-9]");

            for (int i = 0; i < charsTemp.length; i++) {
                StringBuilder sb = new StringBuilder();
                int times = Integer.parseInt(numberTemp[i + 1]);
                for (int j = 0; j < times; j++) {
                    sb.append(charsTemp[i]);
                }
                res.add(new Node(sb.toString(),times));
            }
            Collections.sort(res,new myComparator());

            StringBuilder sbres = new StringBuilder();
            for (Node x : res) {
                sbres.append(x.value);
            }
            System.out.println(sbres.toString());
        }
    }
    
    //排序逻辑
    static class myComparator implements Comparator<Node> {
        @Override
        public int compare(Node o1, Node o2) {

            if (o1.times == o2.times) {
                return o1.value.compareTo(o2.value);
            }
            if (o1.times > o2.times) {
                return 1;
            }else {
                return -1;
            }
        }
    }
}