摘要: 写在前面的话:之前没好好学动态规划,现在开始再重新学一遍吧! 1 买卖股票的最佳时机II // 简易的做法 public int maxProfit(int[] prices) { // 这个题目隐含了可以,同一个股票卖了又买,所以 // 1,2,3,4,5。5 - 1 = (2 - 1) + (3 阅读全文
posted @ 2020-11-26 09:39 Bears9 阅读(98) 评论(2) 推荐(0) 编辑
摘要: 题目链接 检讨代码TAT,模拟思路 Stack<Integer> s1 = new Stack<>(); Stack<Integer> s2 = new Stack<>(); public CQueue() { s1 = new Stack<>(); s2 = new Stack<>(); } // 阅读全文
posted @ 2020-11-22 09:25 Bears9 阅读(77) 评论(0) 推荐(0) 编辑
摘要: 题目链接 代码 第一种写法,多使用了一个辅助指针 public ListNode deleteNode(ListNode head, int val) { if (head == null) return null; // 首先分析在首部的情况 if (head.val == val) { retu 阅读全文
posted @ 2020-11-20 09:35 Bears9 阅读(56) 评论(0) 推荐(0) 编辑
摘要: 题目链接 class Solution { // 说一下我自己的理解 // 数组中的数字都是排好序的,如果左指针和右指针相加大于target, // 那么左指针+1,也肯定不满足,这个时候右指针-1,当s比target小的时候, // 左指针+1, public int[] twoSum(int[] 阅读全文
posted @ 2020-10-26 17:19 Bears9 阅读(57) 评论(0) 推荐(0) 编辑
摘要: 题目链接 代码 /** * 摩尔投票 * 用投票抵消的思路来解题,如果当前的数字和下一个数字相等,那么可以抵消的票数 * +1,如果不相等,那么-1 * */ public int majorityElement(int[] nums) { int ans = 0, votes = 0; for ( 阅读全文
posted @ 2020-10-23 08:31 Bears9 阅读(87) 评论(0) 推荐(0) 编辑
摘要: 题目链接 代码 class Solution { public List<Integer> majorityElement(int[] nums) { // 创建返回值 List<Integer> res = new ArrayList<>(); if (nums == null || nums.l 阅读全文
posted @ 2020-10-23 08:15 Bears9 阅读(75) 评论(0) 推荐(0) 编辑
摘要: 题目链接 代码 public int[] maxSlidingWindow(int[] nums, int k) { // 考虑数组中只有一个数 if (nums.length == 0 || k == 0) return new int[0]; // 采用双端队列 Deque<Integer> d 阅读全文
posted @ 2020-10-20 11:22 Bears9 阅读(102) 评论(0) 推荐(0) 编辑
摘要: 题目链接 代码 class Solution { public List<List<Integer>> threeSum(int[] nums) { int n = nums.length; Arrays.sort(nums); List<List<Integer>> ans = new Array 阅读全文
posted @ 2020-10-18 17:30 Bears9 阅读(71) 评论(0) 推荐(0) 编辑
摘要: 题目链接 二维数组中的查找 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 思路: 直接暴力,或者在这个基础之上,横向加一个二分,可以加速不少。 publ 阅读全文
posted @ 2020-10-09 09:29 Bears9 阅读(155) 评论(0) 推荐(0) 编辑
摘要: 题目链接 **思路:**思路挺简单的,用一个临时节点B记录当前节点,另外一个节点A.next 指向 B,同时更更新节点A,A = A.next。这样就形成了反向? 代码 class Solution { public ListNode reverseList(ListNode head) { if 阅读全文
posted @ 2020-10-06 09:23 Bears9 阅读(66) 评论(0) 推荐(0) 编辑