LeetCode练习题【java】(1)

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);
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fly-ping

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值