当前位置: 首页 > wzjs >正文

网站建设有哪些软件有哪些内容链接转二维码

网站建设有哪些软件有哪些内容,链接转二维码,武汉建设银行网点查询,wordpress输出到模板这两个问题的本质就是一个棵树,然后根据n对树做剪枝。难点在于剪的时候边界条件有些坑,get_lower_largest_digit_dic是这两个题目的共同点 题目一: 小于n的最大数 算法题目:小于n的最大数 问题描述:给一个数组nums[5…

这两个问题的本质就是一个棵树,然后根据n对树做剪枝。难点在于剪的时候边界条件有些坑,get_lower_largest_digit_dic是这两个题目的共同点

题目一: 小于n的最大数

算法题目:小于n的最大数
问题描述:给一个数组nums=[5,4,8,2],给一个n=5416, 让你从nums中选出一些元素,使得组成的数字是小于n的最大数,比如这个例子应该返回5288
这个题其实就是回溯一步,但是讨论的情况有点绕,细节可以看代码

class Solution:def get_num(self, candidates, num):max_str = str(max(list(candidates)))num_str = str(num)def get_lower_largest_digit_dic(candidates):dic={}prev = Nonefor i in range(10):dic[str(i)] = previf i in candidates:prev = str(i)return diclower_largest_digit_dic = get_lower_largest_digit_dic(candidates)i,l = 0,len(num_str)res_str_arr = ['0' for i in range(l)]while(i<l):if int(num_str[i]) in candidates and i<l-1:# 第一阶段,相等的一直往后填res_str_arr[i] = num_str[i]i += 1else:# 第二阶段:遇到最后一个,或者没有相等的(也分为几种情况),统一填为lower_largest_digit,然后后面统一填最大digit = lower_largest_digit_dic[num_str[i]]while digit == None and i > 0: #一直找不到,一直回溯i -= 1digit = lower_largest_digit_dic[num_str[i]]# 按照while不成功的情况讨论下if i == 0 and digit == None and l == 1:return Noneif i == 0 and digit == None and l > 1:res_str_arr[0] = '0'else:res_str_arr[i] = digitfor j in range(i+1,l):res_str_arr[j] = max_strreturn int(''.join(res_str_arr))if __name__ == '__main__':s = Solution()print(s.get_num({1,2,9,4}, 2533))  # 2499print(s.get_num({1,2,5,4}, 2543))  # 2542print(s.get_num({1,2,5,4}, 2541))  # 2525print(s.get_num({1,2,9,4}, 2111))  # 1999print(s.get_num({5,9}, 5555)) #999

题目二: 最大为 N 的数字组合

来自https://leetcode.com/problems/numbers-at-most-n-given-digit-set/

Given an array of digits which is sorted in non-decreasing order. You can write numbers using each digits[i] as many times as we want. For example, if digits = [‘1’,‘3’,‘5’], we may write numbers such as ‘13’, ‘551’, and ‘1351315’.

Return the number of positive integers that can be generated that are less than or equal to a given integer n.

Example 1:

Input: digits = [“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: digits = [“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 array.
Example 3:

Input: digits = [“7”], n = 8
Output: 1

Constraints:

1 <= digits.length <= 9
digits[i].length == 1
digits[i] is a digit from ‘1’ to ‘9’.
All the values in digits are unique.
digits is sorted in non-decreasing order.
1 <= n <= 109

其实和题目一思路很相似,但是边界条件容易错:

class Solution:def atMostNGivenDigitSet(self, digits: List[str], n: int) -> int:def get_lt_digit_cnt_dic(digits_set):dic = {}prev = 0for i in range(0,10):stri = str(i)dic[stri] = previf stri in digits_set:prev += 1return dicdigits_set = set(digits)lt_digit_cnt_dic = get_lt_digit_cnt_dic(digits_set)dl = len(digits)nl = len(str(n))factorial = [1 for i in range(nl)]for i in range(1,nl):factorial[i] = factorial[i-1]*dlres1 = sum(factorial[i] for i in range(1,nl))res2,i = 0,0strn = str(n)while i<nl:lt_digit_cnt = lt_digit_cnt_dic[strn[i]]res2 += lt_digit_cnt * factorial[nl-(i+1)]if (strn[i] not in digits_set):breaki+=1# 这个条件容易漏掉,例如digits =["3","4","8"], n = 4if i == nl and strn[nl-1] in digits_set:res2 += 1# print(res1)return res1+res2
http://www.dtcms.com/wzjs/66668.html

相关文章:

  • 上海省住房与城乡建设厅网站百度服务商
  • 广东网站建设微信商城运营小程序开发制作
  • 海珠区住房和水务建设局网站营销号
  • 网站建设项目竞价推广代运营服务
  • 做系统网站建设三只松鼠软文范例500字
  • 中国十大网络运营商是哪些网站推广优化网址
  • 期货网站做模拟推广发布任务平台app下载
  • 做网站需要后端吗app平台搭建需要多少钱
  • 网站自动更新文章seo是什么职位简称
  • 域名就是网站名吗bt磁力搜索器
  • 网站开发雷小天怎么建立自己的网站
  • 网站开发周期表防恶意竞价点击软件
  • 青岛公司网站建设淘宝定向推广
  • 虚拟主机建多个网站网络推广属于什么行业
  • 让你有做黑客感觉的网站腾讯广告
  • 成都cms建站什么是企业营销型网站
  • 柒比贰主题wordpress优化大师官网登录入口
  • 做的图怎么上传到网站seo免费培训视频
  • 温州网站搭建电脑优化系统的软件哪个好
  • 网站程序代码优化360优化大师app
  • 淘宝优惠券查询网站怎么做网站模板定制
  • 做网站的接私活犯法吗游戏推广赚佣金的平台
  • 上海住房与建设部网站域名停靠网页app推广大全
  • 做网站网站名字自己设置吗seo服务如何收费
  • 专业做w7系统的网站搜索排名广告营销怎么做
  • 网站开发模块学些什么中国数据统计网站
  • 腾讯网站建设的基本情况关键词排名优化方法
  • 床上爱做网站必应搜索引擎入口
  • 阿里虚拟主机怎么做两个网站网络营销五种方法
  • 做网站需要ui设计吗seo排名赚