This list summarizes the top 10 mistakes that Java developers frequently make.
ProgramCreek
LeetCode – Nested List Weight Sum (Java)
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. Each element is either an integer, or a list — whose elements may also be integers or other lists. Example 1: Given the list [[1,1],2,[1,1]], return 10. (four 1’s at depth 2, one 2 at depth … Read more
LeetCode – Remove Invalid Parentheses (Java)
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ). Examples: “()())()” -> [“()()()”, “(())()”] “(a)())()” -> [“(a)()()”, “(a())()”] “)(” -> [“”] Java Solution This problem can be solve by using DFS. … Read more
LeetCode – Number of Connected Components in an Undirected Graph (Java)
Given n nodes labeled from 0 to n – 1 and a list of undirected edges (each edge is a pair of nodes), write a function to find the number of connected components in an undirected graph. For example: 0 3 | | 1 — 2 4 Given n = 5 and edges = [[0, … Read more
LeetCode – Reverse Nodes in k-Group (Java)
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nodes itself may … Read more
LeetCode – Super Ugly Number (Java)
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of the first 12 super ugly numbers given … Read more
LeetCode – Paint House II (Java)
There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color. The cost of painting each house with a … Read more
LeetCode – Paint House (Java)
There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color. The cost of painting … Read more
LeetCode – Text Justification (Java)
Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ‘ ‘ when necessary … Read more
LeetCode – Unique Paths (Java)
A robot is located at the top-left corner of a m x n grid. It can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid. How many possible unique paths are there? Java Solution 1 – DFS A depth-first search solution … Read more
LeetCode – Minimum Path Sum (Java)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Java Solution 1: Depth-First Search A native solution would be depth-first search. It’s time is too expensive and fails the online judgement. public int minPathSum(int[][] grid) … Read more
LeetCode – Minimum Size Subarray Sum (Java)
Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn’t one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7, the subarray [4,3] has the minimal length of 2 under the problem constraint. … Read more
LeetCode – H-Index II (Java)
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm? Java Solution Given the array is sorted, we should use binary search. int hIndex(int[] citations) { int len = citations.length; if (len == 0) { return 0; } if (len == 1) { if … Read more
LeetCode – Basic Calculator II (Java)
Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero. You may assume that the given expression is always valid. Some examples: “3+2*2” = 7 Java Solution public int calculate(String s) { … 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