Zhuanli&Blog


  • 首页

  • 标签

  • 分类

  • 归档

leetcode 300. 最长递增子序列

发表于 2018-06-15 | 分类于 leetcode

给定一个无序的整数数组,找到其中最长递增子序列的长度。

示例:

输入: [10,9,2,5,3,7,101,18]
输出: 4 
解释: 最长的递增子序列是 [2,3,7,101],它的长度是 4。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from bisect import bisect_left
class Solution(object):
def lengthOfLIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
res = []
for i in nums:
index = bisect_left(res,i)
if index == len(res):
res.append(i)
else:
res[index] = i
return len(res)

leetcode 354. 俄罗斯套娃信封问题

发表于 2018-06-15 | 分类于 leetcode

给定一些标记了宽度和高度的信封,宽度和高度以整数对形式 (w, h) 出现。当另一个信封的宽度和高度都比这个信封大的时候,这个信封就可以放进另一个信封里,如同俄罗斯套娃一样。

请计算最多能有多少个信封能组成一组“俄罗斯套娃”信封(即可以把一个信封放到另一个信封里面)。

示例:

输入: envelopes = [[5,4],[6,4],[6,7],[2,3]]
输出: 3 
解释: 最多信封的个数为 3, 组合为: [2,3] => [5,4] => [6,7]。
  • o(n^2) solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution(object):
def maxEnvelopes(self, envelopes):
"""
:type envelopes: List[List[int]]
:rtype: int
"""
if not envelopes:return 0
nums = sorted(envelopes,key=lambda x: x)
dp = [1]*len(nums)
for i in range(1,len(nums)):
for j in range(i-1,-1,-1):
if nums[i][0] > nums[j][0] and nums[i][1] > nums[j][1]:
dp[i] = max(dp[i],dp[j]+1)

return max(dp)
  • o(n*logn) solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import bisect
class Solution(object):
def maxEnvelopes(self, envelopes):
"""
:type envelopes: List[List[int]]
:rtype: int
"""
if not envelopes:return 0
envs = sorted(envelopes,key=lambda (x,y): (x,-y))
tails=[]
for (w,h) in envs:
idx=bisect.bisect_left(tails, h)
if idx==len(tails):
tails.append(h)
elif idx==0 or tails[idx-1]<h:
tails[idx]=h
return len(tails)

解释:通过对w升序排列,对h降序排列,然后h的最长递增子序列长度就是题目要求的长度;求最长递增子序列通过二分法优化成o(n*logn)

关于leetcode 2sum、3sum、4sum到ksum的情况分析

发表于 2018-06-14 | 分类于 leetcode

通过#2sum,#3sum,#4sum
推广到ksum的情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution(object):

def findKsum(self, nums, target, N, result, results):
if len(nums) < N or N<2 or target < nums[0]*N or target>nums[-1]*N :
return []
if N ==2:
l=0
r = len(nums)-1
while l<r:
s = nums[l]+nums[r]
if s == target:
results.append(result + [nums[l], nums[r]])
l +=1
while l<r and nums[l]==nums[l-1]:
l += 1
elif s<target:
l+=1
else:
r-=1
else:
for i, x in enumerate(nums[:-(N-1)]):
if i>0 and nums[i]==nums[i-1]:
continue
self.findKsum(nums[i+1:], target - x, N-1, result + [x], results)
return results

leetcode 18. 四数之和

发表于 2018-06-14 | 分类于 leetcode

给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。

注意:

答案中不可以包含重复的四元组。

示例:

给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。

