-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update leetcode submissions. over 450 solutions.
- Loading branch information
1 parent
f0826ca
commit 827104e
Showing
616 changed files
with
19,430 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Solution { | ||
public: | ||
vector<int> twoSum(vector<int>& nums, int target) { | ||
vector<int> res; | ||
int i = 0, j = 0; | ||
for (; i < nums.size(); i ++) | ||
{ | ||
for (j=i+1; j < nums.size(); j ++) | ||
{ | ||
if (nums[i] + nums[j] == target) | ||
{ | ||
res.push_back(i); | ||
res.push_back(j); | ||
return res; | ||
} | ||
} | ||
} | ||
return res; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
public class Solution { | ||
public int[] twoSum(int[] numbers, int target) { | ||
HashMap<Integer, Integer> map = new HashMap<>(); | ||
for(int i=0; i < numbers.length; i ++){ | ||
map.put(numbers[i], i); | ||
} | ||
int[] result = new int[2]; | ||
for(int i = 0; i < numbers.length; i ++){ | ||
int waitCal = target-numbers[i]; | ||
if(map.get(waitCal) != null && map.get(waitCal) > i){ | ||
result[0] = i; | ||
result[1] = map.get(waitCal); | ||
break; | ||
} | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Solution(object): | ||
def twoSum(self, nums, target): | ||
""" | ||
:type nums: List[int] | ||
:type target: int | ||
:rtype: List[int] | ||
""" | ||
m_map = {} | ||
for i, v in enumerate(nums): | ||
if target - v in m_map: | ||
return m_map[target-v], i | ||
else: | ||
m_map[v] = i | ||
return [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
class Solution { | ||
public: | ||
// 备忘录 | ||
vector<vector<int>> memo; | ||
|
||
bool isMatch(string s, string p) { | ||
int m = s.size(), n = p.size(); | ||
memo = vector<vector<int>>(m, vector<int>(n, -1)); | ||
// 指针 i,j 从索引 0 开始移动 | ||
return dp(s, 0, p, 0); | ||
} | ||
|
||
/* 计算 p[j..] 是否匹配 s[i..] */ | ||
bool dp(string& s, int i, string& p, int j) { | ||
int m = s.size(), n = p.size(); | ||
// base case | ||
if (j == n) { | ||
return i == m; | ||
} | ||
if (i == m) { | ||
if ((n - j) % 2 == 1) { | ||
return false; | ||
} | ||
for (; j + 1 < n; j += 2) { | ||
if (p[j + 1] != '*') { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
// 查备忘录,防止重复计算 | ||
if (memo[i][j] != -1) { | ||
return memo[i][j]; | ||
} | ||
|
||
bool res = false; | ||
|
||
if (s[i] == p[j] || p[j] == '.') { | ||
if (j < n - 1 && p[j + 1] == '*') { | ||
res = dp(s, i, p, j + 2) | ||
|| dp(s, i + 1, p, j); | ||
} else { | ||
res = dp(s, i + 1, p, j + 1); | ||
} | ||
} else { | ||
if (j < n - 1 && p[j + 1] == '*') { | ||
res = dp(s, i, p, j + 2); | ||
} else { | ||
res = false; | ||
} | ||
} | ||
// 将当前结果记入备忘录 | ||
memo[i][j] = res; | ||
return res; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* struct TreeNode { | ||
* int val; | ||
* TreeNode *left; | ||
* TreeNode *right; | ||
* TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
bool isSameTree(TreeNode* p, TreeNode* q) { | ||
if (p == NULL && q == NULL) return true; | ||
if (p == NULL || q == NULL) return false; | ||
if (p->val != q->val) return false; | ||
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, val=0, left=None, right=None): | ||
# self.val = val | ||
# self.left = left | ||
# self.right = right | ||
class Solution: | ||
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: | ||
|
||
if p is None and q is None: | ||
return True | ||
|
||
if p is None or q is None: | ||
return False | ||
|
||
if p.val != q.val: | ||
return False | ||
|
||
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, val=0, left=None, right=None): | ||
# self.val = val | ||
# self.left = left | ||
# self.right = right | ||
class Solution: | ||
def isUnivalTree(self, root: Optional[TreeNode]) -> bool: | ||
self.s = set() | ||
|
||
def pre_order(root): | ||
if root is None: return | ||
|
||
self.s.add(root.val) | ||
pre_order(root.left) | ||
pre_order(root.right) | ||
|
||
pre_order(root) | ||
return len(self.s) == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class Solution { | ||
public: | ||
vector<int> pancakeSort(vector<int>& arr) { | ||
vector<int> res; | ||
sort(res, arr, arr.size()); | ||
return res; | ||
} | ||
|
||
void sort(vector<int> & res, vector<int>& arr, int n) { | ||
if (n == 1) return; | ||
|
||
int maxVal = 0; | ||
int maxIndex = 0; | ||
for (int i = 0; i < n; i ++) { | ||
if (arr[i] > maxVal) { | ||
maxIndex = i; | ||
maxVal = arr[i]; | ||
} | ||
} | ||
|
||
reverse(arr, 0, maxIndex); | ||
res.push_back(maxIndex+1); | ||
reverse(arr, 0, n-1); | ||
res.push_back(n); | ||
sort(res, arr, n-1); | ||
} | ||
|
||
void reverse(vector<int> & arr, int i, int j) { | ||
while (i < j) { | ||
int temp = arr[i]; | ||
arr[i] = arr[j]; | ||
arr[j] = temp; | ||
i ++; | ||
j --; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
class Solution { | ||
// 记录反转操作序列 | ||
LinkedList<Integer> res = new LinkedList<>(); | ||
|
||
public List<Integer> pancakeSort(int[] cakes) { | ||
sort(cakes, cakes.length); | ||
return res; | ||
} | ||
|
||
void sort(int[] cakes, int n) { | ||
// base case | ||
if (n == 1) return; | ||
|
||
// 寻找最大饼的索引 | ||
int maxCake = 0; | ||
int maxCakeIndex = 0; | ||
for (int i = 0; i < n; i++) | ||
if (cakes[i] > maxCake) { | ||
maxCakeIndex = i; | ||
maxCake = cakes[i]; | ||
} | ||
|
||
// 第一次翻转,将最大饼翻到最上面 | ||
reverse(cakes, 0, maxCakeIndex); | ||
res.add(maxCakeIndex + 1); | ||
// 第二次翻转,将最大饼翻到最下面 | ||
reverse(cakes, 0, n - 1); | ||
res.add(n); | ||
|
||
// 递归调用 | ||
sort(cakes, n - 1); | ||
|
||
} | ||
|
||
void reverse(int[] arr, int i, int j) { | ||
while (i < j) { | ||
int temp = arr[i]; | ||
arr[i] = arr[j]; | ||
arr[j] = temp; | ||
i++; | ||
j--; | ||
} | ||
} | ||
} | ||
// 详细解析参见: | ||
// https://labuladong.github.io/article/?qno= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* struct TreeNode { | ||
* int val; | ||
* TreeNode *left; | ||
* TreeNode *right; | ||
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | ||
* }; | ||
*/ | ||
class Solution1 { | ||
public: | ||
bool isSymmetric(TreeNode* root) { | ||
return isMirror(root, root); | ||
} | ||
|
||
bool isMirror(TreeNode * left, TreeNode * right) { | ||
if ( !left && !right ) return true; | ||
if ( !left || !right ) return false; | ||
return ( left->val == right->val ) | ||
&& isMirror(left->left, right->right) | ||
&& isMirror(left->right, right->left); | ||
} | ||
}; | ||
|
||
class Solution { | ||
public: | ||
bool isSymmetric(TreeNode* root) { | ||
queue<TreeNode *> q; | ||
q.push(root); q.push(root); | ||
while ( !q.empty() ) { | ||
TreeNode * left = q.front(); q.pop(); | ||
TreeNode * right = q.front(); q.pop(); | ||
if ( !left && !right ) continue; | ||
if ( (!left || !right) || (left->val != right->val) ) return false; | ||
q.push(left->left); | ||
q.push(right->right); | ||
q.push(left->right); | ||
q.push(right->left); | ||
} | ||
return true; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Definition for a binary tree node. | ||
# class TreeNode(object): | ||
# def __init__(self, val=0, left=None, right=None): | ||
# self.val = val | ||
# self.left = left | ||
# self.right = right | ||
class Solution(object): | ||
def isSymmetric(self, root): | ||
""" | ||
:type root: TreeNode | ||
:rtype: bool | ||
""" | ||
# return self.is_mirror(root.left, root.right) | ||
return self.iterative(root) | ||
|
||
def is_mirror(self, left, right): | ||
if (left is None and right is None): | ||
return True | ||
if (left is None or right is None): | ||
return False | ||
return (left.val == right.val) and \ | ||
self.is_mirror(left.left, right.right) and \ | ||
self.is_mirror(left.right, right.left) | ||
|
||
def iterative(self, root): | ||
queue = [root, root] | ||
while len(queue) != 0: | ||
left = queue.pop(0) | ||
right = queue.pop(0) | ||
if (left is None and right is None): | ||
continue | ||
if (left is None or right is None) or (left.val != right.val): | ||
return False | ||
|
||
queue.append(left.left) | ||
queue.append(right.right) | ||
queue.append(left.right) | ||
queue.append(right.left) | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Solution: | ||
memo = dict() | ||
def fib(self, n: int) -> int: | ||
# if n == 0: return 0 | ||
# if n == 1: return 1 | ||
# return self.fib(n-1) + self.fib(n-2) | ||
# if n == 0 or n == 1: return n | ||
# if n in self.memo: return self.memo[n] | ||
# self.memo[n] = self.fib(n-1) + self.fib(n-2) | ||
# return self.memo[n] | ||
# if n == 0: return 0 | ||
# dp = [0] * (n+1) | ||
# dp[0] = 0 | ||
# dp[1] = 1 | ||
# for i in range(2, n+1): | ||
# dp[i] = dp[i-1] + dp[i-2] | ||
# return dp[n] | ||
if n == 0: return 0 | ||
a, b = 0, 1 | ||
for i in range(2, n+1): | ||
b, a = a + b, b | ||
return b |
Oops, something went wrong.