刷题
码破苍穹
中国普通青年
展开
-
leetcode,jump game 思考
代码基于贪心算法进行,看起来很简单。def canJump(self, nums): """ :type nums: List[int] :rtype: bool """ end = 0 for i in range(len(nums)): if end < i: ...原创 2018-07-19 10:16:15 · 178 阅读 · 0 评论 -
leetcode,买卖股票3,python版本
def maxProfit(prices): # 分别正序和逆序扫描序列。 # 正序序列得出从头往后到某一天卖出时的最大收益。 # 逆序序列得出从后玩前到某一天卖出时的最大收益。 # 然后两个和加起来,取最大即可。 buy_price = prices[0] sell_price = prices[-1] leng = len(prices...原创 2019-01-25 10:52:59 · 230 阅读 · 0 评论 -
python,辗转相除法求最大公约数
def gcd(a, b): if a < b: a, b = b, a while b != 0: temp = a % b print temp a = b b = temp return aprint gcd(8251,6105)求出了这个数可以求出最小公倍数。(...原创 2018-05-30 21:08:56 · 10504 阅读 · 0 评论 -
微软在线笔试font size,python版
题目见与hihocoder,大意就是在已知书的尺寸往里面塞字符,要求总的页码小于一个值。字符以自然段的形式存在。 import mathn = int(raw_input())# N P W Nfor i in range(n): x1 = raw_input().strip("\n").split(" ") x1 = map(int,x1) x2 =...原创 2018-03-30 14:25:44 · 339 阅读 · 0 评论 -
hihocoder,#1051 : 补提交卡,python版本
n = int(raw_input())for i in range(n): x1 = raw_input().strip("\n").split(" ") x1 = map(int, x1) x2 = raw_input().strip("\n").split(" ") x2 = map(int, x2) N, M = x1[0], x1[1] ...原创 2018-03-30 15:39:44 · 218 阅读 · 0 评论 -
回溯法解Leetcode之Combination Sum心得
https://leetcode.com/problems/combination-sum/description/1、得到总的Sum是一个累加的过程。需要使用元素逐一的叠加起来,因此递归的每一步就是再加一个元素的过程。2、对于求和类的题目,使用target 逐一的减,是一个常见的方法。如Two Sum,以及一系列二叉树中求和路径的题目、对于此题,主函数为:def combina...原创 2018-03-07 20:43:38 · 207 阅读 · 0 评论 -
hihocoder,沼跃鱼。python
while True: try: string = raw_input() i = 0 res='' while i<len(string)-8: sub_str=string[i:i+9] # print(sub_str) if(sub_str.lower...原创 2018-03-30 16:50:32 · 180 阅读 · 0 评论 -
hihocoder,#1498 : Diligent Robots。python
# coding=utf8import mathimport sysline = raw_input().strip("\n").split(" ")line = map(int, line)N = line[0] # job的数量Q = line[1] # 自我复制的耗时min_ans = sys.maxint# dup_time = int(math.ceil(ma...原创 2018-03-31 16:44:21 · 172 阅读 · 0 评论 -
从使用python在leetcode里编程中写点对self的理解。
在刷Construct Binary Tree from Preorder and Inorder Traversal时,一种解法是这样的 def buildTree2(self, preorder, inorder): return self.buildTree2_helper(0, len(preorder), 0, len(inorder)) de...原创 2018-01-28 20:57:34 · 1837 阅读 · 0 评论 -
关于二分查找法,高低限+-1的思考
low = 0high = len(nums) - 1 mid = (low + high) / 2当nums的个数为奇数时,mid的值将正好是nums的正中间的那个当nums的个数为偶数时,mid的值将是偶数/2的那个。如[0,1,2,3,4,5],则将取到第3个值,即取2。其实这样有点“左偏的意思了” 也许正是由于这个性质,使得左限每次都要+1(如二分查找,寻找数组的最小...原创 2018-03-06 22:14:05 · 725 阅读 · 0 评论 -
hihocoder,#1497 : Queen Attack。python
N = int(raw_input())data = []x_ = []y_ = []for i in range(N): dataline = raw_input().strip('\n').split(" ") dataline = map(int, dataline) x, y = dataline[0], dataline[1] data.a...原创 2018-03-31 19:35:36 · 254 阅读 · 0 评论 -
4.10华为暑期实习生机试题目,python解答
第一题是给一个字符串,然后按照次数和ascii码的顺序,循环输出。比如输入eeefffggh,由于ascii的大小,e<f<g<h,则输出'efgh' + 'efg' + 'ef' = 'efghefgef'。字符串中可包含数字和大小写英文字母。思路是先用dict统计个数,然后将所有可能出现的字符放在一个数组里,维护起来,Whole = ['0','1...原创 2018-04-10 21:28:04 · 751 阅读 · 0 评论 -
leetcode之n个骰子的点数,python版本
书上写的还是比较晦涩难懂的。实际上用动态规划很好解决:#coding=utf8def get_ans(n): dp = [[0 for i in range(6*n)] for i in range(n)] for i in range(6): dp[0][i] = 1 # print dp for i in range(1,n): #1...原创 2018-06-24 20:55:20 · 2667 阅读 · 0 评论 -
不错的 对 leetcode中Validate Binary Search Tree的理解
https://www.cnblogs.com/agentgamer/p/4100911.html 启发有两点:1、二叉查找树的中序遍历是递增的序列2、二叉查找树的叶节点是有范围的。如下图所示:中序遍历结果正好为1,2,3,4,5,6,7,8,9,10对于3节点,其实际取值为(2,4)之间。对于8节点,其实际取值为(7,9)之间。所以对于一个根节点,其左右节点的值都...转载 2017-12-18 16:21:21 · 156 阅读 · 0 评论