753. 破解保险箱

有一个需要密码才能打开的保险箱。密码是 n 位数, 密码的每一位是 k 位序列 0, 1, …, k-1 中的一个 。

你可以随意输入密码,保险箱会自动记住最后 n 位输入,如果匹配,则能够打开保险箱。

举个例子,假设密码是 “345”,你可以输入 “012345” 来打开它,只是你输入了 6 个字符.

请返回一个能打开保险箱的最短字符串。

示例1:

输入: n = 1, k = 2

输出: “01”

说明: "10"也可以打开保险箱。

示例2:

输入: n = 2, k = 2

输出: “00110”

说明: “01100”, “10011”, “11001” 也能打开保险箱。

提示:

n 的范围是 [1, 4]。

k 的范围是 [1, 10]。

k^n 最大可能为 4096。

PS:

该说不说,百度翻译都比这个强

class Solution {
public static final int[] MASK = {0, 1, 10, 100, 1000};
public String crackSafe(int n, int k) {
M = MASK[n];
StringBuilder sb = new StringBuilder();
Hierholzer(0, k, new BitSet(), sb);
for (int i = 0; i < n - 1; i++) sb.append(0);
return sb.toString();
} public int M;
public void Hierholzer(int n, int k, BitSet bs, StringBuilder sb) {
bs.set(n);
int tail = n % 10;
n = (n % M) * 10;
for (int i = 0; i < k; i++, n++) {
if (!bs.get(n)) Hierholzer(n, k, bs, sb);
}
sb.append(tail);
} }

Java实现 LeetCode 753 破解保险箱(递归)的更多相关文章

  1. Java for LeetCode 044 Wildcard Matching

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  2. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  4. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  5. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  6. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  7. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  8. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. Java for LeetCode 154 Find Minimum in Rotated Sorted Array II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. python 矢量数据转栅格数据

    from osgeo import gdal,osr,ogr#定义投影sr = osr.SpatialReference('LOCAL_CS["arbitrary"]')#在内存中 ...

  2. Day_11【集合】扩展案例1_遍历打印学生信息,获取学生成绩的最高分,获取成绩最高的学员,获取学生成绩的平均值,获取不及格的学员数量

    分析以下需求,并用代码实现: 1.按照以下描述完成类的定义 学生类 属性: 姓名name 年龄age 成绩score 行为: 吃饭eat() study(String content)(content ...

  3. CodeForces-650B Image Preview 二分+模拟

    CodeForces-650B Image Preview 题意 手机里有n张图片,打开相机出现的是第一张,第一张右滑得到第n张,同理第n张左滑得到第1张,翻页耗费a秒,看照片耗费1s,但是照片有横屏 ...

  4. 在微服务框架Demo.MicroServer中添加SkyWalking+SkyApm-dotnet分布式链路追踪系统

    1.APM工具的选取 Apm监测工具很多,这里选用网上比较火的一款Skywalking. Skywalking是一个应用性能监控(APM)系统,Skywalking分为服务端Oap.管理界面UI.以及 ...

  5. python机器学习笔记:EM算法

    EM算法也称期望最大化(Expectation-Maximum,简称EM)算法,它是一个基础算法,是很多机器学习领域的基础,比如隐式马尔科夫算法(HMM),LDA主题模型的变分推断算法等等.本文对于E ...

  6. CSS解决border影响元素宽高的问题(box-sizing属性)

    修改 box-sizing 属性.将 box-sizing 设置为 border-box 即可.

  7. SpringMVC 设置全局DateTime json返回格式

    对于部分返回DateTime的项目,只需要在指定属性上添加@JsonSerialize 使用自定义的json转换格式即可自定义返回DateTime格式 但是对于项目中返回有多个DateTime字段来说 ...

  8. vue element-ui el-form-item 循环渲染,验证表单内容

    data里面如下图:

  9. spring cloud系列教程第四篇-Eureka基础知识

    通过前三篇文章学习,我们搭建好了两个微服务工程.即:order80和payment8001这两个服务.有了这两个基础的框架之后,我们将要开始往里面添加东西了.还记得分布式架构的几个维度吗?我们要通过一 ...

  10. 5.2 Go 包与函数

    5.2 Go 包与函数 在多个包中相互调用函数,需要用到Go包的知识. 代码组织如下: 思路: 1.定义功能函数calc放入到utils.go,将utils.go放在utils文件夹/包中,当其他文件 ...