Middle-题目32:240. Search a 2D Matrix II

本文介绍了一种高效的矩阵搜索算法,该算法能在满足特定条件的矩阵中查找目标值。矩阵的每行每列都按升序排列。算法从矩阵的右上角开始搜索,逐步缩小搜索范围,直至找到目标值或确定不存在。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目原文:
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
For example,
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
题目大意:
在一个满足下列性质的矩阵中寻找一个数:
每一行从左到右,每一列从上到下的数组是递增顺序的。
题目分析:
初始化i=0,j=n-1,即从矩阵的右上角开始找。若当前数比target小,则i++(因为只有下面才有更大的数,这样直接排除掉第i行),若当前数比target大,则j–(因为只有左面才有更小的数,这样直接排除掉第j列),如果找到边缘还没找到,则返回false。
源码:(language:java)

public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        if(matrix.length==0)
            return false;
        int rows=matrix.length,cols=matrix[0].length;
        int i=0,j=cols-1;
        while(j>=0 && i<=rows-1) {
            if(matrix[i][j] == target)
                return true;
            else if(matrix[i][j] < target)
                i++;
            else
                j--;
        }
        return false;
    }
}

成绩:
13ms,beats 56.57%,众数13ms,37.38%
Cmershen的碎碎念:
这道题很经典,传说是MSRA的面试题,因为每次都排除一行(列),而(i,j)点始终位于新矩阵的右上角,故可以看做是一个分治算法,算法很巧妙值得借鉴。

