## LeetCode – Largest Number (Java)

Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. (Note: The result may be very large, so you need to return a string instead of an integer.) Analysis This problem can be solved by … Read more

## LeetCode – Intersection of Two Linked Lists (Java)

Problem Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 -> a2 -> c1 -> c2 -> c3 -> B: b1 -> b2 -> b3 begin to intersect at node c1. Java Solution First calculate the length of … Read more

## LeetCode – Longest Common Prefix (Java)

Problem Write a function to find the longest common prefix string amongst an array of strings. Analysis To solve this problem, we need to find the two loop conditions. One is the length of the shortest string. The other is iteration over every element of the string array. Java Solution public String longestCommonPrefix(String[] strs) { … Read more

## LeetCode – Best Time to Buy and Sell Stock III (Java)

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note: A transaction is a buy & a sell. You may not engage in multiple transactions at the same time … Read more

## LeetCode – Best Time to Buy and Sell Stock II (Java)

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in … Read more

## LeetCode – Best Time to Buy and Sell Stock (Java)

Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. Java Solution Instead of keeping track … Read more

## Spring MVC Tutorial: Process Form Submission

Spring Tutorial Index Page Previous – Dependency Injection Next – Form Validation This article shows how to process form in Spring. In this program, a form submission module will be added. Specifically, we will use web form to add a new employee and show the new employee list. Step 1: Prepare Required Classes Add addEmployee … Read more

## LeetCode – Combination Sum (Java)

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) will be positive integers. Elements in a combination (a1, a2, … , … Read more

## Spring MVC Tutorial – Setter Dependency Injection

Spring Tutorial Index Page Previous: Hello World Next: Handling Form Submission In previous tutorial, we have seen how to create a simple Hello World Spring application using Maven under Eclipse. In the Hello World application, we used annotation. In this tutorial, I will show how to use xml configuration and setter dependency injection. Instead of … Read more

## K-Means Clustering in Java

This post shows how to run k-means clustering algorithm in Java using Weka. First, download weka.jar file here. When it is unzipped, you have files like this: Add the weka.jar file to your project build path, and then take a look at the .arff file under data directory. By reading one or two of them, … Read more

## LeetCode – Majority Element (Java)

Given an array of size n, find the majority element. The majority element is the element that appears more than âŒŠ n/2 âŒ‹ times. (assume that the array is non-empty and the majority element always exist in the array.) Java Solution 1 – Sorting Assuming the majority exists and since the majority always takes more … Read more

## Spring MVC HelloWorld Using Maven in Eclipse

Java developers often rely on examples to learn Spring framework. Simple examples are often a key learning resource. There are many Spring MVC HelloWorld applications. However, most of them are outdated (do not integrate Maven, use old version of Spring, etc) or not complete (missing key steps or file hierarchy view). Therefore can not lead … Read more

## LeetCode – Reverse Words in a String (Java)

Given an input string, reverse the string word by word. For example, given s = “the sky is blue”, return “blue is sky the”. Java Solution This problem is pretty straightforward. We first split the string to words array, and then iterate through the array and add each element to a new string. Note: StringBuilder … Read more