算法习题收集

反转链表

package com.leetcode;

/**
 * 链表反转
 * 将单链表的链表顺序反转过来
 * @author fei
 * @data 2021/10/24 13:01
 */
public class ReverseList {
    static class ListNode {
        int val;
        ListNode next;

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public static ListNode iterate(ListNode head){
        ListNode pre= null,curr,next;
        curr=head;
        while(curr!=null){
            next=curr.next;
            curr.next=pre;
            pre=curr;
            curr=next;
        }
        return pre;
    }

    public static ListNode reverseList(ListNode head){
        if(head==null || head.next==null){
            return  head;
        }
        ListNode newHead = reverseList(head.next);
        head.next.next=head;
        head.next=null;
        return newHead;
    }


    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);
        ListNode pre = iterate(node1);
        System.out.println(pre);
    }
}

寻找数组的中心下标

package com.leetcode;

import java.util.Arrays;

/**
 * 寻找数组的中心下标:
 * 给定一个整数数组nums,返回数组中心下标(下标左侧元素想加的和等于右边元素的和)
 * 如果数组不存在中心下标返回-1;有多个中心下标返回最靠近左边的哪一个
 * @author fei
 * @data 2021/10/24 13:54
 */
public class ArrayCenterIndex {
    public static void main(String[] args) {
        System.out.println(privotIndex(new int[]{1,7,3,6,5,6}));
    }
    public static int privotIndex(int[] nums){
        int sum= Arrays.stream(nums).sum();
        int total=0;
        for(int i=0;i<nums.length;i++){
            total +=nums[i];
            if(total==sum){
                return i;
            }
            sum=sum-nums[i];
        }
        return -1;
    }
}

统计素数个数

package com.leetcode;

/**
 * 素数个数统计
 * 统计n以内的素数个数(只能被1和自身整除的自然数,0,1,除外)
 * @author fei
 * @data 2021/10/24 13:33
 */
public class getPrimeNumber {
    public static void main(String[] args) {
        System.out.println(bf(100));
        System.out.println(eratosthenes(100));
    }
    //暴力BF
    public static int bf(int n){
        int count =0;
        for(int i=2;i<n;i++){
            count += isPrime(i)? 1:0;
        }
        return count;
    }

    public  static boolean isPrime(int x){
        for(int i=2;i*i<=x;i++){
            if(x%i ==0){
                return false;
            }
        }
        return true;
    }

    //埃筛法
    public static int eratosthenes(int n){
        boolean[] isprime = new boolean[n];//false代表素数
        int count =0;
        for(int i=2;i<n;i++){
            if(!isprime[i]){
                count++;
                for(int j=2*i;j<n;j+=i){//j就是合数的标记位
                    isprime[j]=true;
                }
            }
        }
        return count;
    }

}

删除排序数组中的重复项

package com.leetcode;

/**
 * 删除排序数组中的重复项
 * 一个有序数组nums,原地删除重复出现的元素,使每一个元素只出现一次,返回删除后数组的新长度
 * 不能使用额外的数组空间,必须在原地修改输入数组并在使用O(1)额外空间的条件下完成
 * 考察双指针算法
 * @author fei
 * @data 2021/10/24 13:49
 */
public class SortedArrayDuplicates {
    public static void main(String[] args) {
        System.out.println(removeDuplicates(new int[]{0,1,1,1,2,3,3,4}));
    }

    public  static int removeDuplicates(int[] nums){
    if(nums.length==0){
        return 0;
    }
    int i=0;
    for(int j=1;j<nums.length;j++){
        if(nums[j]!=nums[i]){
            i++;
            nums[i]=nums[i];
        }
    }
        return i;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值