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. 折腾了好久的vscode配置c/c++语言环境(Windows环境下)

    最近有c语言相关的作业,但是突然再次拿起大一的时候那些c语言编辑器的时候,总觉得不智能,于是下了一个vscode,准备配一个c语言的环境 步骤如下: 1.vs官网下载好vscode,安装好以后再下载一 ...

  2. 【Hadoop离线基础总结】MapReduce 社交粉丝数据分析 求出哪些人两两之间有共同好友,及他俩的共同好友都有谁?

    MapReduce 社交粉丝数据分析 求出哪些人两两之间有共同好友,及他俩的共同好友都有谁? 用户及好友数据 A:B,C,D,F,E,O B:A,C,E,K C:F,A,D,I D:A,E,F,L E ...

  3. C# 数据操作系列 - 2. ADO.NET操作

    0.前言 在上一篇中初略的介绍了一下SQL的基本写法,这一篇开始我们正式步入C#操作数据库的范围.通过这一系列的内容,我想大家能对于数据库交互有了一定的认识和基础.闲话不多说,先给大家介绍一个C#操作 ...

  4. 小程序-for循环遍历的使用

    .js文件: Page({ /** * 页面的初始数据 */ data: { datas:[ { title: '提交申请', txt: '选择服务类型,填写基本信息,提交' }, { title: ...

  5. lsof 命令用法:查看已删除空间却没有释放的进程

    查看已经删除的文件,空间有没有释放,没有的话kill掉pid lsof -n |grep deleted lsof简介lsof(list open files)是一个列出当前系统打开文件的工具. 问题 ...

  6. android 压缩图片大小,防止OOM

    android开发中,图片的处理是非常普遍的,经常是需要将用户选择的图片上传到服务器,但是现在手机的分辨率越来越好了,随便一张照片都是2M或以上,如果直接显示到ImageView中,是会出现OOM的, ...

  7. 一篇文章教会你利用Python网络爬虫获取电影天堂视频下载链接

    [一.项目背景] 相信大家都有一种头疼的体验,要下载电影特别费劲,对吧?要一部一部的下载,而且不能直观的知道最近电影更新的状态. 今天小编以电影天堂为例,带大家更直观的去看自己喜欢的电影,并且下载下来 ...

  8. 我们为什么推荐在Json中使用string表示Number属性值?

    在这篇简短的文章中,我将解释在使用JSON传输数据时,为什么浮点数或大十进制值应表示为字符串 . long类型引发的诡异情况 长话短说,同事在利用swagger对接后端API时,诡异的发现swagge ...

  9. iptables做nat网络地址转换

    iptables做nat网络地址转换. 0. 权威文档 http://www.netfilter.org/documentation/HOWTO/NAT-HOWTO-6.html e文好的直接跳过本文 ...

  10. ArrrayList底层代码的实现

    定义变量 首先要想实现该块代码,必须定义三个私有变量. private Object[] elementData;该变量用来存储容器中元素的个数. private int size:该变量表示当前容器 ...