1、判断链表是否是环形链表(HashSet、双指针)
public class CircularLinkedList {
public static void main(String[] args) {
ListNode node5 = new ListNode(5, null);
ListNode node4 = new ListNode(4, node5);
ListNode node3 = new ListNode(3, node4);
ListNode node2 = new ListNode(2, node3);
ListNode node1 = new ListNode(1, node2);
node5.next = node2;
System.out.println(hasCycle(node1));
System.out.println(hasCycle2(node1));
}
public static boolean hasCycle(ListNode head) {
Set<ListNode> set = new HashSet<>();
ListNode temp = head;
while (temp != null) {
if (!set.add(temp)) {
return true;
}
temp = temp.next;
}
return false;
}
public static boolean hasCycle2(ListNode head) {
if (head == null && head.next == null) {
return false;
}
ListNode slow = head;
ListNode quick = head.next;
while (slow != quick) {
if (quick == null || quick.next == null) {
return false;
}
slow = slow.next;
quick = quick.next.next;
}
return true;
}
static class ListNode {
public int value;
public ListNode next;
public ListNode(int value, ListNode next) {
this.value = value;
this.next = next;
}
}
}
2、合并两个有序数组(JavaAPI、双指针)
public class MergeTwoOrderedArrays {
public static void main(String[] args) {
int[] nums1 = new int[]{1, 3, 5, 7, 9, 0, 0, 0, 0};
int[] nums2 = new int[]{2, 4, 6, 8};
System.out.println(Arrays.toString(towPointsMerge2(nums1, 5, nums2, 4)));
}
public static int[] merge(int[] nums1, int m, int[] nums2, int n) {
System.arraycopy(nums2, 0, nums1, m, n);
Arrays.sort(nums1);
return nums1;
}
public static int[] towPointsMerge1(int[] nums1, int m, int[] num2, int n) {
int[] nums1_copy = new int[m];
System.arraycopy(nums1, 0, nums1_copy, 0, m);
int p1 = 0;
int p2 = 0;
int p = 0;
while (p1 < m && p2 < n) {
nums1[p++] = nums1_copy[p1] < num2[p2] ? nums1_copy[p1++] : num2[p2++];
}
if (p1 < m) {
System.arraycopy(nums1_copy, p1, nums1, p1 + p2, m + n - p1 - p2);
}
if (p2 < n) {
System.arraycopy(num2, p2, nums1, p1 + p2, m + n - p1 - p2);
}
return nums1;
}
public static int[] towPointsMerge2(int[] nums1, int m, int[] nums2, int n) {
int p1 = m - 1;
int p2 = n - 1;
int p = m + n - 1;
while (p1 >= 0 && p2 >= 0) {
nums1[p--] = nums1[p1] > nums2[p2] ? nums1[p1--] : nums2[p2--];
}
System.arraycopy(nums2, 0, nums1, 0, p2 + 1);
return nums1;
}
}
3、子数组最大平均数(滑动窗口)
public class SubarrayMaxAverageNumber {
public static void main(String[] args) {
int[] arr = {1, 12, -5, -6, 50, 3};
System.out.println(findMaxAverage(arr, 4));
}
public static double findMaxAverage(int[] arr, int k) {
int sum = 0;
for (int i = 0; i < k; i++) {
sum += arr[i];
}
int max = sum;
for (int i = k; i < arr.length; i++) {
sum = sum + arr[i] - arr[i - k];
max = Math.max(max, sum);
}
return 1.0 * max / k;
}
}
4、最长连续递增序列
public class LongestContinuousIncreasingSequence {
public static void main(String[] args) {
int[] arr = new int[]{1, 2, 1, 2, 3, 1, 2, 3, 4};
System.out.println(findLength(arr));
}
public static int findLength(int[] arr) {
int start = 0;
int max = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i - 1] >= arr[i]) {
start = i;
}
max = Math.max(i - start + 1, max);
}
return max;
}
}
5、柠檬水找零
public class LemonadeChange {
public static void main(String[] args) {
int[] arr = new int[]{5, 5, 10, 20};
System.out.println(change(arr));
}
public static boolean change(int[] arr) {
int five = 0, ten = 0;
for (int i : arr) {
if (i == 5) {
five++;
} else if (i == 10) {
if (five > 0) {
five--;
ten++;
} else {
return false;
}
} else {
if (five > 0 && ten > 0) {
five--;
ten--;
} else if (five >= 3) {
five -= 3;
} else {
return false;
}
}
}
return true;
}
}
6、三角形的最大周长
public class TriangleMaxPerimeter {
public static void main(String[] args) {
int[] arr = new int[]{1, 2, 4, 8, 3};
System.out.println(findMaxPerimeter(arr));
}
public static int findMaxPerimeter(int[] arr) {
int len = arr.length;
if (arr.length >= 3) {
Arrays.sort(arr);
for (int i = len - 1; i >= 2; i--) {
if (arr[i - 2] + arr[i - 1] > arr[i]) {
return arr[i - 2] + arr[i - 1] + arr[i];
}
}
}
return 0;
}
}