这几天在闭关修炼数据结构和算法, 也好几天没有更新博客了。
其实我也没学多久的算法, 满打满算牛客和leecode也就刷了四十来道题。
其实算法也没有我们一开始想象的那么难, 至少面试考的算法都还比较基础。
今天参加了华为OD的机试, 没有想象中的那么难, 但是还是熟练度的问题, 加上第一次考试有点紧张。前两题过了100%的用例, 用时一小时, 后面一个半小时都在刚第三题, 结果自己对递归的返回值处理不到位, 相当于没过吧, 晚上抽时间把代码调整了下, 应该是能正常跑过了。
现在把我经历的三道题分享出来, 有兴趣或者有建议的大佬的可以在我的博客留言。
建议看完题意后先自己思考怎么实现
本文题解只能实现功能, 并不是最优算法
ps: 一面 / 二面 也结束了, 我做了一些总结, 有兴趣可以看我这篇博客
华为od一面 / 二面复盘
扑克牌为 345678910JQKA 每种牌4张
输入是你现在的手牌, 还有已经打出去的牌
求剩下的牌中能凑出来的最大的顺子是多少
这里顺子的大小优先比较长度, 相同长度的顺子则比较牌面大小
比如:
你的手牌 3-3-3-8-8-8-8
已经打出的牌 4-4-5-5-6-6
最大的顺子是 9-10-J-Q-K-A
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//手牌
String s1 = sc.nextLine();
//出过的牌
String s2 = sc.nextLine();
//初始化
HashMap<String, Integer> count = new HashMap<>(16);
for (int i = 3; i <= 10; i++) {
count.put(String.valueOf(i), 4);
}
count.put("J", 4);
count.put("Q", 4);
count.put("K", 4);
count.put("A", 4);
String[] ss1 = s1.split("-");
for (int i = 0; i < ss1.length; i++) {
count.put(ss1[i], count.get(ss1[i]) - 1);
}
String[] ss2 = s2.split("-");
for (int i = 0; i < ss2.length; i++) {
count.put(ss2[i], count.get(ss2[i]) - 1);
}
//最大长度,队列, 读到0就输出前面的串
String[] sa = new String[]{"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
Deque<String> list = new LinkedList<>();
//从大的开始
String result = null;
int max = 0;
for (int i = sa.length - 1; i >= 0; i--) {
if (count.get(sa[i]) > 0) {
list.offerFirst(sa[i]);
} else {
//读到0了, 看能不能组成顺子
if (list.size() < 5) {
list.clear();
} else {
if (list.size() > max) {
max = list.size();
String tem = "";
while (list.size() > 0) {
tem += list.pollFirst() + "-";
}
result = tem.substring(0, tem.length() - 1);
}else{
list.clear();
}
}
}
}
//读到头 处理剩余数据
if (list.size() >= 5 && list.size() > max) {
max = list.size();
String tem = "";
while (list.size() > 0) {
tem += list.pollFirst() + "-";
}
result = tem.substring(0, tem.length() - 1);
}
if (max == 0) {
System.out.println("NO-CHAIN");
} else {
System.out.println(result);
}
}
}
给你一系列数字, 从中取出三个数字, 如果给的数字不到三个有几个取几个
求这几个数字拼接后的最小数字是多少。
比如:
给定 18,123,22,5,12,34,23,43,344,21
拼接后的最小数字是 12,18,5的拼接, 12185
这道题比较简单, 注意边界条件的处理, 而且数字可能超长
import java.math.BigDecimal;
import java.util.*;
public class Main2 {
static BigDecimal min;
static String[] choice = new String[3];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String[] ss = s.split(",");
if (ss.length == 0) {
System.out.println(0);
return;
} else if (ss.length == 1) {
System.out.println(ss[0]);
return;
} else if (ss.length == 2) {
System.out.println(Math.min(Integer.parseInt(ss[0] + ss[1]),
Integer.parseInt(ss[1] + ss[0])));
return;
}
//数组长度为3及以上,取三个长度最短的, 相等的都取出来
//截取0后长度最短的
PriorityQueue<String> queue = new PriorityQueue<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return new BigDecimal(o1).compareTo(new BigDecimal(o2));
}
});
for (int i = 0; i < ss.length; i++) {
queue.offer(ss[i]);
}
choice[0] = queue.poll();
choice[1] = queue.poll();
choice[2] = queue.poll();
//最小数字
min = new BigDecimal(choice[0] + choice[1] + choice[2]);
int[] ids = new int[3];
for (int i = 0; i < 3; i++) {
int[] idsCopy = Arrays.copyOf(ids, ids.length);
idsCopy[i] = 1;
build(idsCopy, 0, i, "");
}
System.out.println(min);
}
public static void build(int[] ids, int count, int index, String s) {
s += choice[index];
if (count == 2) {
BigDecimal bigDecimal = new BigDecimal(s);
if (bigDecimal.compareTo(min) < 0) {
min = bigDecimal;
}
return;
}
for (int i = 0; i < 3; i++) {
int[] idsCopy = Arrays.copyOf(ids, ids.length);
if (idsCopy[i] != 1) {
idsCopy[i] = 1;
build(idsCopy, count + 1, i, s);
}
}
}
}
给定一个m*n的矩阵, 矩阵中数字 0 代表空地, 可通过, 1 代表障碍, 不可通过, 数字 2 代表两个人所在的位置, 3代表聚餐点所在的位置。
求两个人同时能够到达的聚餐点有多少个?
比如:
给定, 第一行的两个数字分别是m, n
4 4
2 1 0 3
0 1 2 1
0 3 0 0
0 0 0 0
求得 2
public class Main3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String[] ss = s.split(" ");
//长
m = Integer.parseInt(ss[0]);
//宽
n = Integer.parseInt(ss[1]);
List<Pos> eatPos = new ArrayList<>();
int[][] serched = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int point = sc.nextInt();
if (point == 3) {
eatPos.add(new Pos(i, j));
}
if (point == 1) {
serched[i][j] = 1;
}
arr[i][j] = point;
}
}
if (eatPos.size() == 0) {
System.out.println(0);
return;
}
for (Pos eat : eatPos) {
//从聚餐点可以到达几个人, 如果能到达两个人, 则可达聚餐点+1
int personFlag = 0;
Pos left = eat.left();
Pos right = eat.right();
Pos up = eat.up();
Pos down = eat.down();
serched[eat.x][eat.y] = 1;
//上下左右
personFlag = search(left.x, left.y, personFlag, serched);
if (personFlag == 2) {
result++;
continue;
}
personFlag = search(right.x, right.y, personFlag, serched);
if (personFlag == 2) {
result++;
continue;
}
personFlag = search(up.x, up.y, personFlag, serched);
if (personFlag == 2) {
result++;
continue;
}
personFlag = search(down.x, down.y, personFlag, serched);
if (personFlag == 2) {
result++;
}
}
System.out.println(result);
}
static int m;
static int n;
static int result = 0;
static int[][] arr = new int[99][99];
/**
* @param x 找的下一个点
* @param y 找的下一个点
* @param personFlag 记录已经找到的人数, 找到2个人就成功, 给答案+1
* @param searched 记录已经找过的点
*/
public static int search(int x, int y, int personFlag, int[][] searched) {
if (!isValid(x, y, searched)) {
return personFlag;
}
if (arr[x][y] == 2) {
personFlag++;
}
if (personFlag == 2) {
return personFlag;
}
int[][] serchedCopy = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
serchedCopy[i][j] = searched[i][j];
}
}
serchedCopy[x][y] = 1;
Pos current = new Pos(x, y);
Pos left = current.left();
Pos right = current.right();
Pos up = current.up();
Pos down = current.down();
//上下左右
personFlag = search(left.x, left.y, personFlag, serchedCopy);
if (personFlag == 2) {
return personFlag;
}
personFlag = search(right.x, right.y, personFlag, serchedCopy);
if (personFlag == 2) {
return personFlag;
}
personFlag = search(up.x, up.y, personFlag, serchedCopy);
if (personFlag == 2) {
return personFlag;
}
personFlag = search(down.x, down.y, personFlag, serchedCopy);
return personFlag;
}
/**
* 是否在边界内而且没找过
*/
public static boolean isValid(int x, int y, int[][] searched) {
if (x >= m || x < 0 || y >= n || y < 0) {
return false;
}
if (searched[x][y] == 1) {
return false;
}
return true;
}
static class Pos {
int x;
int y;
public Pos(int x, int y) {
this.x = x;
this.y = y;
}
public Pos left() {
return new Pos(x, y - 1);
}
public Pos right() {
return new Pos(x, y + 1);
}
public Pos up() {
return new Pos(x - 1, y);
}
public Pos down() {
return new Pos(x + 1, y);
}
}
public static void print(int[][] arr) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
上面的解法非常的粗糙, 优点在于方便理解回溯的思想
实际上基于动态规划我们可以做一些优化,
一个是探索过的路径我们不需要重复探索, 因为我们都是从上下左右四个方向探索的, 所以我引入了searched数组, 用来记录已经探索过的坐标
第二个是剪枝操作, 只要在探索的过程中, 从一个聚餐点(数字3)出发已经能到达两个人(数字2), 那么我们就不需要继续探索, 直接return
我把return统一放到回溯方法的前面, 代码上要简洁一些
还有一个就是备忘录, 比如我们从一个聚餐点出发, 探索完这个聚餐点的可达路径后, 如果我们能够同时到达两个人, 那么很明显, 所有我们探索过程中探索过的坐标都能到达两个人(这体现了动态规划重复子问题的特点)
那么我们可以把searched数组缓存起来, 探索其他聚餐点的时候先从缓存获取即可。如果最终只能到达1个人, 或者0个人, 同样的, 我们也是缓存起来就行, 因为结果是一样的。
这样, 最终我们所有的点只需要探索一遍, 也就是时间复杂度O(mn), 空间复杂度我们对每个3节点引入了searched数组记录探过的路径, 引入一个cache数组来缓存子问题结果, 空间复杂度是O(mn)
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String[] ss = s.split(" ");
//长
m = Integer.parseInt(ss[0]);
//宽
n = Integer.parseInt(ss[1]);
List<Pos> eatPos = new ArrayList<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int point = sc.nextInt();
if (point == 3) {
eatPos.add(new Pos(i, j));
}
arr[i][j] = point;
}
}
if (eatPos.size() == 0) {
System.out.println(0);
return;
}
for (Pos eat : eatPos) {
//从聚餐点可以到达几个人, 如果能到达两个人, 则可达聚餐点+1
//初始化
int personFlag = 0;
int[][] searched = new int[m][n];
//上下左右
personFlag = search(eat.x, eat.y, personFlag, searched);
if (personFlag == 2) {
result++;
}
//缓存起来
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cache[i][j] = searched[i][j];
}
}
}
System.out.println(result);
}
static int m;
static int n;
static int result = 0;
static int[][] arr = new int[99][99];
//缓存, 只要最后能到达2个人, 那么所有搜索走过的路径都能到达2个人
static int[][] cache = new int[99][99];
/**
* @param x 找的下一个点
* @param y 找的下一个点
* @param personFlag 记录已经找到的人数, 找到2个人就成功, 给答案+1
* @param searched 记录已经找过的点
*/
public static int search(int x, int y, int personFlag, int[][] searched) {
//不合法/找过的路不走
if (!isValid(x, y, searched)) {
return personFlag;
}
if (cache[x][y] == 2) {
return cache[x][y];
}
searched[x][y] = 1;
//找到人, 人数+1
if (arr[x][y] == 2) {
personFlag++;
}
//人数=2, 结束
if (personFlag == 2) {
return personFlag;
}
Pos current = new Pos(x, y);
Pos left = current.left();
Pos right = current.right();
Pos up = current.up();
Pos down = current.down();
//上下左右
personFlag = search(left.x, left.y, personFlag, searched);
personFlag = search(right.x, right.y, personFlag, searched);
personFlag = search(up.x, up.y, personFlag, searched);
personFlag = search(down.x, down.y, personFlag, searched);
return personFlag;
}
/**
* 是否在边界内而且没找过
*/
public static boolean isValid(int x, int y, int[][] searched) {
if (x >= m || x < 0 || y >= n || y < 0) {
return false;
}
return arr[x][y] != 1 && searched[x][y] != 1;
}
static class Pos {
int x;
int y;
public Pos(int x, int y) {
this.x = x;
this.y = y;
}
public Pos left() {
return new Pos(x, y - 1);
}
public Pos right() {
return new Pos(x, y + 1);
}
public Pos up() {
return new Pos(x - 1, y);
}
public Pos down() {
return new Pos(x + 1, y);
}
}
}
ps:看到这里,我们部门最近还有人力需求。部门业务是华为云计算,位置在杭州研究所,有意向的可以私聊。
目录一.加解密算法数字签名对称加密DES(DataEncryptionStandard)3DES(TripleDES)AES(AdvancedEncryptionStandard)RSA加密法DSA(DigitalSignatureAlgorithm)ECC(EllipticCurvesCryptography)非对称加密签名与加密过程非对称加密的应用对称加密与非对称加密的结合二.数字证书图解一.加解密算法加密简单而言就是通过一种算法将明文信息转换成密文信息,信息的的接收方能够通过密钥对密文信息进行解密获得明文信息的过程。根据加解密的密钥是否相同,算法可以分为对称加密、非对称加密、对称加密和非
华为OD机试题本篇题目:明明的随机数题目输入描述输出描述:示例1输入输出说明代码编写思路最近更新的博客华为od2023|什么是华为od,od薪资待遇,od机试题清单华为OD机试真题大全,用Python解华为机试题|机试宝典【华为OD机试】全流程解析+经验分享,题型分享,防作弊指南华为o
system-view进入系统视图quit退到系统视图sysname交换机命名vlan20创建vlan(进入vlan20)displayvlan显示vlanundovlan20删除vlan20displayvlan20显示vlan里的端口20Interfacee1/0/24进入端口24portlink-typeaccessvlan20把当前端口放入vlan20undoporte1/0/10删除当前VLAN端口10displaycurrent-configuration显示当前配置02配置交换机支持TELNETinterfacevlan1进入VLAN1ipaddress192.168.3.100
1.在Python3中,下列关于数学运算结果正确的是:(B)a=10b=3print(a//b)print(a%b)print(a/b)A.3,3,3.3333...B.3,1,3.3333...C.3.3333...,3.3333...,3D.3.3333...,1,3.3333...解析: 在Python中,//表示地板除(向下取整),%表示取余,/表示除(Python2向下取整返回3)2.如下程序Python2会打印多少个数:(D)k=1000whilek>1: print(k)k=k/2A.1000 B.10C.11D.9解析: 按照题意每次循环K/2,直到K值小于等
1.问题描述使用Python的turtle(海龟绘图)模块提供的函数绘制直线。2.问题分析一幅复杂的图形通常都可以由点、直线、三角形、矩形、平行四边形、圆、椭圆和圆弧等基本图形组成。其中的三角形、矩形、平行四边形又可以由直线组成,而直线又是由两个点确定的。我们使用Python的turtle模块所提供的函数来绘制直线。在使用之前我们先介绍一下turtle模块的相关知识点。turtle模块提供面向对象和面向过程两种形式的海龟绘图基本组件。面向对象的接口类如下:1)TurtleScreen类:定义图形窗口作为绘图海龟的运动场。它的构造器需要一个tkinter.Canvas或ScrolledCanva
所有题目均有五种语言实现。C实现目录、C++实现目录、Python实现目录、Java实现目录、JavaScript实现目录题目n行m列的矩阵,每个位置上有一个元素你可以上下左右行走,代价是前后两个位置元素值差的绝对值.另外,你最多可以使用一次传送阵(只能从一个数跳到另外一个相同的数)求从走上角走到右下角最少需要多少时间。输入描述:第一行两个整数n,m,分别代表矩阵的行和列。后面n行,每行m个整数,分别代表矩阵中的元素。输出描述:一个整数,表示最少需要多少时间。
西安华为OD面试体验开始投简历技术面试进展工作进展开始投简历去年一整年一直在考研和工作之间纠结,感觉自己的状态好像当时的疫情一样差劲。之前刚毕业的时候投了个大厂的简历,结果一面写算法的时候太拉跨了,虽然知道时dfs但是代码熟练度不够,放在平时给足时间自己可以调试通过,但是熟练度不够那面试当时就写不出来被刷了。说真的算法学到后期我感觉最重要的是熟练度和背板子(对于我这种普通玩家来说),面试题如果一上来短时间内想不出思路就完蛋了。然后由于当时找的工作不是很理想就又想考研了。但是考研是有风险的,我自我感觉自己可能冲不上那个学校,而找工作一个没成可以继续找嘛。本着抱着试试看的态度在boss上投了简历,
我一直在尝试用Ruby实现Luhn算法。我一直在执行以下步骤:该公式根据其包含的校验位验证数字,该校验位通常附加到部分帐号以生成完整帐号。此帐号必须通过以下测试:从最右边的校验位开始向左移动,每第二个数字的值加倍。将乘积的数字(例如,10=1+0=1、14=1+4=5)与原始数字的未加倍数字相加。如果总模10等于0(如果总和以零结尾),则根据Luhn公式该数字有效;否则无效。http://en.wikipedia.org/wiki/Luhn_algorithm这是我想出的:defvalidCreditCard(cardNumber)sum=0nums=cardNumber.to_s.s
下面是我写的一个计算斐波那契数列中的值的方法:deffib(n)ifn==0return0endifn==1return1endifn>=2returnfib(n-1)+(fib(n-2))endend它工作到n=14,但在那之后我收到一条消息说程序响应时间太长(我正在使用repl.it)。有人知道为什么会这样吗? 最佳答案 Naivefibonacci进行了大量的重复计算-在fib(14)fib(4)中计算了很多次。您可以将内存添加到您的算法中以使其更快:deffib(n,memo={})ifn==0||n==1returnnen
为了防止在迁移到生产站点期间出现数据库事务错误,我们遵循了https://github.com/LendingHome/zero_downtime_migrations中列出的建议。(具体由https://robots.thoughtbot.com/how-to-create-postgres-indexes-concurrently-in概述),但在特别大的表上创建索引期间,即使是索引创建的“并发”方法也会锁定表并导致该表上的任何ActiveRecord创建或更新导致各自的事务失败有PG::InFailedSqlTransaction异常。下面是我们运行Rails4.2(使用Acti