Skip to content

Instantly share code, notes, and snippets.

Desolve

Block or report user

Report or block Desolve

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@Desolve
Desolve / 1008 Construct Binary Search Tree from Preorder Traversal.java
Created Jun 5, 2020
1008 Construct Binary Search Tree from Preorder Traversal
View 1008 Construct Binary Search Tree from Preorder Traversal.java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
@Desolve
Desolve / 1008 Construct Binary Search Tree from Preorder Traversal.py
Created Jun 5, 2020
1008 Construct Binary Search Tree from Preorder Traversal
View 1008 Construct Binary Search Tree from Preorder Traversal.py
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
i = 0
def bstFromPreorder(self, A, bound=float('inf')):
View 1143 Longest Common Subsequence.java
class Solution {
public int longestCommonSubsequence(String t1, String t2) {
int l1 = t1.length(), l2 = t2.length();
int[][] dp = new int[l1+1][l2+1];
for (int i = 1; i <= l1; ++i) {
for (int j = 1; j <= l2; ++j) {
if (t1.charAt(i-1) == t2.charAt(j-1)) {
dp[i][j] = dp[i-1][j-1] + 1;
} else {
dp[i][j] = Math.max(dp[i][j-1], dp[i-1][j]);
@Desolve
Desolve / 1143 Longest Common Subsequence.py
Created Jun 5, 2020
1143 Longest Common Subsequence
View 1143 Longest Common Subsequence.py
class Solution:
def longestCommonSubsequence(self, t1: str, t2: str) -> int:
l1, l2 = len(t1), len(t2)
dp = [[0]*(l2+1) for _ in range(l1+1)]
for i in range(1, l1+1):
for j in range(1, l2+1):
if t1[i-1] == t2[j-1]:
dp[i][j] = dp[i-1][j-1] +1
else:
dp[i][j] = max(dp[i][j-1], dp[i-1][j])
@Desolve
Desolve / 0438 Find All Anagrams in a String.py
Created May 24, 2020
0438 Find All Anagrams in a String
View 0438 Find All Anagrams in a String.py
class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
res = []
if not s or not p or len(s) < len(p): return res
pHash = {}
for c in 'abcdefghijklmnopqrstuvwxyz':
pHash[c] = 0
for c in p:
pHash[c] += 1
cnt, l, r = len(p), 0, 0
@Desolve
Desolve / 0438 Find All Anagrams in a String.java
Created May 24, 2020
0438 Find All Anagrams in a String
View 0438 Find All Anagrams in a String.java
class Solution {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> list = new ArrayList<>();
if (s == null || s.length() == 0 || p == null || p.length() == 0 || s.length() < p.length()) return list;
int[] hash = new int[26];
for (char c : p.toCharArray()) {
hash[c - 'a']++;
}
int left = 0, right = 0, count = p.length();
while (right < s.length()) {
View 0687 Longest Univalue Path.py
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def __init__(self):
self.mx = 0
View 0687 Longest Univalue Path.java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
View 0680 Valid Palindrome II.py
class Solution:
def validPalindrome(self, s: str) -> bool:
def isp(s: str, l: int, r: int) -> bool:
while l < r:
if s[l] != s[r]: return False
l, r = l + 1, r - 1
return True
i, j = 0, len(s) - 1
while i < j:
View 0680 Valid Palindrome II.java
class Solution {
public boolean validPalindrome(String s) {
char[] arr = s.toCharArray();
for (int i = 0, j = arr.length - 1; i < j; ++i, --j)
if (arr[i] != arr[j]) return isp(arr, i + 1, j) || isp(arr, i, j - 1);
return true;
}
private boolean isp(char[] arr, int l, int r) {
for (; l < r; ++l, --r)
You can’t perform that action at this time.