首页 > 其他分享 >小于n的最大数,记一道字节面试题

小于n的最大数,记一道字节面试题

时间:2024-10-19 19:51:05浏览次数:1  
标签:面试题 字节 最大数 int System maxN Expected new out

package client;

import java.util.Arrays;

public class MainTest {
    public static void main(String[] args) {
        // Test case examples
        System.out.println(maxN(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 235)); // Expected: 235
        System.out.println(maxN(new int[]{0, 2, 4, 6, 8}, 357)); // Expected: 288
        System.out.println(maxN(new int[]{1, 2, 3}, 987654)); // Expected: 333333
        System.out.println(maxN(new int[]{1}, 1111)); // Expected: 1111
        System.out.println(maxN(new int[]{1, 3, 5, 7, 9}, 2468)); // Expected: 1999
        System.out.println(maxN(new int[]{1, 2, 3, 4, 5}, 654)); // Expected: 555
        System.out.println(maxN(new int[]{3, 5, 8}, 572)); // Expected: 558
        System.out.println(maxN(new int[]{2, 3}, 100)); // Expected: 33
        System.out.println(maxN(new int[]{5}, 123)); // Expected: 55
        System.out.println(maxN(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 0)); // Expected: 0
        System.out.println(maxN(new int[]{2,4,9},23121));// Expected: 22999
    }

    private static String result = null;

    public static int maxN(int[] digits, int n) {
        Arrays.sort(digits);
        result = null;
        dfs(digits, String.valueOf(n), new StringBuilder(), 0, true);
        return Integer.parseInt(result);
    }

    private static void dfs(int[] digits, String target, StringBuilder current, int index, boolean isLimit) {
        if (result != null) {
            return;
        }
        if (index == target.length()) {
            result = current.toString();
            return;
        }

        if (!isLimit) {
            current.append(digits[digits.length - 1]);
            dfs(digits, target, current, index + 1, false);
        } else {
            boolean hasValidDigit = false;
            int limit = target.charAt(index) - '0';
            for (int i = digits.length - 1; i >= 0; i--) {
                if (digits[i] <= limit) {
                    hasValidDigit = true;
                    current.append(digits[i]);
                    dfs(digits, target, current, index + 1, digits[i] == limit);
                    current.deleteCharAt(current.length() - 1);
                }
            }
            if (!hasValidDigit) {
                dfs(digits, target, current, index + 1, false);
            }
        }
    }
}

标签:面试题,字节,最大数,int,System,maxN,Expected,new,out
From: https://www.cnblogs.com/jjlCode/p/18485153

相关文章

  • Redis相关面试题
    Redis为什么快?1.纯内存KV操作Redis的操作都是基于内存的,CPU不是Redis性能瓶颈,,Redis的瓶颈是机器内存和网络带宽。在计算机的世界中,CPU的速度是远大于内存的速度的,同时内存的速度也是远大于硬盘的速度。redis的操作都是基于内存的,绝大部分请求是纯粹的内存操作,非常......
  • 码城|第2期一分钟吃透Java面试题
     ......
  • 我希望gid字段是表的自增主键,数据类型采用8个字节的无符号整形数据,并且我要指定自增的
    Sir,为了将gid字段设置为自增主键并且采用8字节无符号整型数据类型(即BIGINTUNSIGNED),您可以使用MySQL的AUTO_INCREMENT机制,并且可以通过ALTERTABLE来指定自增的初始值。具体实现步骤如下:1.字段定义字段名称:gid数据类型:BIGINTUNSIGNED(8字节无符号整数)自增......
  • Java最全面试题->Java基础面试题->JavaWeb面试题->Cookie/Session面试题
    Cookie/Session下边是我自己整理的面试题,基本已经很全面了,想要的可以私信我,我会不定期去更新思维导图哪里不会点哪里什么是Cookie?HTTPCookie(也叫WebCookie或浏览器Cookie)是服务器发送到用户浏览器并保存在本地的一小块数据,它会在浏览器下次向同一服务器再发起请求......
  • IO流读写文件(字节流(单个字节,字节数组),字节缓冲流(..),字符流(..),字符缓冲流(..))
    IO流【输入输出流】:按照流向划分:输入流:外部数据->java程序输出流:java程序->外部数据按照数据类型划分【根据使用记事本打开是否能够看懂来决定】:字节流【万能流】:字节输出流:......
  • 面试题速刷 - 实战会碰到的一些问题
    页面如何进行首屏优化?路由懒加载服务端渲染SSR只获取HTML就可以,里面包含data。APP预取(啥东西)APP结合H5、结合JSbridge分页图片懒加载lazyloadHybrid总结:后端一次性返回10w条数据,你会如何渲染?本身后端设计方案的设计就不合理!非要的话......自定义中间......
  • C++ 基础-面试题01(C和C++区别、C结构体和C++结构体区别、C和C++ static区别、a和&a区
    1.C和C++的区别特性CC++编程范式面向过程编程面向对象编程+面向过程编程+泛型编程类和对象不支持类和对象支持类和对象,封装、继承、多态等特性标准库标准库有限,如stdio.h、stdlib.h丰富的标准库,如STL(容器、算法)函数和运算符重载不支持支持内存管理手动管理,使用malloc......
  • C++ 基础-面试题02(final和override关键字、sizeof和strlen区别、strcpy、sprintf 与me
    1.final和override关键字在C++中,final和override关键字是在面向对象编程中用于处理类的继承和多态的。它们主要用于管理派生类和虚函数,提供额外的安全性和代码可读性,防止意外的函数重写或错误的重载行为。1.final关键字final关键字用于防止进一步的继承或函数重......
  • 解析“60k”大佬的19道C#面试题(上)
    解析“60k”大佬的19道C#面试题(上) 解析“60k”大佬的19道C#面试题(上)先略看题目:请简述async函数的编译方式请简述Task状态机的实现和工作机制请简述await的作用和原理,并说明和GetResult()有什么区别Task和Thread有区别吗?如果有请简述区别简述yield的作用利用IEnumerab......
  • 力扣面试题02.07.链表相交
    题目链接:面试题02.07.链表相交-力扣(LeetCode)给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。图示两个链表在节点 c1 开始相交:题目数据 保证 整个链式结构中不存在环。注意,函数返回结果......