满足要求的四元组集合为:
[
  [-1,  0, 0, 1],
  [-2, -1, 1, 2],
  [-2,  0, 0, 2]
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution(object):
def fourSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
"""
nums.sort()
res = []
for i in range(len(nums)-2):
if i > 0 and nums[i] == nums[i-1]:continue
for j in range(i+1,len(nums)-2):
if j > i+1 and nums[j] == nums[j-1]:continue
l,r = j + 1,len(nums)-1
while l < r:
s = nums[i] + nums[j] + nums[r] + nums[l]
if s <target:
l += 1
elif s > target:
r -= 1
else:
res.append([nums[i] ,nums[j] ,nums[r] , nums[l]])
while l < r and nums[l] == nums[l + 1]:
l += 1
while l < r and nums[r] == nums[r - 1]:
r -= 1
l += 1
r -= 1
return res

leetcode 357. Count Numbers with Unique Digits

发表于 2018-06-13 | 分类于 leetcode

Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10^n.

Example:
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])

1
2
3
4
5
6
7
8
9
10
11
12
class Solution(object):
def countNumbersWithUniqueDigits(self, n):
"""
:type n: int
:rtype: int
"""
res = 1
for i in range(n):
cur = 9
for j in range(i): cur *= 9 - j
res += cur
return res

GET和POST的区别

发表于 2018-06-12 | 分类于 html/css/js

转自微信公众号WebTechGarden

GET和POST是HTTP请求的两种基本方法,要说他们的区别,接触过WEB开发的人都能说出一二。

最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数。

你可能自己写过无数个GET和POST请求,或者已经看过很多权威网站总结出的他们的区别,你非常清楚的知道什么时候该用什么。

当你在面试中遇到这个问题时,你的内心充满了喜悦。

你轻轻松松的给出了一个“标准答案”:

  1. GET在浏览器回退时是无害的,而POST会再次提交请求。
  2. GET产生的URL地址可以被Bookmark,而POST不可以。
  3. GET请求会被浏览器主动cache,而POST不会,除非手动设置。
  4. GET请求的参数会完整的被保存在历史记录里,POST不会。
  5. GET请求参数放在URL中,POST放在request body中。
  6. GET请求只能进行url编码,POST请求支持多种编码方式。
  7. 对于参数类型,GET只接受ASCII字符,而POST没有限制。
  8. GET请求在URL中传递的参数是有长度限制的,而POST没有。
  9. GET比POST更不安全,因为参数直接暴露在URL中,所以不能传递敏感信息。

but,这不是我们想要的回答!!!!

如果我告诉你GET和POST本质上没有区别,你信吗?

GET和POST是什么?
HTTP中两种发送请求的方法。

HTTP是什么?
HTTP是基于TCP/IP的关于数据如何在万维网中传递的通信协议。

HTTP的底层是TCP/IP,所以GET和POST底层也是TCP/IP,也就是说GET和POST都是TCP链接。GET和POST能做的事情是一样的。你要给GET加上request body或者给POST带上url参数技术上是完全行的通的。

在我大万维网世界中,TCP就像汽车,我们用TCP来运输数据,它很可靠,从来不会发生丢件少件的现象。但是如果路上跑的全是看起来一模一样的汽车,那这个世界看起来是一团混乱,送急件的汽车可能被前面满载货物的汽车拦堵在路上,整个交通系统一定会瘫痪。为了避免这种情况发生,交通规则HTTP诞生了。HTTP给汽车运输设定了好几个服务类别,有GET, POST, PUT, DELETE等等,HTTP规定,当执行GET请求的时候,要给汽车贴上GET的标签(设置method为GET),而且要求把传送的数据放在车顶上(url中)以方便记录。如果是POST请求,就要在车上贴上POST的标签,并把货物放在车厢里。当然,你也可以在GET的时候往车厢内偷偷藏点货物,但是这是很不光彩;也可以在POST的时候在车顶上也放一些数据,让人觉得傻乎乎的。HTTP只是个行为准则,而TCP才是GET和POST怎么实现的基本。

但是,我们只看到HTTP对GET和POST参数的传送渠道(url还是requrest body)提出了要求。“标准答案”里关于参数大小的限制又是从哪来的呢?

在我大万维网世界中,还有另一个重要的角色:运输公司。不同的浏览器(发起http请求)和服务器(接受http请求)就是不同的运输公司。 虽然理论上,你可以在车顶上无限的堆货物(url中无限加参数)。但是运输公司可不傻,装货和卸货也是有很大成本的,他们会限制单次运输量来控制风险,数据量太大对浏览器和服务器都是很大负担。业界不成文的规定是,(大多数)浏览器通常都会限制url长度在2K个字节,而(大多数)服务器最多处理64K大小的url。超过的部分,恕不处理。如果你用GET服务,在request body偷偷藏了数据,不同服务器的处理方式也是不同的,有些服务器会帮你卸货,读出数据,有些服务器直接忽略,所以,虽然GET可以带request body,也不能保证一定能被接收到哦。

好了,现在你知道,GET和POST本质上就是TCP链接,并无差别。但是由于HTTP的规定和浏览器/服务器的限制,导致他们在应用过程中体现出一些不同。

GET和POST还有一个重大区别,简单的说:
GET产生一个TCP数据包;POST产生两个TCP数据包。

总的来说:
对于GET方式的请求,浏览器会把http header和data一并发送出去,服务器响应200(返回数据);
而对于POST,浏览器先发送header,服务器响应100 continue,浏览器再发送data,服务器响应200 ok(返回数据)。

也就是说,GET只需要汽车跑一趟就把货送到了,而POST得跑两趟,第一趟,先去和服务器打个招呼“嗨,我等下要送一批货来,你们打开门迎接我”,然后再回头把货送过去。

因为POST需要两步,时间上消耗的要多一点,看起来GET比POST更有效。因此Yahoo团队有推荐用GET替换POST来优化网站性能。但这是一个坑!跳入需谨慎。为什么?

GET与POST都有自己的语义,不能随便混用。
据研究,在网络环境好的情况下,发一次包的时间和发两次包的时间差别基本可以无视。而在网络环境差的情况下,两次包的TCP在验证数据包完整性上,有非常大的优点。
并不是所有浏览器都会在POST中发送两次包,Firefox就只发送一次。

leetcode 365. 水壶问题

发表于 2018-06-12 | 分类于 leetcode

有两个容量分别为 x升 和 y升 的水壶以及无限多的水。请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水?

如果可以,最后请用以上水壶中的一或两个来盛放取得的 z升 水。

你允许:

  • 装满任意一个水壶
  • 清空任意一个水壶
  • 从一个水壶向另外一个水壶倒水,直到装满或者倒空

示例1:

输入: x = 3, y = 5, z = 4
输出: True

示例2:

输入: x = 2, y = 6, z = 5
输出: False
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution(object):
def canMeasureWater(self, x, y, z):
"""
:type x: int
:type y: int
:type z: int
:rtype: bool
"""
#贝祖定理 ax+by = gcd(x,y)
def gcd(x,y):
if x < y:return gcd(y,x)
if y == 0:return x
elif x%2 == 0:
if y%2 == 0:return gcd(x>>1,y>>1)<<1
else:return gcd(x>>1,y)
else:
if y%2 == 0:return gcd(x,y>>1)
else:return gcd(y,x-y)

if z > x+y:return False
if z == y or z == x or z == x+y:return True
return z%gcd(x,y) == 0

用hexo+github搭建自己的博客

发表于 2018-06-12 | 分类于 tools
  1. 安装git客户端
  2. 安装node.js
  3. 使用npm安装hexo

    1
    npm install -g hexo-cli
  4. 初始化博客文件夹

    1
    hexo i blog
  5. 选择主题(非必选),把主题包放在themes文件夹下,修改_config.yml文件中theme字段改为主题名称

    1
    git clone https://github.com/iissnan/hexo-theme-next themes/next
  6. github账号注册,创建仓库yourname.github.io

  7. 修改hexo站点的配置文件_config.yml

    1
    2
    3
    4
    deploy:
    type: git
    repository: git@github.com:zhuanli555/zhuanli555.github.io.git
    branch: master
  8. 部署到github

    1
    2
    3
    4
    npm install hexo-deployer-git --save
    hexo new "postname"
    hexo g
    hexo d
  9. 打开http://zhuanli555.github.io 访问

leetcode 368. Largest Divisible Subset

发表于 2018-06-11 | 分类于 leetcode

Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or Sj % Si = 0.

If there are multiple solutions, return any subset is fine.

Example 1:

nums: [1,2,3]
Result: [1,2] (of course, [1,3] will also be ok)

Example 2:

nums: [1,2,4,8]
Result: [1,2,4,8]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution(object):
def largestDivisibleSubset(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
if not nums:return []
nums.sort()
n = len(nums)
dp = [1]*n
for i in range(1,n):
for j in range(i-1,-1,-1):
if nums[i]%nums[j] == 0:
dp[i] = max(dp[i],dp[j] + 1)
#根据dp数组找出符合规定的一条数据
out = []
curval = 0
curlen = 0
for i in range(len(dp)):
if curlen < dp[i]:
curlen = dp[i]
curval = nums[i]
out.append(curval)
for i in range(n-1,-1,-1):
if curval%nums[i] == 0 and dp[i] == curlen - 1:
curlen = curlen-1
curval = nums[i]
out.append(curval)
return out

leetcode 372. Super Pow

发表于 2018-06-10 | 分类于 leetcode

Your task is to calculate a**b mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.

Example1:


a = 2
b = [3]

Result: 8

Example2:


a = 2
b = [1,0]

Result: 1024

1
2
3
4
5
6
7
8
9
10
11
class Solution(object):
def superPow(self, a, b):
"""
:type a: int
:type b: List[int]
:rtype: int
"""
#f(a,123456) = f(a,123450)*pow(a,6)=f(pow(a,10),12345)*pow(a,6)
#n1*n2%k = (n1%k)(n2%k)%k
if not b:return 1
return pow(a, b.pop(), 1337)*self.superPow(pow(a, 10, 1337), b)%1337

1…121314…20

zhuanli

194 日志
9 分类
41 标签
GitHub
© 2018 — 2019 zhuanli
由 Hexo 强力驱动
|
主题 — NexT.Pisces v5.1.4