一、两数之和
public int[] twoSum(int[] nums, int target) {for (int i = 0; i < nums.length; i++) {for (int j = i + 1; j < nums.length; j++) {if (nums[i] + nums[j] == target) {return new int[]{i, j};}}}throw new IllegalArgumentException("No two sum solution");
}
二、反转字符串
方法一:使用StringBuilder或StringBuffer
可以使用 StringBuilder 或 StringBuffer 类的 reverse
方法来实现字符串的反转。
javaCopy codepublic String reverseString(String s) {StringBuilder sb = new StringBuilder(s);return sb.reverse().toString();
}
方法二:递归
通过递归,将字符串从后往前逐个字符添加到结果字符串中。
javaCopy codepublic String reverseString(String s) {if (s.isEmpty()) {return s;}return reverseString(s.substring(1)) + s.charAt(0);
}
三、判断回文数
public boolean isPalindrome(int x) {String str = String.valueOf(x);int left = 0;int right = str.length() - 1;while (left < right) {if (str.charAt(left) != str.charAt(right)) {return false;}left++;right--;}return true;
}
四、查找数组中的最大值和最小值
public class MaxMinFinder {public static void findMaxMin(int[] nums) {if (nums == null || nums.length == 0) {System.out.println("数组为空");return;}int max = nums[0];int min = nums[0];for (int num : nums) {max = Math.max(max, num);min = Math.min(min, num);}System.out.println("最大值:" + max);System.out.println("最小值:" + min);}public static void main(String[] args) {int[] nums = {4, 7, 2, 9, 1, 5};findMaxMin(nums);}
}
五、合并两个有序数组
public int[] mergeSortedArrays(int[] nums1, int m, int[] nums2, int n) {int p1 = m - 1, p2 = n - 1;int index = m + n - 1;while (p1 >= 0 && p2 >= 0) {if (nums1[p1] > nums2[p2]) {nums1[index--] = nums1[p1--];} else {nums1[index--] = nums2[p2--];}}while (p2 >= 0) {nums1[index--] = nums2[p2--];}return nums1;}
六、斐波那契数列
public int fibonacci(int n) {if (n <= 1) {return n;}return fibonacci(n - 1) + fibonacci(n - 2);
}
七、判断素数
public boolean isPrime(int n) {if (n <= 1) {return false;}for (int i = 2; i * i <= n; i++) {if (n % i == 0) {return false;}}return true;
}
八、字符串中第一个不重复字符
public char firstUniqueChar(String s) {if (s == null || s.length() == 0) {return ' ';}int[] count = new int[26];for (char c : s.toCharArray()) {count[c - 'a']++;}for (char c : s.toCharArray()) {if (count[c - 'a'] == 1) {return c;}}return ' ';
}
九、删除排序数组中的重复项
public int removeDuplicates(int[] nums) {if (nums.length == 0) {return 0;}int uniqueIndex = 0;for (int i = 1; i < nums.length; i++) {if (nums[i] != nums[uniqueIndex]) {uniqueIndex++;nums[uniqueIndex] = nums[i];}}return uniqueIndex + 1;
}
十、二分查找
public int binarySearch(int[] nums, int target) {int left = 0, right = nums.length - 1;while (left <= right) {int mid = left + (right - left) / 2;if (nums[mid] == target) {return mid;} else if (nums[mid] < target) {left = mid + 1;} else {right = mid - 1;}}return -1;
}
十一、冒泡排序
public class BubbleSort {public static void bubbleSort(int[] arr) {int n = arr.length;for (int i = 0; i < n - 1; i++) {for (int j = 0; j < n - i - 1; j++) {if (arr[j] > arr[j + 1]) {// 交换 arr[j] 和 arr[j + 1]int temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}}}}public static void main(String[] args) {int[] arr = {64, 34, 25, 12, 22, 11, 90};bubbleSort(arr);System.out.println("排序后的数组:" + Arrays.toString(arr));}
}