Leetcode 270: Closest Binary Search Tree Value

https://leetcode.com/problems/closest-binary-search-tree-value/ Closest Binary Search Tree Value Total Accepted: 2218 Total Submissions: 7604 Difficulty: Easy Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target. Note: Given target value is a floating point. You are guaranteed to have only one unique value in the …

Leetcode 275 : H-Index II

https://leetcode.com/discuss/56122/standard-binary-search H-Index II Total Accepted: 7373 Total Submissions: 23549 Difficulty: Medium Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm? Code 1:  class Solution(object): def hIndex(self, citations): “”” :type citations: List[int] :rtype: int “”” if citations is None or len(citations) == 0: return 0 for …

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 …