2016年12月5日 星期一

[LeetCode] 347. Top K Frequent Elements

轉自LeetCode

Given a non-empty array of integers, return the k most frequent elements.
For example,
Given [1,1,1,2,2,3] and k = 2, return [1,2].
Note: 
  • You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
  • Your algorithm's time complexity must be better than O(n log n), where n is the array's size.
<Solution>

2016年12月4日 星期日

[LeetCode] 33. Search in Rotated Sorted Array

轉自LeetCode

Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
<Solution>

[LeetCode] 155. Min Stack

轉自LeetCode

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • getMin() -- Retrieve the minimum element in the stack.
Example:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin();   --> Returns -3.
minStack.pop();
minStack.top();      --> Returns 0.
minStack.getMin();   --> Returns -2.
<Solution>

[LeetCode] 29. Divide Two Integers

轉自LeetCode

Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
<Solution>

2016年12月3日 星期六

[LeetCode] 28. Implement strStr()

轉自LeetCode

Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
<Solution>

[LeetCode] 27. Remove Element

轉自LeetCode

Given an array and a value, remove all instances of that value in place and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
Example:
Given input array nums = [3,2,2,3]val = 3
Your function should return length = 2, with the first two elements of nums being 2.
<Solution>

[LeetCode] 26. Remove Duplicates from Sorted Array

轉自LeetCode

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.

<Solution>