LeetCode Java Base Knowledge Sheetcheet (Continuously Updating...)

🔥 LeetCode Java Base Knowledge Summary

(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

MethodFunctionExample
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

MethodFunctionExample
s.split(" ")Split string into String[] by whitespace"a b c".split(" ") → ["a", "b", "c"]
String.join(" ", arr)Join string array with a separatorString.join("-", ["a", "b", "c"]) → "a-b-c"
s.toCharArray()Convert string to char[]"abc".toCharArray() → ['a', 'b', 'c']

1.3 Modifying Strings

MethodFunctionExample
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 = new StringBuilder(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(new String(arr)); // "olleh"

📌 2️⃣ Array Operations

2.1 Basic Array Operations

MethodFunctionExample
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

MethodFunctionExample
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 = new ArrayList<>();
list.add(5);
System.out.println(list.get(0)); // 5

3.2 HashSet (Remove Duplicates)

Set<Integer> set = new HashSet<>();
set.add(5);
System.out.println(set.contains(5)); // true

3.3 HashMap (Key-Value Pairs)

Map<Integer, String> map = new HashMap<>();
map.put(1, "one");
System.out.println(map.get(1)); // "one"

🚀 Summary: Essential Java Knowledge for LeetCode

CategoryKey Knowledge
Stringsplit(), toCharArray(), StringBuilder
ArrayArrays.sort(), Arrays.fill()
CollectionsArrayList, HashMap, HashSet
QueueQueue, Deque
HeapPriorityQueue
SortingComparator, Collections.sort()
Dynamic Programmingdp[], dp[][]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值