华为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 ...
随机推荐
- [CQOI 2018]破解D-H协议
Description 题库链接 给出 \(A,B,P,g\) ,\(g\) 是 \(P\) 的原根,求出 \(A\equiv g^a\pmod{P}\) , \(B\equiv g^b\pmod{P ...
- Node.js之Express三
端午节3天说没就没了,自己的脚伤都快一个月了还没好,原本想着去桂林或者厦门呢,可计划赶不上变化,看自己公司C#软件工程师的招聘条件有要求MongoDb,年前就打算自己学习下,买的这本书就叫Node.j ...
- 常用算法2 - 广度优先搜索 & 深度优先搜索 (python实现)
1. 图 定义:图(Graph)是由顶点的有穷非空集合和顶点之间边的集合组成,通常表示为:G(V,E),其中,G表示一个图,V是图G中顶点的集合,E是图G中边的集合. 简单点的说:图由节点和边组成.一 ...
- vs2010查看quartz.net 2.1.2的源码时其中一报错的解决方法
问题: 使用vs2010查看quartz.net 2.1.2的源码时,报错: ..\Quartz.NET-2.1.2\server\Quartz.Server\Quartz.Server.2010.c ...
- CLR via C# 读书笔记-26.线程基础
前言 这俩个月没怎么写文章做记录分享,一直在忙项目上线的事情,但是学习这件事情,停下来就感觉难受,clr线程这章也是反复看了好多遍,书读百遍其义自见,今天我们来聊下线程基础 1.进程是什么,以及线程起 ...
- 对io进行分流
package org.richin.io.Stream.util; import java.io.BufferedInputStream; import java.io.Buffer ...
- 记录:springmvc + mybatis + maven 搭建配置流程
前言:不会配置 spring mvc,不知道为什么那样配置,也不知道从何下手,那么看这里就对了. 在 IDEA 中搭建 maven + springmvc + mybatis: 一.在 IDEA 中首 ...
- mybatis作用、基本使用、小结
1. MyBatis 1.1. 作用 MyBatis是持久层框架,它是支持JDBC的!简化了持久层开发! 使用MyBatis时,只需要通过接口指定数据操作的抽象方法,然后配置与之关联的SQL语句,即可 ...
- Windows access Linux / Ubuntu via Remote Desktop via xrdp
Windows 多用户远程桌面连接到 Ubuntu / Linux Access Ubuntu from Windows remotely Follow these steps : Step 1 ...
- mysql 运行 sql 脚本
方式一: 打开脚本,复制里面的全部内容,登陆数据库后运行. 方式二: window cmd 运行如下命令: mysql -u root -proot --port 3306 <D:\simple ...