算法: 模拟题目练习

embedded/2024/10/20 9:49:50/

文章目录

  • 模拟
    • 替换所有的问号
    • 提莫攻击
    • Z 字形变换
    • 外观数列
    • 数青蛙
  • 总结


模拟

替换所有的问号

在这里插入图片描述
按照题目的要求写代码即可~

    public String modifyString(String ss) {int n = ss.length();if (n == 1) {return "a";}char[] s = ss.toCharArray();for (int i = 0; i < n; i++) {if (s[i] == '?') {for (char ch = 'a'; ch <= 'z'; ch++) {if (i == 0 && ch != s[i + 1]) {// 第一个s[i] = ch;} else if (i == n - 1 && ch != s[i - 1]) {// 最后一个s[i] = ch;}if (0 < i && i < n - 1 && ch != s[i + 1] && ch != s[i - 1]) {// 中间s[i] = ch;}}}}return String.valueOf(s);}

题解写的更加简洁.

题解代码:

    public String modifyString(String ss) {int n = ss.length();char[] s = ss.toCharArray();for (int i = 0; i < n; i++) {if (s[i] == '?') {for (char ch = 'a'; ch <= 'z'; ch++) {if ((i == 0 || s[i - 1] != ch) && (i == n - 1 || s[i + 1] != ch)) {s[i] = ch;break;}}}}return String.valueOf(s);}

提莫攻击

在这里插入图片描述
草稿:
在这里插入图片描述

    public int findPoisonedDuration(int[] timeSeries, int duration) {int tmp = timeSeries[0] + duration - 1;int sum = duration;for (int i = 1; i < timeSeries.length; i++) {if (tmp >= timeSeries[i]) {sum += timeSeries[i] - timeSeries[i - 1];} else {sum += duration;}tmp = timeSeries[i] + duration - 1;}return sum;}

题解代码:
草图:
在这里插入图片描述

    public int findPoisonedDuration(int[] timeSeries, int duration) {int sum = 0;for (int i = 1; i < timeSeries.length; i++) {int tmp = timeSeries[i] - timeSeries[i - 1];if (tmp > duration) {sum += duration;} else {sum += tmp;}}return sum + duration;}

Z 字形变换

在这里插入图片描述
虽然过了,但是稀里糊涂地过了~

开头和结尾都好说,主要是中间,不知道为啥要 - 2*i.

规律就是这样的~

做题思路就是:

  • 题目让干啥,我们就干啥
  • 画图找规律~

坑:

  • numRows 可能为 1 .
  • 放中间元素时,容易越界.

代码:

public String convert(String ss, int numRows) {if (numRows == 1)return ss;char[] s = ss.toCharArray();int n = s.length;char[] ret = new char[n];int gap = (numRows - 1) * 2;int k = 0;// 开头for (int j = 0; j < n; j += gap) {ret[k++] = s[j];}// 中间for (int i = 1; i <= numRows - 2; i++) {for (int j = i; j < n; j += gap) {ret[k++] = s[j];// 这里为啥 - i*2 就对了?int mid = j + gap - i * 2;if (mid < n) {ret[k++] = s[mid];}}}// 结尾for (int j = numRows - 1; j < n; j += gap) {ret[k++] = s[j];}return String.valueOf(ret);}

外观数列

在这里插入图片描述

终于过了~
不知道为啥,自己写的代码返回的结果一直只有两个数. 在这上面耗了20多分钟.
最后全删了.心态崩了呀.
吃完饭回来,重写了一遍,只用了不到6分钟就写出来了.

坑:

  • 不用考虑怎么替换的问题,最开始我也被题目带偏了.如果用替换来写,需要考虑的情况就复杂了. 其实直接新建一个字符串,不断向这个字符串后面拼接就行了.
    public String countAndSay(int n) {StringBuilder ret = new StringBuilder("1");for (int i = 1; i < n; i++) {StringBuilder tmp = new StringBuilder();int len = ret.length();int left = 0, right = 0;while (right < len) {while (right < len && ret.charAt(left) == ret.charAt(right)) {right++;}tmp.append(right - left);tmp.append(ret.charAt(left));left = right;}ret = tmp;}return ret.toString();}

数青蛙

在这里插入图片描述

最后一个测试用例卡了好久.

坑:

  • 如何判断给出的字符串不是 “croak” 的有效组合? 可以用最后的 sum 来判断,如果 sum 没有减到0,那就说明字符串不完整.
    public int minNumberOfFrogs(String croakOfFrogs) {if (croakOfFrogs.length() < 5 || croakOfFrogs.length() % 5 != 0) {return -1;}int sum = 0;int ret = 0;char[] str = {'c', 'r', 'o', 'a', 'k'};HashMap<Character, Integer> hash = new HashMap<>();HashMap<Character, Character> hash2 = new HashMap<>();for (int i = 1; i < 5; i++) {hash2.put(str[i], str[i - 1]);}int n = croakOfFrogs.length();for (int i = 0; i < n; i++) {char ch = croakOfFrogs.charAt(i);hash.put(ch, hash.getOrDefault(ch, 0) + 1);if (ch != 'c' && hash.getOrDefault(ch, 0) > hash.getOrDefault(hash2.get(ch), 0)) {return -1;}if (ch == 'c') {sum++;} else if (ch == 'k') {ret = Math.max(ret, sum);sum--;}}if (sum != 0) return -1;return ret;}

看了题解后又自己写了一遍:
在这里插入图片描述

    public int minNumberOfFrogs(String croakOfFrogs) {String str = "croak";HashMap<Character, Integer> hashIndex = new HashMap<>();for (int i = 0; i < 5; i++) {hashIndex.put(str.charAt(i), i);}HashMap<Character, Integer> hashCount = new HashMap<>();int n = croakOfFrogs.length();for (int i = 0; i < n; i++) {char ch = croakOfFrogs.charAt(i);if (ch != 'c') {// r,o,a,kchar prev = str.charAt(hashIndex.get(ch) - 1);int pervCount = hashCount.getOrDefault(prev, 0);if (pervCount > 0) {hashCount.put(prev, pervCount - 1);hashCount.put(ch, hashCount.getOrDefault(ch, 0) + 1);} else if (pervCount <= 0) {return -1;}} else {// cif (hashCount.getOrDefault('k', 0) > 0) {hashCount.put('k', hashCount.get('k') - 1);}hashCount.put(ch, hashCount.getOrDefault(ch, 0) + 1);}}// 检验给出的字符串是不是 "croak" 的有效组合。for (int i = 0; i < 4; i++) {if (hashCount.get(str.charAt(i)) != 0) {return -1;}}return hashCount.get('k');}

题解代码:

  • 使用数组替代了 hash 表.
    public int minNumberOfFrogs(String c) {char[] croakOfFrogs = c.toCharArray();String str = "croak";int n = str.length();int[] hash = new int[n];HashMap<Character, Integer> index = new HashMap<>();// 建立字母和下标的关系for (int i = 0; i < n; i++) {index.put(str.charAt(i), i);}for (char ch : croakOfFrogs) {if (ch != 'c') {// r,o,a,kint i = index.get(ch);if (hash[i - 1] > 0) {hash[i - 1]--;hash[i]++;} else {return -1;}} else {// cif (hash[n - 1] > 0)hash[n - 1]--;hash[0]++;}}for (int i = 0; i < n - 1; i++) {if (hash[i] != 0) return -1;}return hash[n - 1];}

看题解看到了一个 if else 大法 :

public int minNumberOfFrogs(String croakOfFrogs) {int c,r,o,a,k;c = 0; r = 0; o = 0; a = 0;k = 0;char []chars = croakOfFrogs.toCharArray();int res = 0;for(int i = 0;i < chars.length;i++){if(chars[i] == 'c'){if(k > 0){k--;}else{res++;}c++;}else if(chars[i] == 'r'){c--;r++;}else if(chars[i] == 'o'){r--;o++;}else if(chars[i] == 'a'){o--;a++;}else if(chars[i] == 'k'){a--;k++;}if(c < 0 || r < 0 || o < 0 || a < 0){break;}}if(c != 0 || r != 0 || o != 0 || a != 0){return -1;}return res;}

总结

  • 做模拟题时, 题目说啥咱干啥~
  • 有难度的模拟题需要我们找规律.
  • 画图是个好东西.

本文到这里就结束啦~

在这里插入图片描述


http://www.ppmy.cn/embedded/128962.html

相关文章

Java基础-CompletableFuture

CompletableFuture 是 Java 8 中引入的一个实现异步编程类。提供了一组丰富的方法来处理异步操作和多个任务的结果。 执行任务 可以使用CompletableFuture.supplyAsync()或者CompletableFuture.runAsync创建CompletableFuture对象&#xff0c;并执行任务。 supplyAsync <U&g…

MATLAB图片拼接配准系统

应用背景 图像配准现在已成为数字图像处理的研究热点&#xff0c;方法繁多&#xff0c;站在时代的前沿。图像配准多采用基于图像特征点的方法&#xff0c;这种方法易于用计算机处理并且容易实现人机交互&#xff0c;其重点在于如何提取图像上的有效特征点。 对图像拼接技术的…

促进绿色可持续发展 能源环保管理重中之重

在全球经济环境发展、资源逐渐减少的背景下&#xff0c;环境保护已成为全球共识&#xff0c;而工业作为经济发展的重要支柱&#xff0c;其环保监测的实现至关重要。以下是对工业重点环保监测实现方式的详细探讨&#xff1a; 18721098782 WPP 一、构建国家级环境监测网络 …

【优选算法】——双指针(下篇)!

&#x1f308;个人主页&#xff1a;秋风起&#xff0c;再归来~ &#x1f525;系列专栏&#xff1a;C刷题算法总结 &#x1f516;克心守己&#xff0c;律己则安 目录 1、有效三角形的个数 2、查找总价值为目标值的两个商品 3、三数之和 4、四数之和 5、完结散花 1、有…

tensorflow + pygame 手写数字识别的小游戏

起因&#xff0c; 目的: 很久之前&#xff0c;一个客户的作业&#xff0c;我帮忙写的。 今天删项目&#xff0c;觉得比较简洁&#xff0c;发出来给大家看看。 效果图: 1. 训练模型的代码 import sys import tensorflow as tf# Use MNIST handwriting dataset mnist tf.kera…

uiautomatorviewer安卓9以上正常使用及问题处理

一、安卓9以上使用uiautomatorviewer问题现象 打开Unexpected error while obtaining UI hierarchy 问题详情 Unexpected error while obtaining UI hierarchy java.lang.reflect.InvocationTargetException 二、问题处理 需要的是替换对应D:\software\android-sdk-windows…

PicoQuant GmbH公司Dr. Christian Oelsner到访东隆科技

昨日&#xff0c;德国PicoQuant公司的光谱和显微应用和市场专家Dr.Christian Oelsner莅临武汉东隆科技有限公司。会议上Dr. Christian Oelsner就荧光寿命光谱和显微技术的最新研究和应用进行了深入的交流与探讨。此次访问不仅加强了两家公司在高科技领域的合作关系&#xff0c;…

HDLBits参考答案合集

关注 望森FPGA 查看更多FPGA资讯 这是望森的第 26 期分享 作者 | 望森 来源 | 望森FPGA 本节内容是HDLBits参考答案合集的索引。 恭喜HDLBits合集完结~ 敬请关注新的专栏内容“FPGA理论基础” 前言 FPGA新手必用&#xff0c;Verilog HDL编程学习网站推荐 —— HDLBits_veri…