华为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 ...
随机推荐
- [转]wx.getUserInfo(OBJECT) 微信小程序 获取用户信息
本文转自:http://mp.weixin.qq.com/debug/wxadoc/dev/api/open.html wx.getUserInfo(OBJECT) 获取用户信息,withCreden ...
- dev中文本框等获取焦点事件
<ClientSideEvents GotFocus="GotFocus" /> editContract.SetFocus()//设置文本框等的焦点 function ...
- java存储图片
import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.util. ...
- HDU 1142
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- [h5+api]移动app开发用到的微信好友,朋友圈,qq好友,新浪微博分享合集
适用H5+环境,能够使用plus方法的移动app中 /** * Created by HBuilder. * User: tyx * Date: 2018-11-21 * Time: 17:28:51 ...
- Web缓存加速指南(转载)
这是一篇知识性的文档,主要目的是为了让Web缓存相关概念更容易被开发者理解并应用于实际的应用环境中.为了简要起见,某些实现方面的细节被简化或省略了.如果你更关心细节实现则完全不必耐心看完本文,后面参考 ...
- js判断移动端页面按home键切换到桌面事件
---恢复内容开始--- 原理就是通过页面标签切换事件(visibilitychange)来判断,亦可用户移动端桌面和app切换. 先看代码: var hiddenProperty = 'hidden ...
- 重要BLOG
Cloud http://www.cnblogs.com/CloudMan6/tag/OpenStack/ 算法基础 http://www.cnblogs.com/ECJTUACM-873284962 ...
- 微信小程序为什么不被看好?
我自认为对新技术还是比较有热情的,可对于小程序这个“新技术”,我却完全是被动的.去年9月份的时候,微信小程序开始内测,瞬间引爆朋友圈.知乎等一众分享平台.当时我大概了解了一下,觉得从技术角度上来说没啥 ...
- flutter Row里面元素居中显示
直接上代码: new Expanded( flex: , child: new Row( children: <Widget>[ Expanded( child: new Containe ...