Telephone Number

Problem

Print all valid phone numbers of length n subject to following constraints:
If a number contains a 4, it should start with 4
No two consecutive digits can be same
Three digits (e.g. 7,2,9) will be entirely disallowed, take as input

Solution

Recursive + DFS

 1 public static List<List<Integer>> generateNumber(int length, List<Integer> baned) {
 2     List<List<Integer>> res = new ArrayList<List<Integer>>();
 3     List<Integer> num = new ArrayList<Integer>();
 4     HashSet<Integer> ban = new HashSet<Integer>();
 5     for(int i=0; i<baned.size(); i++) {
 6         ban.add(baned.get(i));
 7     }
 8
 9     helper(res, num, ban, -1, length, 0);
10
11     return res;
12 }
13
14 public static void helper(List<List<Integer>> res, List<Integer> num, HashSet<Integer> ban, int prev, int length, int pos) {
15     if(pos == length) {
16         res.add(new ArrayList<Integer>(num));
17         return;
18     }
19
20     for(int i=0; i<=9; i++) {
21         if(pos != 0 && i == 4) continue;
22         else if(ban.contains(i)) continue;
23         else if(i == prev) continue;
24
25         num.add(i);
26         helper(res, num, ban, i, length, pos+1);
27         num.remove(num.size()-1);
28     }
29 }
时间: 2024-10-10 20:01:33

Telephone Number的相关文章

A. Telephone Number

链接:https://codeforces.com/contest/1167/problem/A 题意: A telephone number is a sequence of exactly 11 digits, where the first digit is 8. For example, the sequence 80011223388 is a telephone number, but the sequences 70011223388and 80000011223388 are n

SGU[127] Telephone directory

Description 描述 CIA has decided to create a special telephone directory for its agents. The first 2 pages of the directory contain the name of the directory and instructions for agents, telephone number records begin on the third page. Each record tak

Telephone Phrases

There are some common phrases and sentences you can use when speaking on the telephone. The informal phrases are mostly for family and friends. The formalphrases are for business and official calls and for calls to important people. Caller unknown me

python学习之成员信息增删改查

主要实现了成员信息的增加,修改,查询,和删除功能,写着玩玩,在写的过程中,遇到的问题,旧新成员信息数据的合并,手机号和邮箱的验证, #!/usr/bin/env python# coding=utf8#author:[email protected] import os, sys, time,jsonimport re member_dict = \ {}member_name_list = []member_list = []def handler_member_storage(name,pa

PKU OJ 1002 487-3279

? 487-3279 Description Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT

windows 远程桌面研究

最近因为一个监控相关的项目,深入研究了一下 windows 的 远程桌面的相关知识. 1. 如何让关闭了远程桌面连接的用户,对应的 session 立即退出 windows server. 大家使用 mstsc.exe 远程桌面登录windows server时,退出时,99.99%的人会直接关闭 mstsc.exe 窗口,而不会点击开始--->退出.导致的问题是,登录用户已经提出了,但是 query user 和 query session 时,发现退出的用户,在 windows server

B - Numbers That Count

Description "Kronecker's Knumbers" is a little company that manufactures plastic digits for use in signs (theater marquees, gas station price displays, and so on). The owner and sole employee, Klyde Kronecker, keeps track of how many digits of e

OData

http://scn.sap.com/docs/DOC-44986 Introduction OData services provide a uniform interface for interacting with their resources, and in addition are self-describing: The service document (located at the service root) lists the available top-level reso

手机号码提示器 jquery不可行js可行

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> form{width: 300px;margin: 10px;position: relative;} fieldset{padding: 10