### Popular posts

- Leetcode problems classified by company
- Python Queue examples
- Start, Restart and Stop Apache web server on Linux
- Hive Case statement examples
- Set variable for hive script
- Pyspark broadcast variable Example
- Adding Multiple Columns to Spark DataFrames
- pySpark check if file exists
- Chi Square test for feature selection
- Five ways to implement Singleton pattern in Java
- Move Hive Table from One Cluster to Another
- use spark to calculate moving average for time series data
- A Spark program using Scopt to Parse Arguments
- Convert infix notation to reverse polish notation (Java)
- spark submit multiple jars

### Recent Posts

- Most Popular Deep Learning Projects
- Good articles to learn Convolution Neural Networks
- Machine learning in 10 pictures
- Good resources to learn how to use websocket push api in python
- Best examples to learn machine learning
- feature engineering in PySpark
- Good resources to learn auto trade backtest
- visualize iris dataset using python
- Adding Multiple Columns to Spark DataFrames
- 如何在 Kaggle 首战中进入前 10%
- Using Azure ML to Build Clickthrough Prediction Models
- Data Transformation methods: one hot encoding, learning with counts
- 用python参加Kaggle的经验总结
- An Introduction to Stock Market Data Analysis with Python
- use spark to calculate moving average for time series data

### Algorithms

- Good articles to learn Convolution Neural Networks
- Good resources to learn VC dimension
- Calling an external command in Python
- Hive Case statement examples
- [LeetCode] Android Unlock Patterns
- Find median of a infinite stream of integers
- [Leetcode] Fraction to Recurring Decimal
- Ones and Zeroes
- Darts, Dice, and Coins: Sampling from a Discrete Distribution
- The Alias Method for weighted sampling

### Tags

### Machine Learning

- Most Popular Deep Learning Projects
- Machine learning in 10 pictures
- Best examples to learn machine learning
- visualize iris dataset using python
- 如何在 Kaggle 首战中进入前 10%
- Using Azure ML to Build Clickthrough Prediction Models
- Data Transformation methods: one hot encoding, learning with counts
- 用python参加Kaggle的经验总结
- An Introduction to Stock Market Data Analysis with Python
- Good Articles to learn how to implement a neural network 1

### Leetcode Solutions

### Remove Element from an Array (Java)

LeetCode – Remove Element

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.

Analysis

This problem is similar to another leetcode problem: Remove Duplicates from Sorted Array.

[Read More...]### Remove Duplicates from Sorted Array (Java)

Leetcode: Remove Duplicates from Sorted Array

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.

Analysis

We defined two indexes: pre and cur.

[Read More...]### LeetCode- Search in Rotated Sorted Array

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.

Using the idea of binary search, we can reduce the search space based on which range the target value is located.

We first get the mid index of the array,

[Read More...]### Leetcode- Rotate Array to right by K steps (java)

Rotate an array of n elements to the right by k steps.

For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].

How many different ways do you know to solve this problem?

Analysis

It is easy to use an intermediate array to solve this problem. Suppose the original array is A, the new array is B, we will copy

A[0:n-k -1] to B[k:n-1], and A[n – k:n-1] to B[0:k – 1], then we copy all the elements in B back to Array A using System.arrayCopy.

[Read More...]### LeetCode- Evaluate Reverse Polish Notation (Java)

[LeetCode] Evaluate Reverse Polish Notation (RPN)

Evaluate the value of an arithmetic expression in Reverse Polish Notation.

Valid operators are +, -, *, /. Each operand may be an integer or another expression.

Some examples:

1 2 |
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6 |

Analysis

Evaluate RPN is the second step to build a calculator. Once we have converted the infix expression into the RPN, we need to evaluate RPN to get the result.

The basic idea to evaluate reverse polish notation is to use a stack to process the strings.

We scan the array of the RPN expression.

[Read More...]### Leetcode Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

1 2 3 4 5 6 |
1 / \ 2 2 / \ / \ 3 4 4 3 |

