leetcode Find Median from Data Stream

leetcode Find Median from Data Stream

Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.

Examples:
[2,3,4] , the median is 3

[2,3], the median is (2 + 3) / 2 = 2.5

Design a data structure that supports the following two operations:

  • void addNum(int num) – Add a integer number from the data stream to the data structure.
  • double findMedian() – Return the median of all elements so far.

For example:

  • add(1)
  • add(2)
  • findMedian() -> 1.5
  • add(3)
  • findMedian() -> 2

Continue reading


leetcode Nim Game

leetcode Nim Game

You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.

Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.

For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.

Continue reading


leetcode 链表

本次题解包括:

  • 依照leetcode定义 自己写的链表创建/打印方法,便于测试
  • 19 Remove Nth Node From End of List
  • 21 Merge Two Sorted Lists
  • 24 Swap Nodes in Pairs
  • 25 Reverse Nodes in k-Group
  • 61 Rotate List
  • 138 Copy List with Random Pointer
  • 141 Linked List Cycle
  • 142 Linked List Cycle II
  • 143 Reorder List
  • 147 Insertion Sort List
  • 148 Sort List
  • 160 Intersection of Two Linked Lists
  • 203 Remove Linked List Elements
  • 206 Reverse Linked List
  • 234 Palindrome Linked List
  • 237 Delete Node in a Linked List

update:2015-7-14

  • add a 203 solution code:
    • using recursion and reference to delete linklist node which value equals x
    • 单项链表递归删除,使用引用

Continue reading