Leetcode 0503. 下一个更大元素 II
503. 下一个更大元素 II给定一个循环数组 nums ( nums[nums.length - 1] 的下一个元素是 nums[0] ),返回 nums 中每个元素的 下一个更大元素 。 数字 x 的 下一个更大的元素 是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它的下一个更大的数。如果不存在,则输出 -1 。 示例 1: 12345输入: nums = [1,2,1]输出: [2,-1,2]解释: 第一个 1 的下一个更大的数是 2;数字 2 找不到下一个更大的数; 第二个 1 的下一个最大的数需要循环搜索,结果也是 2。 示例 2: 12输入: nums = [1,2,3,4,3]输出: [2,3,4,-1,4] 解题思路核心思路是单调栈 + 数组翻倍模拟循环,通过将数组复制一份拼接在末尾(模拟循环),利用单调栈高效找到每个元素的下一个更大元素: 模拟循环数组: 将原数组 nums 复制一份并拼接在末尾(如 [1,2,1] 变为 [1,2,1,1,2,1]),这样无需额外处理循环边界,可直接线性遍历。 单调栈求解: 使用单调栈存储...
Python字符串处理:strip()方法与链式调用
Python的字符串方法是非常强大的工具,其中strip()系列方法是处理用户输入和字符串清洗时最常用的函数之一。本文将详细介绍Python字符串的strip()方法以及其他常用的字符串处理方法。 一、strip()方法详解1. 基本用法strip()方法用于移除字符串首尾两端的空白字符: 1234567891011# 基本用法text = " Hello, World! "print(f"'{text.strip()}'") # 输出:'Hello, World!'# 移除换行符text = "\nHello\n"print(f"'{text.strip()}'") # 输出:'Hello'# 移除制表符text = "\tHello\t"print(f"'{text.strip()}'") ...
Leetcode 0496. 下一个更大元素 I
496. 下一个更大元素 Inums1 中数字 x 的 下一个更大元素 是指 x 在 nums2 中对应位置 右侧 的 第一个 比 x 大的元素。 给你两个 没有重复元素 的数组 nums1 和 nums2 ,下标从 0 开始计数,其中nums1 是 nums2 的子集。 对于每个 0 <= i < nums1.length ,找出满足 nums1[i] == nums2[j] 的下标 j ,并且在 nums2 确定 nums2[j] 的 下一个更大元素 。如果不存在下一个更大元素,那么本次查询的答案是 -1 。 返回一个长度为 nums1.length 的数组 ans 作为答案,满足 ans[i] 是如上所述的 下一个更大元素 。 示例 1: 123456输入:nums1 = [4,1,2], nums2 = [1,3,4,2].输出:[-1,3,-1]解释:nums1 中每个值的下一个更大元素如下所述:- 4 ,用加粗斜体标识,nums2 = [1,3,4,2]。不存在下一个更大元素,所以答案是 -1 。- 1 ,用加粗斜体标识,nums2 = [1,3,4,2...
Leetcode 0003. Longest Substring Without Repeating Characters (python)
3. Longest Substring Without Repeating Characters题目Given a string s, find the length of the longest substring without repeating characters. Example 1: 123Input: s = "abcabcbb"Output: 3Explanation: The answer is "abc", with the length of 3. Example 2: 123Input: s = "bbbbb"Output: 1Explanation: The answer is "b", with the length of 1. Example 3: 1234Input: s = "pwwkew"Output: 3Explanation: The answer is "wke", with the length of 3.No...
Leetcode 0491. Non-decreasing Subsequences
491. Non-decreasing SubsequencesGiven an integer array nums, return all the different possible non-decreasing subsequences of the given array with at least two elements. You may return the answer in any order. Example 1: 12Input: nums = [4,6,7,7]Output: [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]] Example 2: 12Input: nums = [4,4,3,2,1]Output: [[4,4]] 题目大意给定一个整数数组 nums,返回所有不同的、长度至少为 2 的非递减子序列。子序列是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。 例如: 输入 nums = [4,6,7,7],输出包含 [4,6]、[4,6,7...
C++ 中->和*运算符重载的全面解析
引言在 C++ 编程中,->和*运算符最初设计用于指针操作。然而,通过运算符重载机制,我们可以让自定义类型也支持这些操作,从而实现类似指针的行为,同时添加额外功能。 一、为什么需要重载->和*运算符1. 扩展指针功能的核心需求原生指针 (T*) 虽然简洁高效,但存在明显局限: 无法自动管理资源生命周期 缺乏访问控制机制 不支持额外的调试信息 不能实现代理或间接访问模式 通过重载->和*,我们可以创建 "智能指针" 或 "代理对象",在保持指针操作语法的同时,添加所需功能。 2. 关键应用场景资源自动管理智能指针通过运算符重载实现自动内存管理: 1234567891011121314151617181920// 简化的shared_ptr实现思路template<typename T>class SharedPtr {private: T* ptr; int* ref_count; void release() { if (--(*ref_c...
Leetcode 0461.hamming-distance
461. 汉明距离两个整数之间的 汉明距离 指的是这两个数字对应二进制位不同的位置的数目。 给你两个整数 x 和 y,计算并返回它们之间的汉明距离。 示例 1: 1234567输入:x = 1, y = 4输出:2解释:1 (0 0 0 1)4 (0 1 0 0) ↑ ↑上面的箭头指出了对应二进制位不同的位置。 示例 2: 12输入:x = 3, y = 1输出:1 题目大意 汉明距离是指两个整数的二进制表示中,对应位置二进制位不同的数目。给定两个整数 x 和 y,计算并返回它们之间的汉明距离。 例如: 输入 x=1, y=4,二进制分别为 0001 和 0100,对应位不同的位置有 2 处,输出 2; 输入 x=3, y=1,二进制分别为 0011 和 0001,对应位不同的位置有 1 处,输出 1。 解题思路核心思路是利用异或运算 + 统计 1 的个数,步骤如下: 异或运算(XOR):异或的特性是 “相同为 0,不同为 1”。对 x 和 y 做异或操作,得到的结果 xor_result 中,每一位的 1 都对应 x 和 y 二进制位不同的位...
Leetcode 0002. Add Two Numbers
2. Add Two Numbers题目You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example 1: 123Input: l1 = [2,4,3], l2 = [5,6,4]Output: [7,0,8]Explanation: 342 + 465 = 807. Example 2: 12Input: l1 = [0], l2 = [0]Output: [0] Example 3: 12I...
Python格式化字符串:f-string用法详解
Python的f-string是一种强大的字符串格式化方式,它允许在字符串中直接嵌入表达式。本文将详细介绍f-string的用法和特点。 一、f-string的基本用法1. 基本语法12345# f-string基本用法name = "Alice"age = 25print(f"My name is {name}, and I am {age} years old.")# 输出:My name is Alice, and I am 25 years old. 2. 表达式求值123456789101112# f-string中的表达式x = 10y = 20print(f"The sum of {x} and {y} is {x + y}.")# 输出:The sum of 10 and 20 is 30.# 调用函数def get_greeting(): return "Hello"print...
Leetcode 0455. Assign Cookies
455. Assign CookiesAssume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with; and each cookie j has a size s[j]. If s[j] >= g[i], we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number. Example 1: 1...
C++ 中的 Pimpl 模式:封装实现细节的艺术
一、什么是 Pimpl 模式?Pimpl 模式(Pointer to Implementation)通过私有指针隔离类的接口与实现,是 C++ 封装原则的高级应用。其核心目标为:隐藏实现细节、提升编译效率、保障二进制兼容及简化接口。 二、Pimpl 模式的实现机制传统类设计将接口与实现混在头文件,Pimpl 模式则通过以下方式分离: 头文件声明含私有指针的公共接口类 源文件定义包含实现细节的实现类 接口类借指针间接访问实现类成员 传统设计 Pimpl 模式设计 接口与实现一体 接口类仅含指针 头文件暴露所有细节 头文件隐藏实现 实现变更需重编译所有依赖 仅需重编译源文件 三、Pimpl 模式的代码实现3.1 头文件(widget.h)123456789101112131415161718192021222324252627#include <memory>#include <string>// Widget 类是对外暴露的公共接口类class Widget {public: // 构造函数,用于创建 Widget...
Python基础:print输出、input输入与注释语法
Python是一种简单易学的编程语言,其基础语法非常直观。本文将详细介绍Python中的print函数使用、input函数输入以及注释的使用方法。 一、print函数的使用1. 基本用法print()函数用于在控制台输出信息,是Python中最常用的函数之一。 123456789# 输出字符串print("Hello, World!")# 输出数字print(42)# 输出变量name = "Python"print(name) 2. 使用+连接输出使用+运算符可以连接多个字符串或变量进行输出: 12345678910# 连接字符串print("Hello, " + "World!")# 连接字符串和变量name = "Python"print("Hello, " + name + "!")# 注意:+运算符要求两边类型一致# 错误示例:print("The answer is " + 42) # 会报错# 正确示例:...
Leetcode 0001.two sum(python)
1. Two Sum题目Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. Example 1: 123Input: nums = [2,7,11,15], target = 9Output: [0,1]Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. Example 2: 12Input: nums = [3,2,4], target = 6Output: [1,2] Example 3: 12Input: nums =...
C++ 单例模式的四种自动释放方式详解
导言单例模式的自动释放是解决资源泄漏问题的关键,不同场景下可以选择不同的实现方式。下面详细解析四种典型的自动释放机制,包括其实现原理、代码示例及适用场景。 一、方式一:利用栈对象的生命周期进行管理实现原理利用栈对象 "出作用域自动析构" 的特性,将单例指针存储在栈对象中,当栈对象被销毁时,自动释放单例资源。 代码实现12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758#include <iostream>class Singleton {private: // 私有构造函数 Singleton() { std::cout << "Singleton 构造函数被调用" << std::endl; } // 私有析构函数 ~Singleton() { ...
Leetcode 0454. 4Sum II
454. 4Sum IIGiven four integer arrays nums1, nums2, nums3, and nums4 all of length n, return the number of tuples (i, j, k, l) such that: 0 <= i, j, k, l < n nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0 Example 1: 123456Input: nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]Output: 2Explanation:The two tuples are:1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 02. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 +...
Leetcode 0445. Add Two Numbers II
445. Add Two Numbers IIYou are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. 1234567891011121314151617181920212223242526272829303132333435363738394041424344/** * Definition for singly-linked list. * struct ListNode { * int v...
友元机制与其他访问控制机制的区别与联系
导言在 C++ 面向对象编程中,访问控制机制是实现封装性的核心手段。友元机制和继承是两种主要的访问控制方式,它们既有联系又有显著区别。 一、本质区别 特性 友元机制 继承机制 关系性质 单向的 "授权" 关系 父子间的 "派生" 关系 访问目的 临时突破封装边界 实现代码复用与扩展 关系方向 非对称(A 是 B 的友元≠B 是 A 的友元) 可传递(间接继承) 生命周期 编译期静态确定 运行期动态体现(多态) 代码耦合度 低到中等(仅需声明) 高(子类依赖父类实现) 二、访问权限差异1. 友元机制的访问特点 可以直接访问所有私有成员(private)和保护成员(protected) 无需通过类的接口(public 成员)进行访问 访问权限是单向且不可传递的 示例: 12345678910111213141516171819class A {private: int x; friend class B; // B可以直接访问A的私有成员};class B {pub...
Leetcode 0429. N-ary Tree Level Order Traversal
429. N-ary Tree Level Order TraversalGiven an n-ary tree, return the level order traversal of its nodes' values. Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples). Example 1: 12Input: root = [1,null,3,2,4,null,5,6]Output: [[1],[3,2,4],[5,6]] Example 2: 12Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]Output: [[1],[2,3,4,5],[6,7,8,9,10]...
Leetcode 0409. 最长回文串
409. 最长回文串给定一个包含大写字母和小写字母的字符串 s ,返回 通过这些字母构造成的 最长的 回文串 的长度。 在构造过程中,请注意 区分大小写 。比如 "Aa" 不能当做一个回文字符串。 示例 1: 1234输入:s = "abccccdd"输出:7解释:我们可以构造的最长的回文串是"dccaccd", 它的长度是 7。 示例 2: 123输入:s = "a"输出:1解释:可以构造的最长回文串是"a",它的长度是 1。 解题思路核心思路是统计字符频率,利用回文串的特性计算最大长度: 回文串特性: 回文串对称位置的字符相同; 偶数长度的回文串:所有字符出现次数均为偶数; 奇数长度的回文串:仅有一个字符出现奇数次(位于中心),其余均为偶数次。 频率统计: 统计字符串中每个字符的出现次数; 累计所有字符的最大偶数次(例如,出现 5 次的字符可贡献 4 次); 若存在出现奇数次的字符,可在结果中加 1(作为中心字符)。 计算逻辑: 初始化结果 max_len...
C++ 友元机制深度解析:突破封装边界的艺术
一、友元机制概述C++ 面向对象编程中,封装通过public、private和protected确保数据安全。但特定场景需突破封装,友元(friend)机制由此诞生,它允许指定外部函数或类访问当前类私有、保护成员,同时维持其他实体的封装性,核心价值在于受控突破封装、支持高效数据访问及解决权限问题。 二、友元函数详解2.1 友元函数的定义与实现友元函数是类中声明为friend的非成员函数,可访问类所有成员。如Circle类中,calculateArea和isSameSize通过声明为友元,直接访问私有成员计算圆面积和比较大小。 1234567891011121314151617181920#include <iostream>#include <cmath>class Circle {private: double radius; const double PI = 3.1415926;public: Circle(double r) : radius(r) {} friend double calcul...
Leetcode 0404. Sum of Left Leaves
404. Sum of Left LeavesGiven the root of a binary tree, return the sum of all left leaves. A leaf is a node with no children. A left leaf is a leaf that is the left child of another node. Example 1: 123Input: root = [3,9,20,null,null,15,7]Output: 24Explanation: There are two left leaves in the binary tree, with values 9 and 15 respectively. Example 2: 12Input: root = [1]Output: 0 题目大意给定一棵二叉树的根节点 root,返回所有左叶子的节点值之和。左叶子的定义是:既是叶子节点(没有子节点),又是其父节点的左子节点。 例如: 输入二叉树 [3,9,20,null,null,15,7],左叶子为 ...
C++ 文件读取技术详解:get ()、getline () 与流提取运算符
一、核心函数原型与参数解析1.1 get () 函数家族get()函数是 C++ 中最基础的字符读取工具,有多个重载版本: 1234567891011// 读取单个字符(包含空白字符)int get();// 读取字符到指定缓冲区,最多n-1个字符,遇到分隔符停止istream& get(char* s, streamsize n);// 带自定义分隔符的版本istream& get(char* s, streamsize n, char delim);// 读取字符到字符对象istream& get(char& c); 关键特性: 不会跳过空白字符(空格、制表符、换行符等) 读取失败时返回 EOF(-1) 保留分隔符在输入流中(不提取) 1.2 getline () 函数getline()专为读取完整行设计,主要有两种形式: 123456789// 从输入流读取一行到字符数组istream& getline(char* s, streamsize n);// 带自定义分隔符的版本istream& getline(cha...
Leetcode 0402. 移掉 K 位数字
402. 移掉 K 位数字给你一个以字符串表示的非负整数 num 和一个整数 k ,移除这个数中的 k 位数字,使得剩下的数字最小。请你以字符串形式返回这个最小的数字。 示例 1 : 123输入:num = "1432219", k = 3输出:"1219"解释:移除掉三个数字 4, 3, 和 2 形成一个新的最小的数字 1219 。 示例 2 : 123输入:num = "10200", k = 1输出:"200"解释:移掉首位的 1 剩下的数字为 200. 注意输出不能有任何前导零。 示例 3 : 123输入:num = "10", k = 2输出:"0"解释:从原数字移除所有的数字,剩余为空就是 0 。 解题思路核心思路是单调栈 + 贪心算法,通过维护一个递增的栈来确保剩余数字最小,同时控制移除的数字数量不超过 k: 单调栈逻辑: 遍历字符串中的每个数字,对于当前数字c: 若栈不为空,且栈顶数字大于 c,且仍有可移除的次数(k > 0),则弹...
Leetcode 0367. Valid Perfect Square
367. Valid Perfect Square题目Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Example 1: Input: 16 Output: true Example 2: Input: 14 Output: false 完全平方数可以表示为连续奇数的和。 算法原理该算法利用了以下数学特性: 1 = 1(1²) 1 + 3 = 4(2²) 1 + 3 + 5 = 9(3²) 1 + 3 + 5 + 7 = 16(4²) 以此类推,n² = 1 + 3 + 5 + ... + (2n-1) 算法通过不断从目标数中减去连续的奇数(1, 3, 5, 7...),如果最终结果恰好为 0,则说明该数是完全平方数。 123456789...
auto 关键字在 C++ 迭代器遍历中的应用
一、auto 关键字简化迭代器遍历在 C++11 之前,迭代器遍历代码往往冗长且可读性差: 类型声明冗长,增加代码量和阅读负担 容易出现类型不匹配错误 当容器类型改变时,需要修改所有相关迭代器声明 代码不够直观,隐藏了真正的业务逻辑 C++11 引入的auto关键字彻底改变了迭代器的使用方式,让我们能够完全摆脱冗长的传统迭代器声明。auto能够自动推导迭代器类型,使代码更加简洁、可读且不易出错。 1.1 基本迭代模式1234567891011121314151617181920#include <vector>#include <iostream>int main() { std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9}; // 使用auto声明迭代器 for (auto it = numbers.begin(); it != numbers.end(); ++it) { std::cout <&...

