Leetcode 0005. Longest Palindromic Substring (python)
5. Longest Palindromic Substring题目Given a string s, return the longest palindromic substring in s. Example 1: 123Input: s = "babad"Output: "bab"Explanation: "aba" is also a valid answer. Example 2: 12Input: s = "cbbd"Output: "bb" Constraints: 1 <= s.length <= 1000 s consist of only digits and English letters. 题目大意在给定字符串中找出最长的回文子串(palindromic substring)。回文串是指正读反读都一样的字符串。若存在多个等长的最长回文子串,返回任意一个即可。字符串长度上限为 1000,仅包含数字和英文字母。 你选用何种方法解题?本题的核...
Leetcode 0509. Fibonacci Number
509. Fibonacci Number题目The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, F(0) = 0, F(1) = 1 F(N) = F(N - 1) + F(N - 2), for N > 1. Given N, calculate F(N). Example 1: Input: 2 Output: 1 Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1. Example 2: Input: 3 Output: 2 Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2. Example 3: Input: 4 Output: 3 Explanation: F(4) ...
C++ 写时复制 (Copy-on-Write)
一、写时复制核心概念写时复制 (简称 COW) 是一种资源管理优化技术,其核心思想是:当多个对象需要共享同一资源时,直到其中一个对象需要修改资源前,都不需要真正复制资源,仅在修改时才创建资源的私有副本。 这种机制通过延迟复制操作,减少了不必要的内存分配和数据拷贝,从而提高程序性能,尤其适用于: 频繁复制但很少修改的场景 内存资源宝贵的环境 大型数据结构的共享访问 二、写时复制实现三要素1. 共享数据存储需要一个独立的共享数据结构,存储实际的数据内容。 1234567template <typename T>struct SharedData { T* data; // 实际数据指针 size_t size; // 数据大小 size_t ref_count; // 引用计数 // 其他元数据...}; 2. 引用计数机制通过引用计数跟踪当前有多少对象共享该资源: 当新对象共享资源时,引用计数 + 1 当对象销毁或不再共享时,引用计数 - 1 当引用计数为 0 时,释放共享资源 ...
Leetcode 0004. Median of Two Sorted Arrays (python)
4. Median of Two Sorted Arrays题目Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: 123Input: nums1 = [1,3], nums2 = [2]Output: 2.00000Explanation: merged array = [1,2,3] and median is 2. Example 2: 123Input: nums1 = [1,2], nums2 = [3,4]Output: 2.50000Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. 题目大意给定两个已排序的数组 nums1 和 nums2,找出这两个有序数组的中位数。...
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 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...
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...
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],左叶子为 ...

