If we want to copy an array, we can use either System.arraycopy()
or Arrays.copyOf()
. In this post, I use a simple example to demonstrate the difference between the two.
ProgramCreek
LeetCode – Odd Even Linked List (Java)
Problem
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.
Implement a Stack Using an Array in Java
This post shows how to implement a stack by using an array.
The requirements of the stack are: 1) the stack has a constructor which accepts a number to initialize its size, 2) the stack can hold any type of elements, 3) the stack has a push() and a pop() method.
LeetCode – Queue Reconstruction by Height (Java)
Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this person who have a height greater than or equal to h. Write an … Read more
Rotate Array in 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? Solution 1 – Intermediate Array In a straightforward way, we can create a new array … Read more
LeetCode – Increasing Triplet Subsequence (Java)
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Examples: Given [1, 2, 3, 4, 5], return true. Given [5, 4, 3, 2, 1], return false. Analysis This problem can be formalized as finding a sequence x, y and z, such that x < y < ... Read more
LeetCode – Patching Array (Java)
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in range [1, n] inclusive can be formed by the sum of some elements in the array. Return the minimum number of patches required. Example 1: nums = [1, 3], n = 6 Return 1. … Read more
Java Remove Element from ArrayList
To remove some elements from an ArrayList while iterating over the ArrayList, we need to use Iterator. Integer[] arr = {1,2,3,4,5,6}; ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(arr)); System.out.println(list); Iterator<Integer> iter = list.iterator(); while(iter.hasNext()){ int i = iter.next(); if(i==5) iter.remove(); } System.out.println(list);Integer[] arr = {1,2,3,4,5,6}; ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(arr)); System.out.println(list); Iterator<Integer> iter = list.iterator(); … Read more
LeetCode – Number of Islands II (Java)
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands after each addLand operation. An island is surrounded by water and … Read more
LeetCode – Find Median from Data Stream (Java)
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. Analysis First of all, it seems that the best time complexity we can get for this problem is O(log(n)) of add() … Read more
How to determine if a string is English or Java code?
Consider the following two strings:
1. for (int i = 0; i < b.size(); i++) {
2. do something in English (not necessary to be a sentence).
LeetCode – Verify Preorder Serialization of a Binary Tree (Java)
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node’s value. If it is a null node, we record using a sentinel value such as #. 9 / \ 3 2 / \ / \ 4 1 # 6 / \ / \ … Read more
Download Large Open Source Java Code Corpus
If you want to dump github repositories or download a large number of open source Java projects, here is the script. There are 2 files required to dump the corpus. The first is “repo-list.txtâ€. It contains the 39020 Java project names. The second file is “script-zip.shâ€. It is the shell script that run git to … Read more
Why do we need generic methods in Java?
1. A Method without Generic Type Assuming you want to write a method that takes two sets and get their intersection. Here is one way to write such a method: public static Set getIntersection(Set set1, Set set2){ Set result = new HashSet(); for(Object o: set1){ if(set2.contains(o)) result.add(o); } return result; }public static Set … Read more
Get Programming Language Keywords By Using Regular Expression in Java
We can use regular expression to get all Java keywords in a program. The key is using word boundary correctly. For example, given “static staticField”, the first word should be recognized as a keyword but the second should not.