[LeetCode] 788. Rotated Digits_Easy tag: **Dynamic Programming
基本思路建一个helper function, 然后从1-N依次判断是否为good number, 注意判断条件为没有3,4,7 的数字,并且至少有一个2,5,6,9, 否则的话数字就一样了, 比如88, 18等.
Improve: 利用DP去判断, 时间和空间都能降为O(lgn)
Code T: O(Nlgn) S; O(lgn)
class Solution:
def rotatedDigits(self, N):
## Solution: T: O(Nlgn) S; O(lgn)
def helper(n):
s = str(n)
return all(d not in "" for d in s) and any(d in '' for d in s)
ans = 0
for i in range(1,N + 1):
if helper(i):
ans += 1
return ans
[LeetCode] 788. Rotated Digits_Easy tag: **Dynamic Programming的更多相关文章
- [LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [LeetCode] 72. Edit Distance_hard tag: Dynamic Programming
Given two words word1 and word2, find the minimum number of operations required to convert word1to w ...
- [LeetCode] 120. Triangle _Medium tag: Dynamic Programming
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [LeetCode] 276. Paint Fence_Easy tag: Dynamic Programming
There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...
- [LeetCode] 256. Paint House_Easy tag: Dynamic Programming
There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...
- [LeetCode] 121. Best Time to Buy and Sell Stock_Easy tag: Dynamic Programming
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- Easy Way to Get All Dependent Library Names 快速获得所有依赖库名称
在编译一些大型SDK的时候,比如Qt,OpenCV, PCL, VTK, ITK等等,在VS中,我们需要将编译生成的.lib文件加入Linker->Input中,但是往往生成的.lib文件有很多 ...
- HDU 5542 - The Battle of Chibi - [离散化+树状数组优化DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5542 Problem DescriptionCao Cao made up a big army an ...
- [No0000178]改善C#程序的建议1:非用ICloneable不可的理由
好吧,我承认,这是一个反标题,实际的情况是:我找不到一个非用ICloneable不可的理由.事实上,接口ICloneable还会带来误解,因为它只有一个Clone方法. 我们都知道,对象的拷贝分为:浅 ...
- [No0000143]Win10“卓越性能模式”
从不久之前Win10更新1803以来,微软不仅带来了一些新功能(和BUG),还悄悄地加入了一个“卓越性能模式”,老张想了想,不对呀,以前就有了一个“高性能模式”,这怎么就还多出个新的性能模式来,难道会 ...
- [No0000139]轻量级文本编辑器,Notepad最佳替代品:Notepad++
在详细介绍Notepad++之前,先来解释一下,为何要选择Notepad++,即把常见的一些文本编辑器和Notepad++比较,看看其有哪点好: 常见的文本编辑器有很多,此处,只提及Notepad,N ...
- ElasticSearch入门 第一篇:Windows下安装ElasticSearch
这是ElasticSearch 2.4 版本系列的第一篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...
- 子分区 复合分区 Subpartitioning
https://dev.mysql.com/doc/refman/8.0/en/partitioning-subpartitions.html
- [daily][cgroup] 使用cgroup限制进程的CPU占用
参考: https://segmentfault.com/a/1190000008323952 1. 找到cgroup设置的地方. [root@D128 ~]# mount -l |grep cpu ...
- day2_python基础
1.变量: 用来存东西的,左边是名字,右边是值 2.python中的单引号.双信号.三引号 单引号和双引号和三引号没什么区别,用哪个都可以,如果定义字符串里面如果有单引号,则外面用双引号;如果字符串里 ...
- 2018/04/14 理解oAuth2.0
最近都近没有更新博客了,卡在 oAuth 上了. 之前公司做统一身份的认证,不了解 oAuth 的我在这卡了两天. 于是决定仔细研究原理,理论指导实践. -- 什么是 oAuth ? 简单来说 oAu ...