《1.16-1.19 Leetcode》

1. 最小栈

题目描述:
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。
push(x) – 将元素 x 推入栈中。
pop() – 删除栈顶的元素。
top() – 获取栈顶元素。
getMin() – 检索栈中的最小元素。

代码:

package leetcode.week5;

import java.util.Stack;

/**
 * @author chengzhengda
 * @version 1.0
 * @date 2020-01-17 10:36
 * @desc 最小栈
 */
public class t155 {
    private Stack<Integer> stack1;
    private Stack<Integer> stack2;

    public t155() {
        this.stack1 = new Stack<>();
        this.stack2 = new Stack<>();
    }

    public void push(int x) {
        stack1.push(x);
        // 如果栈不为空,且当前元素小于等于栈顶元素,则入栈
        if (!stack2.isEmpty()) {
            if (x <= stack2.peek()) {
                stack2.push(x);
            }
        } else {
            stack2.push(x);
        }
    }

    public void pop() {
        int num = stack1.pop();
        if (!stack2.isEmpty() && num <= stack2.peek()) {
            stack2.pop();
        }
    }

    public int top() {
        if (stack1.isEmpty()) {
            throw new TypeNotPresentException("当前栈为空", new Throwable());
        }
        return stack1.peek();
    }

    public int getMin() {
        if (stack2.isEmpty()) {
            throw new TypeNotPresentException("当前栈为空", new Throwable());
        }
        return stack2.peek();
    }

    public static void main(String[] args) {
        t155 stack = new t155();
        stack.push(1);
        stack.push(3);
        stack.push(5);
        stack.push(0);
        stack.push(-1);
        stack.pop();
        // 当前栈最小元素
        System.out.println("当前栈最小元素");
        System.out.println(stack.getMin());
        // 辅助栈1全部元素
        System.out.println("辅助栈1全部元素");
        while (!stack.stack1.isEmpty()) {
            System.out.println(stack.stack1.pop());
        }
        // 辅助栈2全部元素
        System.out.println("辅助栈2全部元素");
        while (!stack.stack2.isEmpty()) {
            System.out.println(stack.stack2.pop());
        }
    }
}

2. 相交链表

题目描述:
编写一个程序,找到两个单链表相交的起始节点。
如下面的两个链表:
在这里插入图片描述
在节点 c1 开始相交。

代码:

package leetcode.week5;

/**
 * @author chengzhengda
 * @version 1.0
 * @date 2020-01-17 11:24
 * @desc 相交链表
 */
public class t160 {
    public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) {
            return null;
        }
        ListNode tempA = headA;
        ListNode tempB = headB;
        int i = 0;
        while (i < 3) {
            // 不管A和B的长度如何,A+B一定等于B+A
            if (tempA == null) {
                i++;
                tempA = headB;
            }
            if (tempB == null) {
                i++;
                tempB = headA;
            }

            if (tempA == tempB) {
                return tempA;
            }
            tempA = tempA.next;
            tempB = tempB.next;
        }
        return null;
    }

    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);
        ListNode listNode6 = new ListNode(6);
        ListNode listNode7 = new ListNode(7);
        ListNode listNode8 = new ListNode(8);

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

        listNode6.next = listNode7;
        listNode7.next = listNode8;
        //listNode8.next = listNode3;

        System.out.println(getIntersectionNode(listNode1, listNode6).val);
    }
}

3. 两数之和II-输入有序数组

题目描述:
给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。
函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2。
说明:
返回的下标值(index1 和 index2)不是从零开始的。
你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。

代码:

package leetcode.week5;

import java.lang.Math;

/**
 * @author chengzhengda
 * @version 1.0
 * @date 2020-01-19 13:34
 * @desc 两数之和II-输入有序数组
 */
public class t167 {
    public static int[] twoSum(int[] numbers, int target) {
        int[] res = new int[2];
        int right = getMinIndex(numbers, target, 0, numbers.length - 1);
        int left = 0;
        // 双指针夹逼算法
        while (left < right) {
            int sum = numbers[left] + numbers[right];
            if (sum > target) {
                right--;
            } else if (sum < target) {
                left++;
            } else {
                res[0] = left + 1;
                res[1] = right + 1;
                break;
            }
        }
        return res;
    }

    /**
     * 二分法找出最小的右指针
     */
    public static int getMinIndex(int[] numbers, int target, int left, int right) {
        if (Math.abs(right - left) < 2) {
            return right;
        }
        int mid = (left + right) / 2;
        if (numbers[mid] >= target) {
            return getMinIndex(numbers, target, left, mid);
        }
        return getMinIndex(numbers, target, mid + 1, right);
    }

    public static void main(String[] args) {
        int[] nums = {-1, 0};
        int[] res = twoSum(nums, -1);
        for (int i : res) {
            System.out.print(i + " ");
        }
    }
}

4. 多数元素

题目描述:
给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。
你可以假设数组是非空的,并且给定的数组总是存在多数元素。
示例 1:
输入: [3,2,3]
输出: 3

代码:

package leetcode.week5;

/**
 * @author chengzhengda
 * @version 1.0
 * @date 2020-01-19 15:03
 * @desc 多数元素
 */
public class t169 {
    // 投票法
    public static int majorityElement(int[] nums) {
        int cadidate = nums[0];
        int count = 1;
        for (int i = 1; i <= nums.length - 1; i++) {
            if (nums[i] == cadidate) {
                count++;
            } else {
                count--;
            }
            if (count == 0) {
                cadidate = nums[i];
                count = 1;
            }
        }
        return cadidate;
    }

    public static void main(String[] args) {
        int[] nums = {1, 3, 4, 5, 1, 1, 1, 1};
        System.out.println(majorityElement(nums));
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值