[Leetcode] Math & Dynamic

本文介绍了解决两个相似LeetCode问题的方法:子数组和等于K与连续子数组求和。通过动态规划技巧,实现了高效的解决方案,并提供了源代码实现。

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

[Leetcode] Math & Dynamic

In this blog, I try to explain how to solve two similar question about Math & Dynamic. The idea in these two question is pretty similar, if you have mastered how to solve one of these two questions, you will feel it easy to solve another.

Subarray Sum Equals K

Subarray Sum Equals K

Analysis

In this question, you have to get the number of continuous subarrays whose sum equals to K. Pay attention to that the description of question doesn’t emphasize the size of the subarray so it will be valid if the size of subarray just be 1. The easy solution is by brutal force. By looping the array in two-layer, you can have all of the subarrays the array generates, then just sum them up to check if they equal to K. But it takes a bit more time. The other solution is dynamic programming. By defining T(n) as the sum of subarray which starts at beginning of the array and ends at the index n, then we get know the sum of array[i : n] is T(n) - T(i). SO, things are easy now. We can just check if T(n) - T(i) equals to K. There is one more trick we need to know. We can store all of the T(i) in a map. The key in map stands for the sum of the subarray, and the value in the map represents the num of such sum. Then the num of subarray which equals to K is map[T(n) - T(i)].

Stary 2017-11-04 at 9.55.54 A

Code

The detail of algorithm can be found in the code. K = 0 is a special situation, you have to consider carefully about how to dealing with the situation when K = 0.

class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        unordered_map<int, int> hash;
        int sum = 0;
        int result = 0;
        int mod = 0;
        for (int i = 0; i < nums.size(); i++) {
            sum += nums[i];
            mod = k == 0 ? sum : sum - k;
            result += hash[mod];
            hash[sum] += 1;
            if (sum == k) {
                result += 1;
            }
        }
        return result;
    }
};

Time complexity: O(n)

Continuous Subarray Sum

Continuous Subarray Sum

Analysis

This question is similar to the above one. You can try to think about that by using the idea which I have described. What you have to know is the size of subarray must be at least 2. There is a trick which you have to store pre in the map instead of storing sum directly.

Stary 2017-11-04 at 10.09.27 A

Code

class Solution {
public:
    bool checkSubarraySum(vector<int>& nums, int k) {
        unordered_set<int> hash;
        int sum = 0, pre = 0;
        for (int i = 0; i < nums.size(); i++) {
            sum += nums[i];
            int mod = k == 0 ? sum : sum % k;
            if (hash.count(mod)) { return true; }
            hash.insert(pre);
            pre = mod;
        }
        return false;
    }
};

Time complexity: O(n)

资源下载链接为: https://pan.quark.cn/s/d3128e15f681 罗技MX Master 2S是一款高端无线鼠标,凭借其卓越的性能和舒适性,深受专业设计师、程序员以及需要长时间使用鼠标的人群的喜爱。它在macOS平台上表现出色,功能丰富。而&ldquo;LogiMgr Installer 8.20.233.zip&rdquo;是该鼠标在macOS系统上对应的软件安装程序,版本号为8.20.233,主要功能如下: 驱动安装:该安装包可确保MX Master 2S在macOS系统中被正确识别和配置,发挥出最佳硬件性能,同时保证良好的兼容性。它会安装必要的驱动程序,从而启用鼠标的高级功能。 自定义设置:借助此软件,用户能够根据自己的工作习惯,对MX Master 2S的各个按钮和滚轮功能进行自定义。比如设置特定快捷键、调整滚动速度和方向等,以满足个性化需求。 Flow功能:罗技Flow是一项创新技术,允许用户在多台设备间无缝切换。只需在软件中完成设备配置,鼠标就能在不同电脑之间进行复制、粘贴操作,从而大幅提升工作效率。 电池管理:软件具备电池状态监控功能,可帮助用户实时了解MX Master 2S的电量情况,并及时提醒用户充电,避免因电量不足而影响工作。 手势控制:MX Master 2S配备独特的侧边滚轮和拇指按钮,用户可通过软件定义这些手势,实现诸如浏览页面、切换应用等操作,进一步提升使用便捷性。 兼容性优化:罗技的软件会定期更新,以适应macOS系统的最新变化,确保软件与操作系统始终保持良好的兼容性,保障鼠标在不同系统版本下都能稳定运行。 设备配对:对于拥有多个罗技设备的用户,该软件能够方便地管理和配对这些设备,实现快速切换,满足多设备使用场景下的需求。 在安装&ldquo;LogiMgr Installer 8.20.233.app&rdquo;时,用户需确保macOS系统满足软件的最低要求,并
资源下载链接为: https://pan.quark.cn/s/27e1210fbf58 《RT-Thread在STM32F103C8T6上的移植实战指南》 RT-Thread是一款开源、轻量级且高可扩展性的实时操作系统(RTOS),广泛应用于物联网、工业控制和消费电子等领域。STM32F103C8T6作为一款基于ARM Cortex-M3内核的微控制器,凭借其丰富的外设资源和高性价比,成为嵌入式系统学习与开发的理想平台。本文将详细介绍如何将RT-Thread移植到STM32F103C8T6,并逐步添加rt_printf支持和Finsh组件,以实现调试与交互功能。 一、移植准备 移植RT-Thread到STM32F103C8T6的第一步是下载RT-Thread Nano的源码,具体操作方法在2.下载RT-Thread Nano源码中详细说明。RT-Thread Nano是RT-Thread的精简版,专为资源受限的嵌入式设备设计。 二、整合源码 将下载的RT-Thread Nano源码复制到STM32的裸机工程中,相关步骤在3.拷贝RT-Thread Nano源码到裸机工程中介绍。需确保源码结构与工程匹配,以便后续编译和配置。 三、调整工程目录结构 为适应STM32开发环境,需对工程目录结构进行调整。5.修改工程目录结构中详细说明了如何组织和调整文件,确保编译器正确识别和处理所有源文件。 四、删除未使用的文件 为优化内存占用,需删除RT-Thread中不必要的文件。4.删除RT-Thread中不必要的文件中列出了可安全移除的文件清单。 五、编译与错误修复 完成目录结构调整和冗余文件删除后,开始编译工程。在6.编译工程并修复错误中,将逐一解决编译过程中的错误和警告,确保代码无误。 六、配置Board.c文件 8.修改board.c文件涉及针对STM32F103C8T6硬件特性的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值