笔试狂刷--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;
    }
相关推荐
风中的微尘19 小时前
39.网络流入门
开发语言·网络·c++·算法
西红柿维生素20 小时前
JVM相关总结
java·jvm·算法
ChillJavaGuy1 天前
常见限流算法详解与对比
java·算法·限流算法
sali-tec1 天前
C# 基于halcon的视觉工作流-章34-环状测量
开发语言·图像处理·算法·计算机视觉·c#
你怎么知道我是队长1 天前
C语言---循环结构
c语言·开发语言·算法
艾醒1 天前
大模型面试题剖析:RAG中的文本分割策略
人工智能·算法
纪元A梦1 天前
贪心算法应用:K-Means++初始化详解
算法·贪心算法·kmeans
_不会dp不改名_1 天前
leetcode_21 合并两个有序链表
算法·leetcode·链表
mark-puls1 天前
C语言打印爱心
c语言·开发语言·算法
Python技术极客1 天前
将 Python 应用打包成 exe 软件,仅需一行代码搞定!
算法