But the following [1,2,2,null,3,null,3] is not:

1 2 3 4 5 6 |
1 / \ 2 2 \ \ 3 3 |

Note:

Bonus points if you could solve it both recursively and iteratively.

Analysis

This problem can be easily solved using a recursive algorithm. We defined a method with the following signature:

equal(left, right)

then we recursively call equal(left.left, right.right) and equal(left.right, right.left).

The method returns false as long as only one of the two nodes is null,

[Read More...]### Leetcode Sliding Window Maximum

Given an array *nums*, there is a sliding window of size *k* which is moving from the very left of the array to the very right. You can only see the*k* numbers in the window. Each time the sliding window moves right by one position.

For example,

Given *nums* = [1,3,-1,-3,5,3,6,7], and *k* = 3.

1 2 3 4 5 6 7 8 9 |
Window position Max --------------- ----- [1 3 -1] -3 5 3 6 7 3 1 [3 -1 -3] 5 3 6 7 3 1 3 [-1 -3 5] 3 6 7 5 1 3 -1 [-3 5 3] 6 7 5 1 3 -1 -3 [5 3 6] 7 6 1 3 -1 -3 5 [3 6 7] 7 |

Therefore, return the max sliding window as [3,3,5,5,6,7].

Note:

You may assume *k* is always valid, ie: 1 ≤ k ≤ input array’s size for non-empty array.

### LeetCode Shuffle an Array (Java)

Shuffle a set of numbers without duplicates.

Example:

1 2 3 4 5 6 7 8 9 10 11 12 13 |
// Init an array with set 1, 2, and 3. int[] nums = {1,2,3}; Solution solution = new Solution(nums); // Shuffle the array [1,2,3] and return its result. Any permutation of [1,2,3] must equally likely to be returned. solution.shuffle(); // Resets the array back to its original configuration [1,2,3]. solution.reset(); // Returns the random shuffling of array [1,2,3]. solution.shuffle(); |

Java Solution

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
public class Solution { int[] origin; int[] res; Random r; public Solution(int[] nums) { origin = nums; res = Arrays.copyOf(nums, nums.length); r = new Random(); } /** Resets the array to its original configuration and return it. */ public int[] reset() { res = Arrays.copyOf(origin, origin.length); return res; } /** Returns a random shuffling of the array. */ public int[] shuffle() { int right = res.length - 1; int j; while(right > 0) { j = r.nextInt(right + 1); swap(res, j, right); right--; } return res; } void swap(int[] A, int i, int j) { int tmp = A[i]; A[i] = A[j]; A[j] = tmp; } } /** * Your Solution object will be instantiated and called as such: * Solution obj = new Solution(nums); * int[] param_1 = obj.reset(); * int[] param_2 = obj.shuffle(); */ |

[Read More...]

### LeetCode Shortest Word Distance I II and III

Shortest Word Distance I

Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.

For example, Assume that words = [“practice”, “makes”, “perfect”, “coding”, “makes”].

Given word1 = “coding”, word2 = “practice”, return 3. Given word1 = “makes”, word2 = “coding”, return 1.

Note

You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.

Analysis

We can scan the list and use two pointers to record the most recent indexes of the two words.

[Read More...]### LeetCode Serialize and Deserialize Binary Tree (Java)

Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.

Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.

For example, you may serialize the following tree

1 2 3 4 5 6 |
1 / \ 2 3 / \ 4 5 |

as “[1,2,3,null,null,4,5]”,

[Read More...]### Related posts:

- Index
- Best websites to learn java programming
- Chi Square test for feature selection
- Learn Spark by Examples
- pyspark unit test based on python unittest library
- Scala read file examples
- Set variable for hive script
- Leetcode – Maximum Depth of Binary Tree
- LeetCode Serialize and Deserialize Binary Tree (Java)
- pySpark check if file exists
- How to setup ipython notebook server to run spark in local or yarn model
- Count word frequency