栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

LeetCode练习题【java】(1)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

LeetCode练习题【java】(1)

文章目录

1、单链表的反转(迭代、递归)2、统计n以内的素数个数(暴力法、埃筛法)3、删除排序数组中的重复项(双指针法)4、寻找数组的中心下标5、求x的平方根(二分法、牛顿迭代)

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));
        //System.out.println("递归:"+recursion(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]; //默认false为素数
        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 = Arrays.stream(arr).sum(); //求数组中元素的和
        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);
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/774879.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号