But duplicate elements wasn't removed from the original array(list).
The requirement of this problem can't be satisfied completely in python because python doesn't allow delete elements when looping over a list!
Posts made by zenofpython
What's the problem with this python solution?
class Solution(object): def reverseList(self, head): """ :type head: ListNode :rtype: ListNode """ if not head: return head previous_node = None while True: next_node = head.next if previous_node: head.next = previous_node if not next_node: break previous_node = head head = next_node return head
I reviewed it through, but didn't find any bugs by myself. Why it failed to pass the exam?
Python super simple solutions, and fast!
class Solution(object): """ 600 / 600 test cases passed. Status: Accepted Runtime: 44 ms Submitted: 0 minutes ago """ def hammingWeight(self, n): """ :type n: int :rtype: int """ binary_str_n = bin(n) hamming_weight = binary_str_n.count('1') return hamming_weight
51 / 54 test cases passed mysterious error in my Python solution.
Today I changed a bit my Binary Tree Searching model to solve this problem.
The same model was tested by question 104, 235 and 257.
But I encountered a strange error looks like below, which confused me.
51 / 54 test cases passed. Status: Wrong Answer Submitted: 1 minute ago Input: [1,2], [1,null,2] Output: true Expected: false
My code will loop over all nodes of a root, if a node's left leaf and right leaf is both empty.
A back-retrieve path is logged. It seems the logic has no weakage. So I post this question.
My code looks like below:
class Solution(object): """ 209 / 209 test cases passed. Status: Accepted Runtime: 52 ms Submitted: 0 minutes ago """ def isSameTree(self, p, q): """ :type p: TreeNode :type q: TreeNode :rtype: bool """ return self.binaryTreePaths(p) == self.binaryTreePaths(q) def log_path(self, node): path =  while node.upper_node: path.append(node.val) node = node.upper_node path.append(node.val) # The last node has no upper_node, but its value should be include in path self.all_paths.append(tuple(path)) def process_leaves(self, node): left_leaf = node.left right_leaf = node.right if left_leaf: left_leaf.upper_node = node if right_leaf: right_leaf.upper_node = node if not left_leaf and not right_leaf: self.log_path(node) return left_leaf, right_leaf def traverse_analyze(self, process_results): for leaf in process_results: if leaf: process_results = self.process_leaves(leaf) self.traverse_analyze(process_results) def binaryTreePaths(self, root): """ :type root: TreeNode :rtype: List[str] """ self.all_paths =  if not root: # leetcode will pass None type in shamelessly here. return  root.upper_node = None process_results = self.process_leaves(root) self.traverse_analyze(process_results) return set(self.all_paths)