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的更多相关文章

  1. 剑指Offer——腾讯+360+搜狗校招笔试题+知识点总结

    剑指Offer--腾讯+360+搜狗校招笔试题+知识点总结 9.11晚7:00,腾讯笔试.选择题与编程.设计题单独计时. 栈是不是顺序存储的线性结构啊? 首先弄明白两个概念:存储结构和逻辑结构. 数据 ...

  2. 剑指Offer——京东校招笔试题+知识点总结

    剑指Offer--京东校招笔试题+知识点总结 笔试感言 经过一系列的笔试,发觉自己的基础知识还是比较薄弱的,尤其是数据结构和网络,还有操作系统.工作量还是很大的.做到精确制导的好方法就是在网上刷题,包 ...

  3. 剑指Offer——美团内推+校招笔试题+知识点总结

    剑指Offer--美团内推+校招笔试题+知识点总结 前言 美团9.9内推笔试.9.11校招笔试,反正就是各种虐,笔试内容如下: 知识点:图的遍历(DFS.BFS).进程间通信.二叉查找树节点的删除及中 ...

  4. 剑指Offer——CVTE校招笔试题+知识点总结(Java岗)

    剑指Offer(Java岗)--CVTE校招笔试题+知识点总结 2016.9.3 19:00参加CVTE笔试,笔试内容如下: 需要掌握的知识:Linux基本命令.网络协议.数据库.数据结构. 选择题 ...

  5. 2018 CVTE 前端校招笔试题整理

    昨天晚上(7.20)做了CVTE的前端笔试,总共三十道题,28道多选题,2道编程题 .做完了之后觉得自己基础还是不够扎实,故在此整理出答案,让自己能从中得到收获,同时给日后的同学一些参考. 首先说一下 ...

  6. 2016京东Android研发校招笔试题

    一.选择题汇总,具体的记不住啦.. 1.计网:ip的网络前缀.SNMP(报文组成):http://blog.csdn.net/shanzhizi/article/details/11606767 参考 ...

  7. 阿里2014校招笔试题(南大)——利用thread和sleep生成字符串的伪随机序列

    引言:题目具体描述记不大清了,大概是:Linux平台,利用线程调度的随机性和sleep的不准确性,生成一个各位均不相同的字符数组的伪随机序列.不得使用任何库函数.(这句记得清楚,当时在想线程库算不算, ...

  8. 微软2017校招笔试题3 registration day

    题目 It's H University's Registration Day for new students. There are M offices in H University, numbe ...

  9. 微软2017校招笔试题2 composition

    题目 Alice writes an English composition with a length of N characters. However, her teacher requires ...

随机推荐

  1. bootstrap、qjuery、插件 、字体网页

    http://www.bootcss.com/ 前端框架bootstrap http://www.fontawesome.com.cn/faicons/ 字体图标库 https://daneden.g ...

  2. DotNet Core 2.0部署后外网IP访问

    将DotNet Core2.0项目部署在Ubuntu上并且运行后,可以用localhost:5000来访问. 但是如果这时候用外网来访问就不行了. 这时候就有两种解决方案,第一种是用Nginx做代理实 ...

  3. JSONArray排序[收藏]

    问题 JSONArray中嵌套JSONObject, 对JSONArray进行排序 排序前: [{"id":1,"name":"ljw"}, ...

  4. WinForm中使用CrystalReport水晶报表——基础,分组统计,自定义数据源

    开篇 本篇文章主要是帮助刚开始接触CrystalReport报表的新手提供一个循序渐进的教程.该教程主要分为三个部分1)CrystalReport的基本使用方法:2)使用CrystalReport对数 ...

  5. 简易的canvas画板

    没事仿照windows画板工具用canvas实现了一个简易版的画板. html: <!doctype html> <html> <head> <meta ch ...

  6. 应用Python处理空间关系数据

    from osgeo import ogrimport jsonfrom geojson import loads, dumps, Feature, FeatureCollectionfrom sha ...

  7. adb调试桥(5037端口)

    path里添加路径:../platform 查看设备 adb devices 杀死adb:adb kill -server 启动adb:adb start- server adb不能启动解决办法: 1 ...

  8. windows下php使用zerophp

    官网地址:http://zeromq.org/ 下载windows版本安装(不过php可以不用安装,直接使用扩展包就可以了) 然后下载php的zmq扩展包:https://pecl.php.net/p ...

  9. PHP匹配当前传入是何种类型

    本文出至:新太潮流网络博客 /** * [is_string_regular_type 正则自动验证传入数据] * @E-mial wuliqiang_aa@163.com * @TIME 2017- ...

  10. Mysql 事务日志(Ib_logfile)

    mysql的innodb中事务日志ib_logfile(0/1) 概念:事务日志或称redo日志,在mysql中默认以ib_logfile0,ib_logfile1名称存在,可以手工修改参数,调节开启 ...