博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode刷题:902. Numbers At Most N Given Digit Set
阅读量:4039 次
发布时间:2019-05-24

本文共 2906 字,大约阅读时间需要 9 分钟。

LeetCode刷题:902. Numbers At Most N Given Digit Set

原题链接:

We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}.  (Note that '0' is not included.)

Now, we write numbers using these digits, using each digit as many times as we want.  For example, if D = {'1','3','5'}, we may write numbers such as '13', '551', '1351315'.

Return the number of positive integers that can be written (using the digits of D) that are less than or equal to N.

 

Example 1:

Input: D = ["1","3","5","7"], N = 100

Output: 20
Explanation: 
The 20 numbers that can be written are:
1, 3, 5, 7, 11, 13, 15, 17, 31, 33, 35, 37, 51, 53, 55, 57, 71, 73, 75, 77.
Example 2:

Input: D = ["1","4","9"], N = 1000000000

Output: 29523
Explanation: 
We can write 3 one digit numbers, 9 two digit numbers, 27 three digit numbers,
81 four digit numbers, 243 five digit numbers, 729 six digit numbers,
2187 seven digit numbers, 6561 eight digit numbers, and 19683 nine digit numbers.
In total, this is 29523 integers that can be written using the digits of D.
 

Note:

D is a subset of digits '1'-'9' in sorted order.

1 <= N <= 10^9

我们有一组排序的数字 D,它是  {'1','2','3','4','5','6','7','8','9'} 的非空子集。(请注意,'0' 不包括在内。)

现在,我们用这些数字进行组合写数字,想用多少次就用多少次。例如 D = {'1','3','5'},我们可以写出像 '13', '551', '1351315' 这样的数字。

返回可以用 D 中的数字写出的小于或等于 N 的正整数的数目。

 

示例 1:

输入:D = ["1","3","5","7"], N = 100

输出:20
解释:
可写出的 20 个数字是:
1, 3, 5, 7, 11, 13, 15, 17, 31, 33, 35, 37, 51, 53, 55, 57, 71, 73, 75, 77.
示例 2:

输入:D = ["1","4","9"], N = 1000000000

输出:29523
解释:
我们可以写 3 个一位数字,9 个两位数字,27 个三位数字,
81 个四位数字,243 个五位数字,729 个六位数字,
2187 个七位数字,6561 个八位数字和 19683 个九位数字。
总共,可以使用D中的数字写出 29523 个整数。
 

提示:

D 是按排序顺序的数字 '1'-'9' 的子集。

1 <= N <= 10^9


算法设计

package com.bean.algorithm.basic;/* * Input: D = ["1","3","5","7"], N = 100 * Output: 20 * Explanation: * The 20 numbers that can be written are: * 1, 3, 5, 7, 11, 13, 15, 17, 31, 33, 35, 37, 51, 53, 55, 57, 71, 73, 75, 77. * */public class LeetCode902 {			public int atMostNGivenDigitSet(String[] D, int N) {        String T = String.valueOf(N+1);        int[] dp = new int[T.length()+1];                for(int i = T.length() - 1; i >= 0; i--) {            int t = T.charAt(i) - '0';            for(int j = 0; j < D.length; j++) {                int v = D[j].charAt(0) - '0';                if(v > t) break;                if(v < t) {                    dp[i] += (int)Math.pow(D.length, T.length() - 1 - i);                 } else if(v == t) {                    dp[i] += dp[i+1];                    break;                }            }        }                for(int i = 1; i < T.length(); i++) dp[0] += Math.pow(D.length, i);        return dp[0];    }	public static void main(String[] args) {		// TODO Auto-generated method stub		String[] D= {"1","3","5","7"};		int N = 100;				LeetCode902 leetcode902=new LeetCode902();		int result=leetcode902.atMostNGivenDigitSet(D, N);		System.out.println("result = "+result);	}}

 

转载地址:http://hjtdi.baihongyu.com/

你可能感兴趣的文章
MPMoviePlayerViewController和MPMoviePlayerController的使用
查看>>
CocoaPods实践之制作篇
查看>>
[Mac]Mac 操作系统 常见技巧
查看>>
苹果Swift编程语言入门教程【中文版】
查看>>
捕鱼忍者(ninja fishing)之游戏指南+游戏攻略+游戏体验
查看>>
iphone开发基础之objective-c学习
查看>>
iphone开发之SDK研究(待续)
查看>>
计算机网络复习要点
查看>>
Variable property attributes or Modifiers in iOS
查看>>
NSNotificationCenter 用法总结
查看>>
C primer plus 基础总结(一)
查看>>
剑指offer算法题分析与整理(一)
查看>>
剑指offer算法题分析与整理(三)
查看>>
Ubuntu 13.10使用fcitx输入法
查看>>
pidgin-lwqq 安装
查看>>
mint/ubuntu安装搜狗输入法
查看>>
C++动态申请数组和参数传递问题
查看>>
opencv学习——在MFC中读取和显示图像
查看>>
retext出现Could not parse file contents, check if you have the necessary module installed解决方案
查看>>
Matlab与CUDA C的混合编程配置出现的问题及解决方案
查看>>