华为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 ...
随机推荐
- [转] 如何应用设计模式设计你的足球引擎(三和四)----Design Football Game(Part III and IV)
原文地址:http://www.codeproject.com/KB/cpp/applyingpatterns2.aspx 作者:An 'OOP' Madhusudanan 译者:赖勇浩(http:/ ...
- Unity3d嵌入web网页
应用场景 程序中的界面风格 UI内容等相关内容需要很容易方便的跟新替换,不使用unity传统的热加载方式,也不想使用和H5等做混合APP的时候, 就用嵌入web来实现. 假如我想替换某个背景图,一般来 ...
- C# 分页方法
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web; ...
- iOS系统库头文件中NS_AVAILABLE相关
转载: NS_AVAILABLE_IOS(5_0) 这个方法可以在iOS5.0及以后的版本中使用,如果在比5.0更老的版本中调用这个方法,就会引起崩溃. NS_DEPRECATED_IOS(2_0, ...
- Struts框架的执行流程或原理
Struts2的执行流程如下: 1.浏览器发送请求,经过一系列的过滤器,到达StrutsPreapareAndExecteFilter 2.StrutsPrepareAndExectueFilter通 ...
- 地址解析协议ARP,网络层协议IP、ICMP协议
分析所用软件下载:Wireshark-win32-1.10.2.exe 阅读导览 1. 分析并且应用ARP协议 2.分析IP协议 3.分析ICMP协议 1.分析arp报文的格式与内容 (1)ping ...
- spring Controller 层注解获取 properties 里面的值
前言:最近在做一个项目,想要在 controller 层直接通过注解 @Value("")来获取 properties 里面配置的属性. 这个其实和 springmvc.sprin ...
- Spring学习手札(一)
Spring能做什么 1. 能根据配置文件创建及组装对象之间的依赖关系: 2. 面向切面编程,能帮助我们无耦合的实现日志记录,性能统计,安全控制等: 3. 提供第三方数据访问框架(如Hibernate ...
- 【代码笔记】iOS-自动成表格的效果
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> #import "LabelOnBackImag ...
- Oracle数据库拼接字符串
Oracle数据库中字符串拼接的实现 select count() from THT_HTFKJL where ht=1: 假如结果为:31.例如上面例子想要给结果拼接字符串有二种实现方法,第一种使用 ...