Skip to content

Commit 131face

Browse files
author
sharanbale
committed
adding questions
1 parent 5e9d564 commit 131face

File tree

651 files changed

+35100
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

651 files changed

+35100
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
leet.py
2+

1.two-sum.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#
2+
# [1] Two Sum
3+
#
4+
# https://leetcode.com/problems/two-sum/description/
5+
#
6+
# algorithms
7+
# Easy (38.50%)
8+
# Total Accepted: 979.9K
9+
# Total Submissions: 2.5M
10+
# Testcase Example: '[2,7,11,15]\n9'
11+
#
12+
# Given an array of integers, return indices of the two numbers such that they
13+
# add up to a specific target.
14+
#
15+
# You may assume that each input would have exactly one solution, and you may
16+
# not use the same element twice.
17+
#
18+
# Example:
19+
#
20+
#
21+
# Given nums = [2, 7, 11, 15], target = 9,
22+
#
23+
# Because nums[0] + nums[1] = 2 + 7 = 9,
24+
# return [0, 1].
25+
#
26+
#
27+
#
28+
#
29+
#
30+
class Solution(object):
31+
def twoSum(self, nums, target):
32+
"""
33+
:type nums: List[int]
34+
:type target: int
35+
:rtype: List[int]
36+
"""
37+

10.regular-expression-matching.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#
2+
# [10] Regular Expression Matching
3+
#
4+
# https://leetcode.com/problems/regular-expression-matching/description/
5+
#
6+
# algorithms
7+
# Hard (24.36%)
8+
# Total Accepted: 209.9K
9+
# Total Submissions: 861.8K
10+
# Testcase Example: '"aa"\n"a"'
11+
#
12+
# Given an input string (s) and a pattern (p), implement regular expression
13+
# matching with support for '.' and '*'.
14+
#
15+
#
16+
# '.' Matches any single character.
17+
# '*' Matches zero or more of the preceding element.
18+
#
19+
#
20+
# The matching should cover the entire input string (not partial).
21+
#
22+
# Note:
23+
#
24+
#
25+
# s could be empty and contains only lowercase letters a-z.
26+
# p could be empty and contains only lowercase letters a-z, and characters like
27+
# . or *.
28+
#
29+
#
30+
# Example 1:
31+
#
32+
#
33+
# Input:
34+
# s = "aa"
35+
# p = "a"
36+
# Output: false
37+
# Explanation: "a" does not match the entire string "aa".
38+
#
39+
#
40+
# Example 2:
41+
#
42+
#
43+
# Input:
44+
# s = "aa"
45+
# p = "a*"
46+
# Output: true
47+
# Explanation: '*' means zero or more of the precedeng element, 'a'. Therefore,
48+
# by repeating 'a' once, it becomes "aa".
49+
#
50+
#
51+
# Example 3:
52+
#
53+
#
54+
# Input:
55+
# s = "ab"
56+
# p = ".*"
57+
# Output: true
58+
# Explanation: ".*" means "zero or more (*) of any character (.)".
59+
#
60+
#
61+
# Example 4:
62+
#
63+
#
64+
# Input:
65+
# s = "aab"
66+
# p = "c*a*b"
67+
# Output: true
68+
# Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore
69+
# it matches "aab".
70+
#
71+
#
72+
# Example 5:
73+
#
74+
#
75+
# Input:
76+
# s = "mississippi"
77+
# p = "mis*is*p*."
78+
# Output: false
79+
#
80+
#
81+
#
82+
class Solution(object):
83+
def isMatch(self, s, p):
84+
"""
85+
:type s: str
86+
:type p: str
87+
:rtype: bool
88+
"""
89+

100.same-tree.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#
2+
# [100] Same Tree
3+
#
4+
# https://leetcode.com/problems/same-tree/description/
5+
#
6+
# algorithms
7+
# Easy (48.11%)
8+
# Total Accepted: 281.3K
9+
# Total Submissions: 584.7K
10+
# Testcase Example: '[1,2,3]\n[1,2,3]'
11+
#
12+
# Given two binary trees, write a function to check if they are the same or
13+
# not.
14+
#
15+
# Two binary trees are considered the same if they are structurally identical
16+
# and the nodes have the same value.
17+
#
18+
# Example 1:
19+
#
20+
#
21+
# Input: 1 1
22+
# ⁠ / \ / \
23+
# ⁠ 2 3 2 3
24+
#
25+
# ⁠ [1,2,3], [1,2,3]
26+
#
27+
# Output: true
28+
#
29+
#
30+
# Example 2:
31+
#
32+
#
33+
# Input: 1 1
34+
# ⁠ / \
35+
# ⁠ 2 2
36+
#
37+
# ⁠ [1,2], [1,null,2]
38+
#
39+
# Output: false
40+
#
41+
#
42+
# Example 3:
43+
#
44+
#
45+
# Input: 1 1
46+
# ⁠ / \ / \
47+
# ⁠ 2 1 1 2
48+
#
49+
# ⁠ [1,2,1], [1,1,2]
50+
#
51+
# Output: false
52+
#
53+
#
54+
#
55+
# Definition for a binary tree node.
56+
# class TreeNode(object):
57+
# def __init__(self, x):
58+
# self.val = x
59+
# self.left = None
60+
# self.right = None
61+
62+
class Solution(object):
63+
def isSameTree(self, p, q):
64+
"""
65+
:type p: TreeNode
66+
:type q: TreeNode
67+
:rtype: bool
68+
"""
69+

