当前位置: 首页 > 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;}
}

相关文章:

  • 赶紧收藏!2024 年最常见 20道 Kafka面试题(十)
  • 前端树形结构组件的设计与实现:以企查查、天眼查股权结构为例
  • 翘首以盼的抗锯齿
  • 【java 如何将字符串反转?】
  • C++访问越界
  • python 批量ts合并成一个mp4
  • Unity3D 基于YooAssets的资源管理详解
  • Web安全渗透攻防技术
  • 深度解读:Apache Kafka如何超越消息引擎的界限
  • echaerts图例自动滚动并隐藏翻页按钮
  • Android基础-性能优化
  • Iphone自动化指令每隔固定天数打开闹钟关闭闹钟(二)
  • Vue 跨平台性能优化十法
  • HLA高层体系结构1.0.0版本
  • 父组件调用子组件方法(组合式 API版)
  • 「译」Node.js Streams 基础
  • 【108天】Java——《Head First Java》笔记(第1-4章)
  • angular2 简述
  • express.js的介绍及使用
  • flutter的key在widget list的作用以及必要性
  • HTML中设置input等文本框为不可操作
  • IDEA 插件开发入门教程
  • JavaScript对象详解
  • JavaScript类型识别
  • Magento 1.x 中文订单打印乱码
  • open-falcon 开发笔记(一):从零开始搭建虚拟服务器和监测环境
  • oschina
  • Python语法速览与机器学习开发环境搭建
  • Sublime Text 2/3 绑定Eclipse快捷键
  • TCP拥塞控制
  • 面试遇到的一些题
  • 悄悄地说一个bug
  • 首页查询功能的一次实现过程
  • 思考 CSS 架构
  • 微信支付JSAPI,实测!终极方案
  • 小而合理的前端理论:rscss和rsjs
  • 怎么将电脑中的声音录制成WAV格式
  • MiKTeX could not find the script engine ‘perl.exe‘ which is required to execute ‘latexmk‘.
  • Hibernate主键生成策略及选择
  • kubernetes资源对象--ingress
  • 翻译 | The Principles of OOD 面向对象设计原则
  • # 手柄编程_北通阿修罗3动手评:一款兼具功能、操控性的电竞手柄
  • #图像处理
  • (13)[Xamarin.Android] 不同分辨率下的图片使用概论
  • (17)Hive ——MR任务的map与reduce个数由什么决定?
  • (1综述)从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练
  • (22)C#传智:复习,多态虚方法抽象类接口,静态类,String与StringBuilder,集合泛型List与Dictionary,文件类,结构与类的区别
  • (Qt) 默认QtWidget应用包含什么?
  • (二)linux使用docker容器运行mysql
  • (免费领源码)Java#Springboot#mysql农产品销售管理系统47627-计算机毕业设计项目选题推荐
  • (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境
  • (原创)Stanford Machine Learning (by Andrew NG) --- (week 9) Anomaly DetectionRecommender Systems...
  • (原創) 人會胖會瘦,都是自我要求的結果 (日記)
  • (转)微软牛津计划介绍——屌爆了的自然数据处理解决方案(人脸/语音识别,计算机视觉与语言理解)...
  • (转载)hibernate缓存