(Below contents are generated with LLM tools.) When solving LeetCode problems in Java, you need to be familiar with string operations, arrays, collections, algorithms, and data structures. Below is a list of essential Java concepts.
📌 1️⃣ String Operations
1.1 Basic String Operations
Method
Function
Example
s.length()
Get string length
"abc".length() → 3
s.charAt(i)
Get character at index i
"abc".charAt(1) → 'b'
s.equals(t)
Compare two strings
"abc".equals("abc") → true
s.compareTo(t)
Compare strings lexicographically
"abc".compareTo("bcd") → -1
1.2 Splitting & Joining Strings
Method
Function
Example
s.split(" ")
Split string into String[] by whitespace
"a b c".split(" ") → ["a", "b", "c"]
String.join(" ", arr)
Join string array with a separator
String.join("-", ["a", "b", "c"]) → "a-b-c"
s.toCharArray()
Convert string to char[]
"abc".toCharArray() → ['a', 'b', 'c']
1.3 Modifying Strings
Method
Function
Example
s.trim()
Remove leading & trailing spaces
" abc ".trim() → "abc"
s.replace("a", "x")
Replace all occurrences of a character
"abc".replace("a", "x") → "xbc"
s.replaceAll("\\s+", " ")
Replace multiple spaces with a single space
"a b".replaceAll("\\s+", " ") → "a b"
1.4 Reversing a String
Method 1: built-in methods
String s ="hello";String reversed =newStringBuilder(s).reverse().toString();System.out.println(reversed);// "olleh"
Method 2: double pointers
char[] arr = s.toCharArray();int left =0, right = arr.length -1;while(left < right){char temp = arr[left];
arr[left++]= arr[right];
arr[right--]= temp;}System.out.println(newString(arr));// "olleh"
📌 2️⃣ Array Operations
2.1 Basic Array Operations
Method
Function
Example
int[] arr = new int[10]
Initialize an array
[0,0,0,0,0,0,0,0,0,0]
Arrays.fill(arr, 1)
Fill the array with a value
[1,1,1,1,1]
Arrays.toString(arr)
Convert the array into a string
"[1,1,1,1,1]"
2.2 Sorting
Method
Function
Example
Arrays.sort(arr)
Sort an array in ascending order
[3,2,1] → [1,2,3]
Arrays.sort(arr, Collections.reverseOrder())
Sort in descending order
[3,2,1] → [3,2,1]
2.3 Copying an Array
int[] original ={1,2,3};int[] copy =Arrays.copyOf(original,5);System.out.println(Arrays.toString(copy));// [1,2,3,0,0]
📌 3️⃣ Collection Framework
3.1 ArrayList (Dynamic Array)
List<Integer> list =newArrayList<>();
list.add(5);System.out.println(list.get(0));// 5
3.2 HashSet (Remove Duplicates)
Set<Integer> set =newHashSet<>();
set.add(5);System.out.println(set.contains(5));// true