https://leetcode.com/problems/sort-list/ Sort List Total Accepted: 54042 Total Submissions: 237747 Difficulty: Medium Sort a linked list in O(n log n) time using constant space complexity. Code: # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def sortList(self, head): “”” :type head: ListNode :rtype: …
Author Archives: czxttkl
Leetcode 257: Binary Tree Path
https://leetcode.com/problems/binary-tree-paths/ Binary Tree Paths Total Accepted: 15076 Total Submissions: 68979 Difficulty: Easy Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: [“1->2->5”, “1->3”] Code1: # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, …
Leetcode 187: Repeated DNA Sequences
https://leetcode.com/problems/repeated-dna-sequences/ Repeated DNA Sequences Total Accepted: 26334 Total Submissions: 125642 Difficulty: Medium All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: “ACGAATTCCG”. When studying DNA, it is sometimes useful to identify repeated sequences within the DNA. Write a function to find all the 10-letter-long sequences (substrings) …
Leetcode 236: Lowest Common Ancestor of a Binary Tree
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Lowest Common Ancestor of a Binary Tree Total Accepted: 17453 Total Submissions: 63885 Difficulty: Medium Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the …
Continue reading “Leetcode 236: Lowest Common Ancestor of a Binary Tree”
Leetcode 226: Invert Binary Tree
Invert Binary Tree https://leetcode.com/problems/invert-binary-tree/ Total Accepted: 39609 Total Submissions: 100664 Difficulty: Easy Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell: Google: …
Leetcode 5: Longest Palindromic Substring
Longest Palindromic Substring https://leetcode.com/problems/longest-palindromic-substring/ Total Accepted: 71879 Total Submissions: 343504 Difficulty: Medium Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. Code: class Solution(object): def longestPalindrome(self, s): “”” :type s: str :rtype: str “”” …
Continue reading “Leetcode 5: Longest Palindromic Substring”
Compile Hadoop 2.7 on Eclipse on Ubuntu 15.04
Install libtool: sudo apt-get install libtool Install protoc 2.5: http://stackoverflow.com/questions/29797763/how-do-i-install-protobuf-2-5-on-arch-linux-for-compiling-hadoop-2-6-0-using-mav Git clone hadoop-common and compile using Maven then import into Eclipse: https://wiki.apache.org/hadoop/EclipseEnvironment Make sure Eclipse uses officially supported JRE: https://wiki.apache.org/hadoop/HadoopJavaVersions, http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-add_new_jre.htm You may still find some class cannot be resolved, or some import package can’t be resolved. This is due to that Maven generates some …
Continue reading “Compile Hadoop 2.7 on Eclipse on Ubuntu 15.04”
Python multiprocessing map function with shared memory object as additional parameter
Suppose you want to create a bunch of processes to consume a list of elements: import multiprocessing def consume(ele): return ele ** 2 if __name__ == ‘__main__’: m_list = [9,8,7,6,5] pool = multiprocessing.Pool(processes=300) result = pool.map(consume, m_list) print result Now, if you want to have a dictionary to be shared across processes, and pass …
Very important to set verbose output for Ubuntu User
I often mess up with Ubuntu System so much that I can’t manage to boot the system. I always end up with being stuck at the splash screen with no clue what is going on in the background. So, I suggest that for every ubuntu user you should turn on verbose output during the boot. …
Continue reading “Very important to set verbose output for Ubuntu User”
Leetcode 4: Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ Median of Two Sorted Arrays Total Accepted: 65901 Total Submissions: 385447 Difficulty: Hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Let me first show the code and then show the …