1、单链表的反转(迭代、递归)
public class ReverseSingleList {
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);
System.out.println("反转前");
System.out.println(node1);
System.out.println("反转后");
System.out.println("迭代:"+iterator(node1));
}
public static ListNode iterator(ListNode head) {
ListNode next, prev = null;
ListNode temp = head;
while (temp != null) {
next = temp.next;
temp.next = prev;
prev = temp;
temp = next;
}
return prev;
}
public static ListNode recursion(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode newHead = recursion(head.next);
head.next.next = head;
head.next = null;
return newHead;
}
static class ListNode {
int val;
ListNode next;
public ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
@Override
public String toString() {
return "ListNode{" +
"val=" + val +
", next=" + next +
'}';
}
}
}
2、统计n以内的素数个数(暴力法、埃筛法)
public class PrimeCount {
public static void main(String[] args) {
System.out.println("暴力法:" + bf(100));
System.out.println("埃筛法:" + eratosthenes(100));
}
public static int bf(int n) {
int count = 0;
for (int i = 2; i < n; i++) {
if (isPrime(i)) {
count++;
}
}
return count;
}
private static boolean isPrime(int i) {
for (int j = 2; j * j <= i; j++) {
if (i % j == 0) {
return false;
}
}
return true;
}
public static int eratosthenes(int n) {
boolean[] flag = new boolean[n];
int count = 0;
for (int i = 2; i < n; i++) {
if (!flag[i]) {
count++;
for (int j = i * i; j < n; j = j + i) {
flag[j] = true;
}
}
}
return count;
}
}
3、删除排序数组中的重复项(双指针法)
public class RemoveSortedArrayDuplicates {
public static void main(String[] args) {
int[] arr = new int[]{1, 2, 3, 3, 4, 4, 5, 5, 5};
System.out.println(twoPoints(arr));
}
public static int twoPoints(int[] arr) {
if (arr.length == 0) {
return 0;
}
int i = 0;
for (int j = 1; j < arr.length; j++) {
if (arr[i] != arr[j]) {
i++;
arr[i] = arr[j];
}
}
return i + 1;
}
}
4、寻找数组的中心下标
public class FindArrayCenterIndex {
public static void main(String[] args) {
int[] arr = new int[]{1,7,3,6,5,6};
System.out.println(findCenterIndex(arr));
}
public static int findCenterIndex(int[] arr){
int sum = 0;
for (int i : arr) {
sum += i;
}
int rightTotal = sum;
int leftTotal = 0;
for (int i = 0; i < arr.length; i++) {
leftTotal += arr[i];
if(leftTotal == rightTotal){
return i;
}
rightTotal = rightTotal - arr[i];
}
return -1;
}
}
5、求x的平方根(二分法、牛顿迭代)
public class Sqrt {
public static void main(String[] args) {
System.out.println(binarySearch(24));
System.out.println(newton(25));
}
public static int binarySearch(int x) {
int index = -1;
int left = 0, right = x;
while (left <= right) {
int mid = (left + right) / 2;
if (mid * mid <= x) {
index = mid;
left = mid + 1;
} else {
right = mid - 1;
}
}
return index;
}
public static int newton(int x) {
if (x == 0) {
return 0;
}
return (int) sqrt(x, x);
}
public static double sqrt(double n, int x) {
double res = (n + x / n) / 2;
if (res == n) {
return n;
} else {
return sqrt(res, x);
}
}
}