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

大家好,我是LvZi,今天带来笔试狂刷--Day9
在这里插入图片描述

一.添加逗号

题目链接:添加逗号

分析:

模拟

代码:

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());
    }
}

二.跳台阶

题目链接:跳台阶

分析:

基础的动态规划

代码:

// 注意类名必须为 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

代码:

    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;
    }

相关推荐

  1. 算法day39:树形DP

    2024-05-03 09:48:06       25 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-03 09:48:06       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-03 09:48:06       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-03 09:48:06       87 阅读
  4. Python语言-面向对象

    2024-05-03 09:48:06       96 阅读

热门阅读

  1. Nacos在微服务架构中如何支持服务发现和注册

    2024-05-03 09:48:06       40 阅读
  2. 区分Vue2和Vue3的配置读取(附Demo)

    2024-05-03 09:48:06       32 阅读
  3. 高可用系列四:loadbalancer 负载均衡

    2024-05-03 09:48:06       31 阅读
  4. 愚安科技安全工程师面经:

    2024-05-03 09:48:06       33 阅读
  5. Windows 系统运维常用命令

    2024-05-03 09:48:06       28 阅读
  6. 什么是Vuex它的作用是什么怎么用

    2024-05-03 09:48:06       36 阅读
  7. 新人天上下凡尘

    2024-05-03 09:48:06       30 阅读
  8. redis

    redis

    2024-05-03 09:48:06      32 阅读
  9. 孩子学编程,考级和竞赛二者间的比较

    2024-05-03 09:48:06       29 阅读
  10. Qt绘图与图形视图之Graphics View坐标系的简单介绍

    2024-05-03 09:48:06       30 阅读
  11. Unity功能——开发中逻辑坐标和世界坐标是什么?

    2024-05-03 09:48:06       30 阅读
  12. J2EE模式

    2024-05-03 09:48:06       27 阅读
  13. 企微私域运营软件:构建高效私域流量的利器

    2024-05-03 09:48:06       34 阅读