笔试狂刷--Day9(模拟 + dp + 规律)

大家好,我是LvZi ,今天带来笔试狂刷--Day9

一.添加逗号

题目链接:添加逗号

分析:

模拟

代码:

java 复制代码
import java.util.*;


// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        char[] tmp = String.valueOf(n).toCharArray();
        char[] s = new char[tmp.length];

        for (int i = 0; i < tmp.length; i++) {
            s[i] = tmp[tmp.length - 1 - i];
        }

        StringBuffer sb = new StringBuffer();
        int j = 0;

        while (j < s.length) {
            if ((j != 0) && ((j % 3) == 0)) {
                sb.append(",");
            }

            sb.append(s[j]);
            j++;
        }

        System.out.println(sb.reverse().toString());
    }
}

二.跳台阶

题目链接:跳台阶

分析:

基础的动态规划

代码:

java 复制代码
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();

        if (n == 1) {
            System.out.println(1);

            return;
        }

        if (n == 2) {
            System.out.println(2);

            return;
        }

        int a = 1;
        int b = 2;
        int c = 0;

        for (int i = 3; i <= n; i++) {
            c = a + b;
            a = b;
            b = c;
        }

        System.out.println(c);
    }
}

三.

题目链接:扑克牌顺子

分析:

找规律

如果能构成顺子,则所有非零元素一定满足

  • 不存在重复元素
  • max - min <= 4

代码:

java 复制代码
    public boolean IsContinuous (int[] numbers) {
        // write code here
        boolean[] hash = new boolean[14];

        int max = 0, min = 14;
        for(int x : numbers) {
            if(x != 0) {
                if(hash[x]) return false;// 同一数字出现两次  一定不是顺子
                hash[x] = true;
                max = Math.max(max,x);
                min = Math.min(min,x);
            }
        }

        return max - min <= 4;
    }
相关推荐
wearegogog1234 小时前
基于 MATLAB 的卡尔曼滤波器实现,用于消除噪声并估算信号
前端·算法·matlab
一只小小汤圆4 小时前
几何算法库
算法
Evand J4 小时前
【2026课题推荐】DOA定位——MUSIC算法进行多传感器协同目标定位。附MATLAB例程运行结果
开发语言·算法·matlab
leo__5205 小时前
基于MATLAB的交互式多模型跟踪算法(IMM)实现
人工智能·算法·matlab
忆锦紫5 小时前
图像增强算法:Gamma映射算法及MATLAB实现
开发语言·算法·matlab
t198751285 小时前
基于自适应Chirplet变换的雷达回波微多普勒特征提取
算法
guygg885 小时前
采用PSO算法优化PID参数,通过调用Simulink和PSO使得ITAE标准最小化
算法
老鼠只爱大米5 小时前
LeetCode算法题详解 239:滑动窗口最大值
算法·leetcode·双端队列·滑动窗口·滑动窗口最大值·单调队列
mit6.8246 小时前
序列化|质数筛|tips|回文dp
算法
rgeshfgreh6 小时前
C++字符串处理:STL string终极指南
java·jvm·算法