This is the style section, please modify and give me the full code <style> /* Custom Styles for Cart Page */ .cart-page-section { background: white; padding: 30px; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1); margin-bottom: 30px; } .cart-page-section table.cart { width: 100%; border-collapse: collapse; margin-bottom: 20px; } .cart-page-section table.cart th, .cart-page-section table.cart td { padding: 15px; text-align: left; border-bottom: 1px solid #eee; } .cart-page-section table.cart th { background-color: #f8f8f8; font-weight: bold; } /* 调整列宽 - 增加全选列宽度 */ .cart-page-section table.cart th.product-select, .cart-page-section table.cart td.product-select { width: 8%; /* 增加宽度 */ text-align: center; vertical-align: middle; white-space: nowrap; /* 防止文字换行 */ } .cart-page-section table.cart td.product-info { width: 37%; /* 微调其他列宽度 */ vertical-align: top; } .cart-page-section table.cart .product-info .product-image { float: left; margin-right: 15px; width: 100px; height: 100px; } .cart-page-section table.cart .product-info .product-image img { max-width: 100%; height: auto; display: block; } .cart-page-section table.cart .product-info .product-name { overflow: hidden; } /* 调整剩余列宽度 */ .cart-page-section table.cart td.product-price, .cart-page-section table.cart td.product-quantity, .cart-page-section table.cart td.product-subtotal { width: 15%; /* 三列平均分配剩余宽度 */ vertical-align: middle; } .cart-page-section table.cart td.product-remove { width: 5%; text-align: center; vertical-align: middle; } .cart-footer-actions { position: sticky; bottom: 0; background: #fff; padding: 15px; border-top: 1px solid #ddd; box-shadow: 0 -2px 10px rgba(0,0,0,0.1); z-index: 1000; } .cart-footer-actions .button { padding: 10px 20px; margin-left: 10px; background-color: #f44336; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; transition: background-color 0.3s; } .cart-footer-actions .button:hover { background-color: #d32f2f; } #partial-checkout { padding: 12px 24px; background-color: #2196F3; color: white; text-decoration: none; border-radius: 4px; display: inline-block; margin-top: 10px; transition: background-color 0.3s; } #partial-checkout:hover { background-color: #0b7dda; } .selected-summary { font-size: 16px; font-weight: bold; } .selected-summary p { margin: 0 0 10px 0; } /* Responsive styles */ @media (max-width: 768px) { .cart-page-section table.cart thead { display: none; } .cart-page-section table.cart td { display: block; width: 100% !important; text-align: right; padding: 10px; position: relative; padding-left: 50%; } .cart-page-section table.cart td::before { content: attr(data-title); position: absolute; left: 15px; font-weight: bold; text-align: left; } .cart-page-section table.cart .product-info .product-image { float: none; margin: 0 auto 10px; } .cart-footer-actions { position: sticky; bottom: 0; } .cart-footer-actions > div { flex-direction: column; align-items: flex-start; } .cart-footer-actions .selected-summary { margin-top: 20px; text-align: left; width: 100%; } /* 移动端调整全选列 */ .cart-page-section table.cart td.product-select::before { content: "选择"; } } </style>
最新发布
08-02
<think>根据之前的要求,我们需要调整购物车页脚(sticky footer)的布局,从三行改为两行,并将结算按钮放在与“已选商品”信息同一行。同时,我们还需要调整相应的CSS样式。 修改思路: 1. 将原来的三行布局改为两行布局: - 第一行:包含操作按钮(全选、删除选中商品、清空购物车)和优惠券输入框及按钮。 - 第二行:包含已选商品信息(件数和总价)和结算按钮。 2. 调整CSS样式,确保布局合理,特别是在移动端下的显示。 下面是修改后的完整样式代码,包括对原有样式的调整和新增的样式: ```html <style> /* Custom Styles for Cart Page */ .cart-page-section { background: white; padding: 30px; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1); margin-bottom: 30px; } .cart-page-section table.cart { width: 100%; border-collapse: collapse; margin-bottom: 20px; } .cart-page-section table.cart th, .cart-page-section table.cart td { padding: 15px; text-align: left; border-bottom: 1px solid #eee; } .cart-page-section table.cart th { background-color: #f8f8f8; font-weight: bold; } /* 调整列宽 - 增加全选列宽度 */ .cart-page-section table.cart th.product-select, .cart-page-section table.cart td.product-select { width: 8%; /* 增加宽度 */ text-align: center; vertical-align: middle; white-space: nowrap; /* 防止文字换行 */ } .cart-page-section table.cart td.product-info { width: 37%; /* 微调其他列宽度 */ vertical-align: top; } .cart-page-section table.cart .product-info .product-image { float: left; margin-right: 15px; width: 100px; height: 100px; } .cart-page-section table.cart .product-info .product-image img { max-width: 100%; height: auto; display: block; } .cart-page-section table.cart .product-info .product-name { overflow: hidden; } /* 调整剩余列宽度 */ .cart-page-section table.cart td.product-price, .cart-page-section table.cart td.product-quantity, .cart-page-section table.cart td.product-subtotal { width: 15%; /* 三列平均分配剩余宽度 */ vertical-align: middle; } .cart-page-section table.cart td.product-remove { width: 5%; text-align: center; vertical-align: middle; } /* 粘性页脚样式 */ .cart-footer-actions { position: sticky; bottom: 0; background: #fff; padding: 15px; border-top: 1px solid #ddd; box-shadow: 0 -2px 10px rgba(0,0,0,0.1); z-index: 1000; } /* 操作按钮样式 */ .cart-footer-actions .button { padding: 10px 20px; margin-left: 10px; background-color: #f44336; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; transition: background-color 0.3s; } .cart-footer-actions .button:hover { background-color: #d32f2f; } /* 结算按钮样式 */ #partial-checkout { padding: 12px 24px; background-color: #2196F3; color: white; text-decoration: none; border-radius: 4px; display: inline-block; transition: background-color 0.3s; white-space: nowrap; /* 防止按钮文字换行 */ } #partial-checkout:hover { background-color: #0b7dda; } /* 已选商品信息样式 */ .selected-summary { font-size: 16px; font-weight: bold; display: flex; align-items: center; gap: 10px; } /* 页脚布局调整 */ .cart-footer-actions .footer-row { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 10px; } .cart-footer-actions .footer-left { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; } .cart-footer-actions .footer-right { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; } .cart-footer-actions .coupon-section { display: flex; align-items: center; gap: 5px; } /* 第二行布局 */ .cart-footer-actions .summary-row { display: flex; justify-content: space-between; align-items: center; margin-top: 10px; flex-wrap: wrap; gap: 10px; } /* 响应式样式 */ @media (max-width: 768px) { .cart-page-section table.cart thead { display: none; } .cart-page-section table.cart td { display: block; width: 100% !important; text-align: right; padding: 10px; position: relative; padding-left: 50%; } .cart-page-section table.cart td::before { content: attr(data-title); position: absolute; left: 15px; font-weight: bold; text-align: left; } .cart-page-section table.cart .product-info .product-image { float: none; margin: 0 auto 10px; } /* 移动端调整全选列 */ .cart-page-section table.cart td.product-select::before { content: "选择"; } /* 页脚响应式调整 */ .cart-footer-actions .footer-row, .cart-footer-actions .summary-row { flex-direction: column; align-items: flex-start; gap: 15px; } .cart-footer-actions .footer-left, .cart-footer-actions .footer-right { width: 100%; justify-content: space-between; } .cart-footer-actions .coupon-section { width: 100%; margin-top: 10px; } .cart-footer-actions .coupon-section input { flex: 1; } .cart-footer-actions .summary-row .selected-summary { width: 100%; } #partial-checkout { width: 100%; text-align: center; padding: 12px; } } </style> ``` ### 修改点说明: 1. **布局结构调整**: - 将原来的三行布局改为两行布局,使用两个`div`分别代表两行: - 第一行(`footer-row`):包含左侧的操作按钮(全选、删除、清空)和右侧的优惠券输入框及按钮。 - 第二行(`summary-row`):包含左侧的已选商品信息和右侧的结算按钮。 2. **样式调整**: - 使用Flexbox布局,确保元素在行内对齐,并且有适当的间距(使用`gap`属性)。 - 为操作按钮和优惠券输入框添加了包裹容器(`footer-left`和`footer-right`),以便在移动端下可以自动换行或调整布局。 - 结算按钮现在与已选商品信息在同一行,并且使用`justify-content: space-between`确保它们分布在两端。 3. **响应式设计**: - 在移动端(`max-width: 768px`)下,将两行布局改为垂直堆叠,并且让操作按钮和优惠券输入框占据全宽。 - 结算按钮在移动端下占据全宽,方便点击。 4. **其他优化**: - 为结算按钮添加了`white-space: nowrap`,防止按钮文字换行。 - 调整了已选商品信息的样式,使用`display: flex`和`align-items: center`使其与按钮在同一行时垂直居中。 注意:以上样式代码需要替换原有的`<style>`标签内的内容。同时,请确保HTML结构也按照两行布局进行了调整(如之前提供的HTML结构)。 §§相关问题§§: 1. 如何进一步优化购物车页面的性能? 2. 如何实现购物车商品的拖动排序功能? 3. 如何添加购物车商品图片的懒加载? 4. 如何实现购物车侧边栏弹出效果?</think>根据您的要求,我对购物车页面的样式进行了全面优化,特别是针对粘性页脚布局进行了调整,使其符合两行布局的要求。以下是完整的优化后样式代码: ```html <style> /* Custom Styles for Cart Page */ .cart-page-section { background: white; padding: 30px; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1); margin-bottom: 30px; } .cart-page-section table.cart { width: 100%; border-collapse: collapse; margin-bottom: 20px; } .cart-page-section table.cart th, .cart-page-section table.cart td { padding: 15px; text-align: left; border-bottom: 1px solid #eee; } .cart-page-section table.cart th { background-color: #f8f8f8; font-weight: bold; } /* 调整列宽 - 增加全选列宽度 */ .cart-page-section table.cart th.product-select, .cart-page-section table.cart td.product-select { width: 8%; /* 增加宽度 */ text-align: center; vertical-align: middle; white-space: nowrap; /* 防止文字换行 */ } .cart-page-section table.cart td.product-info { width: 37%; /* 微调其他列宽度 */ vertical-align: top; } .cart-page-section table.cart .product-info .product-image { float: left; margin-right: 15px; width: 100px; height: 100px; } .cart-page-section table.cart .product-info .product-image img { max-width: 100%; height: auto; display: block; } .cart-page-section table.cart .product-info .product-name { overflow: hidden; } /* 调整剩余列宽度 */ .cart-page-section table.cart td.product-price, .cart-page-section table.cart td.product-quantity, .cart-page-section table.cart td.product-subtotal { width: 15%; /* 三列平均分配剩余宽度 */ vertical-align: middle; } .cart-page-section table.cart td.product-remove { width: 5%; text-align: center; vertical-align: middle; } /* 粘性页脚样式 - 更新为两行布局 */ .cart-footer-actions { position: sticky; bottom: 0; background: #fff; padding: 15px; border-top: 1px solid #ddd; box-shadow: 0 -2px 10px rgba(0,0,0,0.1); z-index: 1000; display: flex; flex-direction: column; gap: 15px; } /* 第一行:操作按钮和优惠券 */ .cart-footer-actions .footer-top-row { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 15px; } .footer-left { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; } .coupon-section { display: flex; align-items: center; gap: 5px; } /* 第二行:已选商品信息和结算按钮 */ .cart-footer-actions .footer-bottom-row { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 15px; } .selected-summary { font-size: 16px; font-weight: bold; flex: 1; } /* 操作按钮样式 */ .cart-footer-actions .button { padding: 10px 20px; background-color: #f44336; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; transition: background-color 0.3s; white-space: nowrap; } .cart-footer-actions .button:hover { background-color: #d32f2f; } /* 结算按钮样式 */ #partial-checkout { padding: 12px 24px; background-color: #2196F3; color: white; text-decoration: none; border-radius: 4px; display: inline-block; transition: background-color 0.3s; white-space: nowrap; text-align: center; font-weight: bold; } #partial-checkout:hover { background-color: #0b7dda; } /* 优惠券输入框样式 */ #coupon_code { padding: 8px; width: 200px; border: 1px solid #ddd; border-radius: 4px; transition: border-color 0.3s; } #coupon_code:focus { border-color: #2196F3; outline: none; } /* 响应式样式 */ @media (max-width: 768px) { .cart-page-section table.cart thead { display: none; } .cart-page-section table.cart td { display: block; width: 100% !important; text-align: right; padding: 10px; position: relative; padding-left: 50%; } .cart-page-section table.cart td::before { content: attr(data-title); position: absolute; left: 15px; font-weight: bold; text-align: left; } .cart-page-section table.cart .product-info .product-image { float: none; margin: 0 auto 10px; } /* 移动端调整全选列 */ .cart-page-section table.cart td.product-select::before { content: "选择"; } /* 页脚响应式调整 */ .cart-footer-actions .footer-top-row { flex-direction: column; align-items: flex-start; } .footer-left { width: 100%; justify-content: space-between; } .coupon-section { width: 100%; margin-top: 10px; } .coupon-section input { flex: 1; } .cart-footer-actions .footer-bottom-row { flex-direction: column; align-items: stretch; } .selected-summary { text-align: center; margin-bottom: 10px; } #partial-checkout { width: 100%; padding: 15px; } .cart-footer-actions .button { padding: 12px 15px; margin: 5px 0; width: 100%; text-align: center; } } /* 小屏幕设备优化 */ @media (max-width: 480px) { .cart-page-section { padding: 15px; } .cart-page-section table.cart td { padding-left: 45%; } .cart-page-section table.cart td::before { font-size: 14px; } .cart-footer-actions { padding: 10px; } #coupon_code { width: 100%; } } </style> ``` ### 主要优化点: 1. **两行布局实现**: - 将页脚分为两个主要部分:`.footer-top-row`(操作按钮和优惠券)和`.footer-bottom-row`(商品信息和结算按钮) - 使用Flexbox布局确保元素正确对齐和间距控制 2. **结算按钮位置调整**: - 结算按钮现在位于`.footer-bottom-row`中,与"已选商品"信息在同一行 - 使用`justify-content: space-between`确保它们分居两侧 3. **响应式优化**: - 在小屏幕设备上,元素会垂直堆叠 - 操作按钮和输入框会占据全宽 - 结算按钮在移动端显示为醒目的全宽按钮 4. **视觉增强**: - 为按钮添加悬停效果 - 优化输入框的焦点状态 - 增加元素间距和视觉层次感 - 结算按钮使用更醒目的蓝色和加粗字体 5. **代码结构优化**: - 使用更有意义的类名(如`.footer-top-row`, `.footer-bottom-row`) - 添加注释说明各部分功能 - 优化媒体查询顺序和覆盖范围 这个样式方案完全符合您的要求,将结算按钮与商品信息放在同一行,同时保持了所有功能不变,并增强了移动端的用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值