Middle-题目3:238. Product of Array Except Self

本文介绍了一种不使用除法求解数组中除自身以外元素乘积的方法,并提供了两种算法实现,一种为朴素解法,另一种为优化后的O(n)算法。

题目原文:
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
Solve it without division and in O(n).
For example, given [1,2,3,4], return [24,12,8,6].
题目大意:
给一个数组nums,输出一个数组output,其中output[i]等于nums数组中除了nums[i]外所有数的乘积。
要求不使用除法,且复杂度O(n),
题目分析:
1. 朴素解法
既然是oj题,AC就好,你说不用除就不用除了?那么问题来了,如果nums[i]是0怎么办?
这时要分两种情况:
(1) nums数组里面有两个以上的0,那么output数组必然全是0.
(2) nums数组里面只有一个0,那么0这一位对应是其他所有非0数的乘积,其他位都是0.
按这个思路,先统计数组里有几个0,作为两种特殊情况。其他的就都乘起来分别算即可。
2. 真的不用除的算法:
考虑output[i]=(nums[0]nums[1]nums[i-1])(nums[i+1]…*nums[n-1]),那么遍历两次数组,第一轮正向遍历,计算出nums[0]*nums[1]nums[i-1]的积放到output[i]里面,第二轮反向遍历,计算出nums[i+1]…*nums[n-1]的积再乘进去即可。
源码:(language:java)
1. 朴素解法:

public class Solution {
    public int[] productExceptSelf(int[] nums) {
        int product=1,product2=1;
        int zerocount=0;
        for(int num:nums)
        {
            if(num==0)
                zerocount++;
            if(zerocount==2)
                break;
            product*=num;
            product2*=(num==0)?1:num;
        }
        if(zerocount==0){
            for(int i=0;i<nums.length;i++)
                nums[i]=product/nums[i];
            return nums;  
        }
        else if(zerocount==1)
        {
            int[] array=new int[nums.length];
            for(int j=0;j<nums.length;j++)
                array[j]=(nums[j]==0)?product2:0;
            return array;
        }
        else
            return new int[nums.length];      
    }
}
  1. 不用除的巧妙算法:
public class Solution {
    public int[] productExceptSelf(int[] nums) {
        int[] res = new int[nums.length];
        res[res.length-1] = 1;
        for(int i=nums.length-2; i>=0; i--) {
            res[i] = res[i+1] * nums[i+1];
        }

        int left = 1;
        for(int i=0; i<nums.length; i++) {
            res[i] *= left;
            left *= nums[i];
        }
        return res;
    }
}

成绩:
朴素解法:3ms,beats 11.83%,
巧妙解法:2ms,beats 46.07%,众数2ms,53.93%
Cmershen的碎碎念:
Middle前期的很多道题看似简单,但却有很巧妙的算法,应予以注意。

内容概要:本文详细介绍了“秒杀商城”微服务架构的设计与实战全过程,涵盖系统从需求分析、服务拆分、技术选型到核心功能开发、分布式事务处理、容器化部署及监控链路追踪的完整流程。重点解决了高并发场景下的超卖问题,采用Redis预减库存、消息队列削峰、数据库乐观锁等手段保障数据一致性,并通过Nacos实现服务注册发现与配置管理,利用Seata处理跨服务分布式事务,结合RabbitMQ实现异步下单,提升系统吞吐能力。同时,项目支持Docker Compose快速部署和Kubernetes生产级编排,集成Sleuth+Zipkin链路追踪与Prometheus+Grafana监控体系,构建可观测性强的微服务系统。; 适合人群:具备Java基础和Spring Boot开发经验,熟悉微服务基本概念的中高级研发人员,尤其是希望深入理解高并发系统设计、分布式事务、服务治理等核心技术的开发者;适合工作2-5年、有志于转型微服务或提升架构能力的工程师; 使用场景及目标:①学习如何基于Spring Cloud Alibaba构建完整的微服务项目;②掌握秒杀场景下高并发、超卖控制、异步化、削峰填谷等关键技术方案;③实践分布式事务(Seata)、服务熔断降级、链路追踪、统一配置中心等企业级中间件的应用;④完成从本地开发到容器化部署的全流程落地; 阅读建议:建议按照文档提供的七个阶段循序渐进地动手实践,重点关注秒杀流程设计、服务间通信机制、分布式事务实现和系统性能优化部分,结合代码调试与监控工具深入理解各组件协作原理,真正掌握高并发微服务系统的构建能力。
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、付费专栏及课程。

余额充值