101.symmetric-tree.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#
2+
# [101] Symmetric Tree
3+
#
4+
# https://leetcode.com/problems/symmetric-tree/description/
5+
#
6+
# algorithms
7+
# Easy (40.90%)
8+
# Total Accepted: 269.8K
9+
# Total Submissions: 659.5K
10+
# Testcase Example: '[1,2,2,3,4,4,3]'
11+
#
12+
# Given a binary tree, check whether it is a mirror of itself (ie, symmetric
13+
# around its center).
14+
#
15+
#
16+
# For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
17+
#
18+
# ⁠ 1
19+
# ⁠ / \
20+
# ⁠ 2 2
21+
# ⁠/ \ / \
22+
# 3 4 4 3
23+
#
24+
#
25+
#
26+
# But the following [1,2,2,null,3,null,3] is not:
27+
#
28+
# ⁠ 1
29+
# ⁠ / \
30+
# ⁠ 2 2
31+
# ⁠ \ \
32+
# ⁠ 3 3
33+
#
34+
#
35+
#
36+
#
37+
# Note:
38+
# Bonus points if you could solve it both recursively and iteratively.
39+
#
40+
#
41+
# Definition for a binary tree node.
42+
# class TreeNode(object):
43+
# def __init__(self, x):
44+
# self.val = x
45+
# self.left = None
46+
# self.right = None
47+
48+
class Solution(object):
49+
def isSymmetric(self, root):
50+
"""
51+
:type root: TreeNode
52+
:rtype: bool
53+
"""
54+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#
2+
# [102] Binary Tree Level Order Traversal
3+
#
4+
# https://leetcode.com/problems/binary-tree-level-order-traversal/description/
5+
#
6+
# algorithms
7+
# Medium (43.61%)
8+
# Total Accepted: 255.3K
9+
# Total Submissions: 585.4K
10+
# Testcase Example: '[3,9,20,null,null,15,7]'
11+
#
12+
# Given a binary tree, return the level order traversal of its nodes' values.
13+
# (ie, from left to right, level by level).
14+
#
15+
#
16+
# For example:
17+
# Given binary tree [3,9,20,null,null,15,7],
18+
#
19+
# ⁠ 3
20+
# ⁠ / \
21+
# ⁠ 9 20
22+
# ⁠ / \
23+
# ⁠ 15 7
24+
#
25+
#
26+
#
27+
# return its level order traversal as:
28+
#
29+
# [
30+
# ⁠ [3],
31+
# ⁠ [9,20],
32+
# ⁠ [15,7]
33+
# ]
34+
#
35+
#
36+
#
37+
# Definition for a binary tree node.
38+
# class TreeNode(object):
39+
# def __init__(self, x):
40+
# self.val = x
41+
# self.left = None
42+
# self.right = None
43+
44+
class Solution(object):
45+
def levelOrder(self, root):
46+
"""
47+
:type root: TreeNode
48+
:rtype: List[List[int]]
49+
"""
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#
2+
# [103] Binary Tree Zigzag Level Order Traversal
3+
#
4+
# https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/description/
5+
#
6+
# algorithms
7+
# Medium (37.56%)
8+
# Total Accepted: 146.7K
9+
# Total Submissions: 390.5K
10+
# Testcase Example: '[3,9,20,null,null,15,7]'
11+
#
12+
# Given a binary tree, return the zigzag level order traversal of its nodes'
13+
# values. (ie, from left to right, then right to left for the next level and
14+
# alternate between).
15+
#
16+
#
17+
# For example:
18+
# Given binary tree [3,9,20,null,null,15,7],
19+
#
20+
# ⁠ 3
21+
# ⁠ / \
22+
# ⁠ 9 20
23+
# ⁠ / \
24+
# ⁠ 15 7
25+
#
26+
#
27+
#
28+
# return its zigzag level order traversal as:
29+
#
30+
# [
31+
# ⁠ [3],
32+
# ⁠ [20,9],
33+
# ⁠ [15,7]
34+
# ]
35+
#
36+
#
37+
#
38+
# Definition for a binary tree node.
39+
# class TreeNode(object):
40+
# def __init__(self, x):
41+
# self.val = x
42+
# self.left = None
43+
# self.right = None
44+
45+
class Solution(object):
46+
def zigzagLevelOrder(self, root):
47+
"""
48+
:type root: TreeNode
49+
:rtype: List[List[int]]
50+
"""
51+

0 commit comments

Comments
 (0)