Leetcode 274: H-index

https://leetcode.com/problems/h-index/ H-Index Total Accepted: 10163 Total Submissions: 40365 Difficulty: Medium Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher’s h-index. According to the definition of h-index on Wikipedia: “A scientist has index h if h of his/her N papers have at least h …

Leetcode 280: Wiggle Sort

https://leetcode.com/problems/wiggle-sort/ Wiggle Sort Total Accepted: 1570 Total Submissions: 3651 Difficulty: Medium Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3]…. For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4]. Code: class Solution(object): def wiggleSort(self, nums): …

Leetcode 148: Sort List

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: …

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 …

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 “”” …

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 …