《1.20 Leetcode》

1. 阶乘后的0

题目描述:
给定一个整数 n,返回 n! 结果尾数中零的数量。
示例 1:
输入: 3
输出: 0
解释: 3! = 6, 尾数中没有零。

代码:

package leetcode.week6;

/**
 * @author chengzhengda
 * @version 1.0
 * @date 2020-01-20 15:24
 * @desc 阶乘后的0
 */
public class t172 {

    public static int trailingZeroes(int n) {
        if (n <= 0) {
            return 0;
        }
        int count = 0;
        while (n >= 5) {
            count += n / 5;
            n /= 5;
        }
        return count;
    }

    // 计算阶乘
    public static long getCount(int n) {
        long sum = 1;
        for (long i = n; i > 0; i--) {
            sum *= i;
        }
        return sum;
    }

    public static void main(String[] args) {
        System.out.println(trailingZeroes(20));
        System.out.println(getCount(20));
    }
}

2. 旋转数组

题目描述:
给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。
示例 1:
输入: [1,2,3,4,5,6,7] 和 k = 3
输出: [5,6,7,1,2,3,4]
解释:
向右旋转 1 步: [7,1,2,3,4,5,6]
向右旋转 2 步: [6,7,1,2,3,4,5]
向右旋转 3 步: [5,6,7,1,2,3,4]

代码:

package leetcode.week6;

/**
 * @author chengzhengda
 * @version 1.0
 * @date 2020-01-20 15:44
 * @desc 旋转数组
 */
public class t189 {

    public static void rotate(int[] nums, int k) {

        int index = k % nums.length;
        if (index <= 0) {
            return;
        }
        // 反转整个数组
        reversal(nums, 0, nums.length - 1);
        // 反转数组前面的元素
        reversal(nums, 0, index - 1);
        // 反转数组后面的元素
        reversal(nums, index, nums.length - 1);
    }

    // 反转数组
    public static void reversal(int[] nums, int begin, int end) {
        for (int i = 0; i <= (end - begin) / 2; i++) {
            int temp = nums[begin + i];
            nums[begin + i] = nums[end - i];
            nums[end - i] = temp;
        }
    }

    public static void main(String[] args) {
        int[] arr = {1};
        rotate(arr, 0);
    }
}

3. 反转链表

题目描述:
反转一个单链表。
示例:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL

代码:

package leetcode.week6;

import leetcode.week5.ListNode;


/**
 * @author chengzhengda
 * @version 1.0
 * @date 2020-01-20 16:45
 * @desc 反转链表
 */
public class t206 {
    public static ListNode reverseList(ListNode head) {
        ListNode last = null;
        while (head != null) {
            ListNode next = head.next;
            head.next = last;
            last = head;
            head = next;
        }
        return last;
    }

    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode4 = new ListNode(4);
        ListNode listNode5 = new ListNode(5);

        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;

        ListNode result = reverseList(listNode1);
        while (result != null) {
            System.out.println(result.val);
            result = result.next;
        }
    }
}

4. 用队列实现栈

题目描述:
使用队列实现栈的下列操作:
push(x) – 元素 x 入栈
pop() – 移除栈顶元素
top() – 获取栈顶元素
empty() – 返回栈是否为空

代码:

package leetcode.week6;

import java.util.LinkedList;

/**
 * @author chengzhengda
 * @version 1.0
 * @date 2020-01-20 22:54
 * @desc
 */
public class t225 {
    private LinkedList<Integer> queue;

    /**
     * Initialize your data structure here.
     */
    public t225() {
        queue = new LinkedList<>();
    }

    /**
     * Push element x onto stack.
     */
    public void push(int x) {
        queue.offerFirst(x);
    }

    /**
     * Removes the element on top of the stack and returns that element.
     */
    public int pop() {
        return queue.pollFirst();
    }

    /**
     * Get the top element.
     */
    public int top() {
        return queue.peekFirst();
    }

    /**
     * Returns whether the stack is empty.
     */
    public boolean empty() {
        return queue.isEmpty();
    }

    public static void main(String[] args) {
        t225 stack = new t225();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        System.out.println(stack.top());
        System.out.println(stack.pop());

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值