mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -122,6 +122,24 @@ class Solution:
|
|||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```Go
|
||||||
|
func canJUmp(nums []int) bool {
|
||||||
|
if len(nums)<=1{
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
dp:=make([]bool,len(nums))
|
||||||
|
dp[0]=true
|
||||||
|
for i:=1;i<len(nums);i++{
|
||||||
|
for j:=i-1;j>=0;j--{
|
||||||
|
if dp[j]&&nums[j]+j>=i{
|
||||||
|
dp[i]=true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dp[len(nums)-1]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -136,7 +136,34 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```
|
||||||
|
class Solution(object):
|
||||||
|
def canConstruct(self, ransomNote, magazine):
|
||||||
|
"""
|
||||||
|
:type ransomNote: str
|
||||||
|
:type magazine: str
|
||||||
|
:rtype: bool
|
||||||
|
"""
|
||||||
|
|
||||||
|
# use a dict to store the number of letter occurance in ransomNote
|
||||||
|
hashmap = dict()
|
||||||
|
for s in ransomNote:
|
||||||
|
if s in hashmap:
|
||||||
|
hashmap[s] += 1
|
||||||
|
else:
|
||||||
|
hashmap[s] = 1
|
||||||
|
|
||||||
|
# check if the letter we need can be found in magazine
|
||||||
|
for l in magazine:
|
||||||
|
if l in hashmap:
|
||||||
|
hashmap[l] -= 1
|
||||||
|
|
||||||
|
for key in hashmap:
|
||||||
|
if hashmap[key] > 0:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
@ -121,6 +121,37 @@ class Solution {
|
|||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
```
|
||||||
|
class Solution(object):
|
||||||
|
def fourSumCount(self, nums1, nums2, nums3, nums4):
|
||||||
|
"""
|
||||||
|
:type nums1: List[int]
|
||||||
|
:type nums2: List[int]
|
||||||
|
:type nums3: List[int]
|
||||||
|
:type nums4: List[int]
|
||||||
|
:rtype: int
|
||||||
|
"""
|
||||||
|
# use a dict to store the elements in nums1 and nums2 and their sum
|
||||||
|
hashmap = dict()
|
||||||
|
for n1 in nums1:
|
||||||
|
for n2 in nums2:
|
||||||
|
if n1 + n2 in hashmap:
|
||||||
|
hashmap[n1+n2] += 1
|
||||||
|
else:
|
||||||
|
hashmap[n1+n2] = 1
|
||||||
|
|
||||||
|
# if the -(a+b) exists in nums3 and nums4, we shall add the count
|
||||||
|
count = 0
|
||||||
|
for n3 in nums3:
|
||||||
|
for n4 in nums4:
|
||||||
|
key = - n3 - n4
|
||||||
|
if key in hashmap:
|
||||||
|
count += hashmap[key]
|
||||||
|
return count
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
@ -175,36 +175,35 @@ Python:
|
|||||||
Go:
|
Go:
|
||||||
```Go
|
```Go
|
||||||
func longestPalindromeSubseq(s string) int {
|
func longestPalindromeSubseq(s string) int {
|
||||||
str:=[]byte(s)
|
lenth:=len(s)
|
||||||
dp:=make([][]int,len(s))
|
dp:=make([][]int,lenth)
|
||||||
for i:=0;i<len(s);i++{
|
for i:=0;i<lenth;i++{
|
||||||
dp[i]=make([]int,len(s))
|
for j:=0;j<lenth;j++{
|
||||||
|
if dp[i]==nil{
|
||||||
|
dp[i]=make([]int,lenth)
|
||||||
}
|
}
|
||||||
for i:=1;i<len(s);i++{
|
if i==j{
|
||||||
for j:=i-1;j>=0;j--{
|
dp[i][j]=1
|
||||||
if str[i]==str[j]{
|
}
|
||||||
if j==i-1{
|
}
|
||||||
dp[j][i]=2
|
}
|
||||||
|
for i:=lenth-1;i>=0;i--{
|
||||||
|
for j:=i+1;j<lenth;j++{
|
||||||
|
if s[i]==s[j]{
|
||||||
|
dp[i][j]=dp[i+1][j-1]+2
|
||||||
}else {
|
}else {
|
||||||
dp[j][i]=dp[j+1][i-1]+2
|
dp[i][j]=max(dp[i+1][j],dp[i][j-1])
|
||||||
}
|
|
||||||
}else{
|
|
||||||
dp[j][i]=Max(dp[j+1][i],dp[j][i-1])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return dp[0][len(s)-1]
|
|
||||||
}
|
return dp[0][lenth-1]
|
||||||
func Max(a,b int)int{
|
|
||||||
if a>b{
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
return b
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
|
Reference in New Issue
Block a user