ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode练习题

2022-03-20 18:32:22  阅读:135  来源: 互联网

标签:练习题 arr return int public static ListNode LeetCode


文章目录

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以内的素数个数(暴力法、埃筛法)

/**
 * 统计n以内的素数个数
 * 素数:只能被1和自身整除的自然数,0、1除外
 * 例:输入:100
 * 输出:25
 * 重点考察:埃拉托色尼筛选法
 */
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;
    }

    /**
     * 判断是否为素数
     *
     * @param i 需判断的数
     * @return 若是素数返回true,反之返回false
     */
    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、删除排序数组中的重复项(双指针法)

/**
 * 删除排序数组中的重复项
 * 一个有序数组nums ,原地删除重复出现的元系,便每个元素只出现一次,返回删除后数组的新长度
 * 不能使用额外的数组空间,必须在原地修改输入数组并在使用O(1)额外空间的条件下完成。
 * 重点考察:双指针法
 */
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、寻找数组的中心下标

/**
 * 寻找数组的中心下标
 * 给定一个整数数组nums,请编写一个能够返回数组“中心下标”的方法。
 * 中心下标是数组的一个下标,其左侧所有元素相加的和等于右侧所有元素相加的和。如果数组不存在中心下标,返回-1。如果数组有多个中心下标,应该返回最靠近左边的那一个。
 * 注意:中心下标可能出现在数组的两端。
 */
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的平方根(二分法、牛顿迭代)

/**
 * x的平方根
 * 在不使用sqrt(x)函数的情况下,得到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);
        }
    }
}

标签:练习题,arr,return,int,public,static,ListNode,LeetCode
来源: https://blog.csdn.net/qq_45974648/article/details/123618127

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有