https://leetcode.com/problems/scramble-string/ Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / \ gr eat / \ / \ g r e at / \ a t To scramble the string, we may choose any …
Author Archives: czxttkl
Leetcode 147: Insertion Sort List
https://leetcode.com/problems/insertion-sort-list/ Sort a linked list using insertion sort. Code # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def insertionSortList(self, head): “”” :type head: ListNode :rtype: ListNode “”” new_head = ListNode(0) cur = head while cur: isrt = new_head next = …
Leetcode 6: ZigZag Conversion
https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSIIGYIR" Write the …
Leetcode 164: Maximum Gap
https://leetcode.com/problems/maximum-gap/ Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in linear time/space. Return 0 if the array contains less than 2 elements. You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range. Code …
Leetcode 71: Simplify Path
https://leetcode.com/problems/simplify-path/ Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c" click to show corner cases. Corner Cases: Did you consider the case where path = "/../"?In this case, you should return "/". Another corner case is the path might contain multiple slashes '/' together, …
Leetcode 89: Gray Code
https://leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0. For example, given n = 2, return [0,1,3,2]. Its …
Leetcode 95: Unique Binary Search Trees II
https://leetcode.com/problems/unique-binary-search-trees-ii/ Given n, generate all structurally unique BST’s (binary search trees) that store values 1…n. For example,Given n = 3, your program should return all 5 unique BST’s shown below. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3 …
Continue reading “Leetcode 95: Unique Binary Search Trees II”
Leetcode 96: Unique Binary Search Trees
https://leetcode.com/problems/unique-binary-search-trees/ Given n, how many structurally unique BST’s (binary search trees) that store values 1…n? For example,Given n = 3, there are a total of 5 unique BST’s. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3 Code …
Leetcode 97: Interleaving String
https://leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = "aabcc",s2 = "dbbca", When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc", return false. Code class Solution(object): def isInterleave(self, s1, s2, s3): “”” :type s1: str :type s2: str :type s3: str :rtype: bool …
Leetcode 101: Symmetric Tree
https://leetcode.com/problems/symmetric-tree/ Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the following is not: 1 / \ 2 2 \ \ 3 3 Note:Bonus points if …