Get Target Number Using Number List and Arithmetic Operations

Given a list of numbers and a target number, write a program to determine whether the target number can be calculated by applying “+-*/” operations to the number list? You can assume () is automatically added when necessary. An operator should be put between each two consecutive numbers. So each number has to be used.

For example, given {1,2,3,4} and 21, return true. Because (1+2)*(3+4)=21

Read more

LeetCode – Lexicographical Numbers (Java)

Given an integer n, return 1 – n in lexicographical order. For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9]. Please optimize your algorithm to use less time and space. The input size may be as large as 5,000,000. Java Solution – DFS public List<Integer> lexicalOrder(int n) { int c=0; int t=n; while(t>0){ c++; t=t/10; }   ArrayList<Integer> … Read more

LeetCode – Count Numbers with Unique Digits (Java)

Given a non-negative integer n, count all numbers with unique digits, x, where x is from 0 to 10^n-1. Java Solution public int countNumbersWithUniqueDigits(int n) { int[] arr = new int[n+1]; arr[0]=1; // x can be 0   for(int i=1; i<=n; i++){ arr[i]=9; for(int j=9; j>=9-i+2; j–){ arr[i] *= j; } }   int result … Read more

LeetCode – Kth Largest Element in an Array (Java)

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array’s length. Java Solution 1 – Sorting … Read more

LeetCode – PDF Update History (Java)

5/10/2016 1. Add about 20 more problems. 2. Optimize solutions for some old problems. 5/17/2016 1. Add 12 more problems. 2. Optimize solutions for some old problems. 3. Remove some naive solutions because they are too obvious and have no values. 8/1/2016 1. Add 30 more problems. 2. Optimize solutions for some old problems. 3. … Read more

LeetCode – Palindrome Pairs (Java)

Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome. Example 1: Given words = [“bat”, “tab”, “cat”] Return [[0, 1], [1, 0]] The palindromes are [“battab”, “tabbat”] Java Solution public List<List<Integer>> … Read more

R hierarchical cluster data read from CSV file

The following code records how I load csv files to R and run hierarchical clustering algorithm on the data. I have to say that using R to plot the data is extremely EASY to do! > df <- read.csv("C:/Users/Creek/Desktop/data.txt", row.names=1,head=FALSE, sep=",") > d <- dist(as.matrix(df)) > hc <- hclust(d) > plot(hc) > q()> df <- … Read more

Install Pandas Without Root Access

If you want to install Pandas on Linux (Ubuntu, Redhat, etc.), but you don’t have root access, the installation may take a while. This is just record how I did it. After trying various kinds of different ways, I finally use one command to install it. Download Anaconda: http://continuum.io/downloads Change the download .sh file to … Read more

Use LISTAGG() on multiple joined table in Oracle

You may know how to use LISTAGG() on a single table, but don’t know how to use LISTAGG on multiple joined tables. This example demonstrates how to use aggregate function on joined multiple tables in Oracle 12g. Assuming we have the following two tables. “User” Table ID Name 111 aaa 222 bbb 333 ccc “Record” … Read more