华为18.9.5校招笔试题AK
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;
}
}
}
}
华为18.9.5校招笔试题AK的更多相关文章
- 剑指Offer——腾讯+360+搜狗校招笔试题+知识点总结
剑指Offer--腾讯+360+搜狗校招笔试题+知识点总结 9.11晚7:00,腾讯笔试.选择题与编程.设计题单独计时. 栈是不是顺序存储的线性结构啊? 首先弄明白两个概念:存储结构和逻辑结构. 数据 ...
- 剑指Offer——京东校招笔试题+知识点总结
剑指Offer--京东校招笔试题+知识点总结 笔试感言 经过一系列的笔试,发觉自己的基础知识还是比较薄弱的,尤其是数据结构和网络,还有操作系统.工作量还是很大的.做到精确制导的好方法就是在网上刷题,包 ...
- 剑指Offer——美团内推+校招笔试题+知识点总结
剑指Offer--美团内推+校招笔试题+知识点总结 前言 美团9.9内推笔试.9.11校招笔试,反正就是各种虐,笔试内容如下: 知识点:图的遍历(DFS.BFS).进程间通信.二叉查找树节点的删除及中 ...
- 剑指Offer——CVTE校招笔试题+知识点总结(Java岗)
剑指Offer(Java岗)--CVTE校招笔试题+知识点总结 2016.9.3 19:00参加CVTE笔试,笔试内容如下: 需要掌握的知识:Linux基本命令.网络协议.数据库.数据结构. 选择题 ...
- 2018 CVTE 前端校招笔试题整理
昨天晚上(7.20)做了CVTE的前端笔试,总共三十道题,28道多选题,2道编程题 .做完了之后觉得自己基础还是不够扎实,故在此整理出答案,让自己能从中得到收获,同时给日后的同学一些参考. 首先说一下 ...
- 2016京东Android研发校招笔试题
一.选择题汇总,具体的记不住啦.. 1.计网:ip的网络前缀.SNMP(报文组成):http://blog.csdn.net/shanzhizi/article/details/11606767 参考 ...
- 阿里2014校招笔试题(南大)——利用thread和sleep生成字符串的伪随机序列
引言:题目具体描述记不大清了,大概是:Linux平台,利用线程调度的随机性和sleep的不准确性,生成一个各位均不相同的字符数组的伪随机序列.不得使用任何库函数.(这句记得清楚,当时在想线程库算不算, ...
- 微软2017校招笔试题3 registration day
题目 It's H University's Registration Day for new students. There are M offices in H University, numbe ...
- 微软2017校招笔试题2 composition
题目 Alice writes an English composition with a length of N characters. However, her teacher requires ...
随机推荐
- Node.js其他模块
清明假期过得挺快,3天说没就没了,天热了今天把房间打扫了一下,看着挺舒心的.周六了解了下进程管理的Process模块,由于进程管理知识也比较多,今天先把其他的一些模块了解一下,进程管理这块以后慢慢学. ...
- 解决部分小程序无法获取UnionId的问题
问题背景 通过观察数据,发现有一部分用户是无法获取到UnionId的 也就是接口返回的参数中不包含UnionId参数 看了微信文档的解释,只要小程序在开放平台绑定,就一定会分配UnionId 网上也有 ...
- .net core 2.2 部署CentOS7(1)安装虚拟机
目录: .net core 2.2 部署CentOS7(1)安装虚拟机 .net core 2.2 部署CentOS7(2)给虚拟机安装CentOS7 .net core 2.2 部署CentOS7( ...
- cv程序员的日常_1
某天pom文件粘贴报红 然后我就问我同事 然后我就百度 https://blog.csdn.net/guoyiyun_tz/article/details/82115024 看完之后…emmmm没错啊 ...
- HDU 1428
漫步校园 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- 浅谈常用的设计模式(new)
简单工厂模式 抽象工厂模式 代理模式 装饰者模式(Decorator):动态地给一个对象添加一些额外的职责,就增加功能来说,装饰着模式比生成子类更加灵活. 建造者模式:builder构建
- YII中利用urlManager将URL改写成restful风格
这里主要涉及url显示样式 1.打开config文件夹下面的mian.php 2.修改内容 如把地址http://www.test.com/index.php?r=site/page/sid/ ...
- loj#6235. 区间素数个数(min25筛)
题意 题目链接 Sol min25筛的板子题,直接筛出\(g(N, \infty)\)即可 筛的时候有很多trick,比如只存\(\frac{N}{x}\)的值,第二维可以滚动数组滚动掉 #inclu ...
- hallo world
- Checkpoint not complete
Checkpoint not complete Current log# 2 seq# 876 mem# 0: +DATA/tykfdb/onlinelog/group_2.258.983586883 ...