当前位置: 首页 > news >正文

每日5题Day17 - LeetCode 81 - 85

每一步向前都是向自己的梦想更近一步,坚持不懈,勇往直前!

第一题:81. 搜索旋转排序数组 II - 力扣(LeetCode)

class Solution {public boolean search(int[] nums, int target) {int n = nums.length;if (n == 0) {return false;}if (n == 1) {return nums[0] == target;}//由于是降序的,所以我们采用二分查找的思路int l = 0, r = n - 1;while (l <= r) {int mid = (l + r) / 2;if (nums[mid] == target) {return true;}//注意把重复的去掉if (nums[l] == nums[mid] && nums[mid] == nums[r]) {++l;--r;} else if (nums[l] <= nums[mid]) {if (nums[l] <= target && target < nums[mid]) {r = mid - 1;} else {l = mid + 1;}} else {if (nums[mid] < target && target <= nums[n - 1]) {l = mid + 1;} else {r = mid - 1;}}}return false;}
}

第二题:82. 删除排序链表中的重复元素 II - 力扣(LeetCode)

class Solution {public ListNode deleteDuplicates(ListNode head) {if (head == null || head.next == null) {return head;}ListNode dummy = new ListNode(-1, head);//注意使用三个标记位ListNode pre = dummy, cur = head, nex = head.next;while (cur != null && nex != null) {//如果不同,则向后移位一位if (cur.val != nex.val) {pre = pre.next;cur = cur.next;nex = nex.next;} else {//否则找到下一个不一样的while (nex != null && nex.val == cur.val) {nex = nex.next;}//把不同的连接起来pre.next = nex;//向后移动cur = nex;if (nex != null) {nex = nex.next;}}}return dummy.next;}
}

第三题:83. 删除排序链表中的重复元素 - 力扣(LeetCode)

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode deleteDuplicates(ListNode head) {//很简单,就留一次if(head == null){return head;}ListNode dummyhead = new ListNode(-1, head);ListNode cur = head;while(cur.next != null){if(cur.val == cur.next.val){cur.next = cur.next.next;}else{cur = cur.next;}}return dummyhead.next;}
}

第四题:84. 柱状图中最大的矩形 - 力扣(LeetCode)

class Solution {public int largestRectangleArea(int[] heights) {int MAX = 0;//我们使用单调站,找出两边最低的位置int [] left = new int[heights.length], right = new int[heights.length];for (int i = 0; i < left.length; ++i) left[i] = -1; for (int i = 0; i < right.length; ++i) right[i] = heights.length;     LinkedList<Integer> list = new LinkedList<>();for (int i = 0; i < heights.length; ++i) {while (!list.isEmpty() && heights[i] < heights[list.peekLast()]) {right[list.pollLast()] = i;}list.offerLast(i);}list.clear();for (int i = heights.length - 1; i >= 0; --i) {while (!list.isEmpty() && heights[i] < heights[list.peekLast()]) {left[list.pollLast()] = i;}list.offerLast(i);}for (int i = 0; i < heights.length; ++i) {//对于每一个,我们均计算最大值MAX = Math.max(MAX, (right[i] - left[i] - 1) * heights[i]);}return MAX;}
}

 第五题:85. 最大矩形 - 力扣(LeetCode)

class Solution {public int maximalRectangle(char[][] matrix) {int m = matrix.length;if (m == 0) {return 0;}int n = matrix[0].length;int[][] left = new int[m][n];//我们找到以当前位置为右边界,当前行最长的1的连续长度for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {if (matrix[i][j] == '1') {left[i][j] = (j == 0 ? 0 : left[i][j - 1]) + 1;}}}int ret = 0;for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {if (matrix[i][j] == '0') {continue;}int width = left[i][j];int area = width;for (int k = i - 1; k >= 0; k--) {width = Math.min(width, left[k][j]);area = Math.max(area, (i - k + 1) * width);}ret = Math.max(ret, area);}}return ret;}
}

http://www.lryc.cn/news/365301.html

相关文章:

  • 后端开发面经系列 --中望C++面经
  • 德国西门子论未来质量管理 - 如何与明天相遇?
  • webpack快速入门---webpack的安装和基本使用
  • 后端开发面经系列 -- 华为C++一面面经
  • csrf漏洞与ssrf漏洞
  • AWS EC2服务器开启root密码,SSH登录
  • 常见代码版本管理工具
  • 最新版点微同城源码34.7+全套插件+小程序前后端
  • 逻辑回归及python实现
  • 大模型押题高考语文作文,带着大模型参加语文高考会怎么样?
  • Linux Ext2/3/4文件系统
  • SIMBA方法解读
  • VueRoute url参数
  • WPS表格插件方方格子【凑数】功能:选出和等于固定数字的数
  • 通过SpringCloudGateway中的GlobalFilter实现鉴权过滤
  • TCP/IP(网络编程)
  • 网工内推 | 网络运维工程师,H3CIE认证优先,13薪,享股票期权
  • QT C++ 基于word模板 在书签位置写入文字和图片
  • 根据word模板生成word内容(JAVA)
  • vscode运行命令报错:标记“”不是此版本中的有效语句分隔符。
  • 搜索与图论:树的重心
  • 程序代写,代码编写
  • PbootCms微信小程序官网模版/企业官网/社交电商官网/网络工作室/软件公司官网
  • 【机器学习】GLM4-9B-Chat大模型/GLM-4V-9B多模态大模型概述、原理及推理实战
  • Kotlin 函数式接口
  • 【数据结构】平衡二叉树(AVL树)
  • python数据文件处理库-pandas
  • stm32 h5 串口采用DMA循环BUFF接收数据
  • 海外媒体通稿:9个极具创意的旅游业媒体推广案例分享-华媒舍
  • 接口自动化框架封装思想建立(全)