[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 ...
随机推荐
- AJAX基本操作 + 登录 + 删除 + 模糊查询
AJAX练习显示页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// ...
- secureCRT修改背景色、字体颜色
参考: http://blog.csdn.net/jediael_lu/article/details/37729465 http://blog.csdn.net/dagefeijiqumeiguo/ ...
- 洛谷P1118 数字三角形【dfs】【STL】
题目链接:https://www.luogu.org/problemnew/show/P1118 题意: 1~n的一个排列,相邻的两项加起来得到下一行. 现在给定最后一行的数字,问最初的1~n的排列是 ...
- TOP100summit:【分享实录-华为】微服务场景下的性能提升最佳实践
本篇文章内容来自2016年TOP100summit华为架构部资深架构师王启军的案例分享.编辑:Cynthia 王启军:华为架构部资深架构师.负责华为的云化.微服务架构推进落地,前后参与了华为手机祥云4 ...
- .NET Core开发日志——Peachpie
.NET Core的生态圈随着开源社区的力量不断注入至其中,正在变得越来越强盛,并且不时得就出现些有意思的项目,比如Peachpie,它使得PHP的代码迁移到.NET Core项目变得可能. 从创建简 ...
- [No0000E1]C# 关键字
关键字是 C# 编译器预定义的保留字.这些关键字不能用作标识符,但是,如果您想使用这些关键字作为标识符,可以在关键字前面加上 @ 字符作为前缀. 在 C# 中,有些标识符在代码的上下文中有特殊的意义, ...
- 【紫书】Urban Elevations UVA - 221 离散化
题意:给你俯视图,要求依次输出正视图中可以看到的建筑物 题解:任意相邻的x间属性相同,所以离散化. 坑:unique只能对数组用.下标易错 list不能找某元素的next.用了个很麻烦的处理 数组: ...
- MyCAT 在 Cobar 的基础上,完成了彻底的 NIO 通讯,并且合并了两个线程池
研读: 1.http://www.mycat.io <Mycat权威指南> 第 2 章 Mycat 前世今生: 浏览: 深度认识 Sharding-JDBC:做最轻量级的数据库中间层 - ...
- day0311 深浅copy
1.赋值运算 l1 = [1,2,3,['barry','alex']] l2 = l1 l1[0] = 111 print(l1) # [111, 2, 3, ['barry', 'alex']] ...
- CALayer的子类之CAShapeLayer
一,CAShapeLayer介绍 * CAShapeLayer继承自CALayer,属于QuartzCore框架,可使用CALayer的所有属性. CAShapeLayer是在坐标系内绘制贝塞尔曲 ...