笔试狂刷--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;
    }
相关推荐
浅念-7 小时前
递归解题指南:LeetCode经典题全解析
数据结构·算法·leetcode·职场和发展·排序算法·深度优先·递归
Kiling_07047 小时前
Java集合进阶:Set与Collections详解
算法·哈希算法
智者知已应修善业7 小时前
【51单片机89C51及74LS273、74LS244组成】2022-5-28
c++·经验分享·笔记·算法·51单片机
洛水水8 小时前
【力扣100题】33.验证二叉搜索树
算法·leetcode·职场和发展
SimpleLearingAI8 小时前
聚类算法详解
算法·数据挖掘·聚类
刀法如飞9 小时前
Go 字符串查找的 20 种实现方式,用不同思路解决问题
算法·面试·程序员
Dlrb121111 小时前
C语言-指针数组与数组指针
c语言·数据结构·算法·指针·数组指针·指针数组·二级指针
WL_Aurora11 小时前
Python 算法基础篇之集合
python·算法
平行侠11 小时前
A15 工业路由器IP前缀高速检索与内存压缩系统
网络·tcp/ip·算法
阿旭超级学得完12 小时前
C++11包装器(function和bind)
java·开发语言·c++·算法·哈希算法·散列表