Leetcode 0011. Container With Most Water
11. Container With Most Water题目Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of...
C++ 中->和*运算符重载的全面解析
引言在 C++ 编程中,->和*运算符最初设计用于指针操作。然而,通过运算符重载机制,我们可以让自定义类型也支持这些操作,从而实现类似指针的行为,同时添加额外功能。 一、为什么需要重载->和*运算符1. 扩展指针功能的核心需求原生指针 (T*) 虽然简洁高效,但存在明显局限: 无法自动管理资源生命周期 缺乏访问控制机制 不支持额外的调试信息 不能实现代理或间接访问模式 通过重载->和*,我们可以创建 "智能指针" 或 "代理对象",在保持指针操作语法的同时,添加所需功能。 2. 关键应用场景资源自动管理智能指针通过运算符重载实现自动内存管理: 1234567891011121314151617181920// 简化的shared_ptr实现思路template<typename T>class SharedPtr {private: T* ptr; int* ref_count; void release() { if...
Leetcode 0010. Regular Expression Matching
10. Regular Expression MatchingGiven an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where: '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). Example 1: 123Input: s = "aa", p = "a"Output: falseExplanation: "a" does not match the entire string "aa". Example...
Leetcode 0009. Palindrome Number
9. Palindrome Number题目Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: 12Input: 121Output: true Example 2: 123Input: -121Output: falseExplanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: 123Input: 10Output: falseExplanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it without converting...
Leetcode 0008. String to Integer (atoi)
8. String to Integer (atoi)题目Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function). The algorithm for myAtoi(string s) is as follows: Read in and ignore any leading whitespace. Check if the next character (if not already at the end of the string) is '-' or '+'. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is...
Leetcode 0007. Reverse Integer
7. Reverse Integer题目Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 **Note:**Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. 题目大意给出一个 32 位的有符号整数,需要将这个整数中每位上的数字进行反转。注意:假设我们的环境只能存储得下 32...
Leetcode 0005. Longest Palindromic Substring
5. Longest Palindromic Substring题目Given a string s, return the longest palindromic substring in s. Example 1: 1234Input: s = "babad"Output: "bab"Note: "aba" is also a valid answer. Example 2: 123Input: s = "cbbd"Output: "bb" Example 3: 123Input: s = "a"Output: "a" Example 4: 123Input: s = "ac"Output: "a" Constraints: 1 <= s.length <= 1000 s consist of only digits and English letters...
Leetcode 0004. Median of Two Sorted Arrays
4. Median of Two Sorted Arrays题目There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). You may assume nums1 and nums2 cannot be both empty. Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1, 2] nums2 = [3, 4] The median is (2 + 3)/2 = 2.5 题目大意给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m +...
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...
Leetcode 0003. Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters题目Given a string, find the length of the longest substring without repeating characters. Example 1: 123Input: "abcabcbb"Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: 123Input: "bbbbb"Output: 1Explanation: The answer is "b", with the length of 1. Example 3: 1234Input: "pwwkew"Output: 3Explanation: The answer is "wke", with the length of 3. ...
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: // 构造函数,用于创建...
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 zeros, 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...
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 zeros, 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...
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, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: 1234Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,return [0, 1]. 12345678910from typing import Listclass Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: seen = {} for i, num...
C++ 单例模式的四种自动释放方式详解
导言单例模式的自动释放是解决资源泄漏问题的关键,不同场景下可以选择不同的实现方式。下面详细解析四种典型的自动释放机制,包括其实现原理、代码示例及适用场景。 一、方式一:利用栈对象的生命周期进行管理实现原理利用栈对象 "出作用域自动析构" 的特性,将单例指针存储在栈对象中,当栈对象被销毁时,自动释放单例资源。 代码实现12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758#include <iostream>class Singleton {private: // 私有构造函数 Singleton() { std::cout << "Singleton 构造函数被调用" << std::endl; } // 私有析构函数 ~Singleton() { ...
Leetcode 0001.two sum
1. Two Sum题目Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: 1234Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,return [0, 1]. 题目大意在数组中找到两个数,它们的和等于给定的目标值 target,并返回这两个数在数组中的下标。要求每个元素只能使用一次,且保证有且仅有一个解。 解题思路方法一:双层循环(暴力枚举)思路 通过双重循环遍历数组中的每一对元素,检查它们的和是否等于目标值 target。外层循环控制第一个元素的索引...
Python 格式规范
格式规范文件名格式1YYYY-MM-DD 标题.md 文章头部格式1234567891011---title: 文章标题tags: - Python - 相关标签categories: - Pythonseries: Pythonabbrlink: 唯一标识符date: YYYY-MM-DD HH:MM:SS--- 内容结构一、标题 二、简介 三、正文内容 四、代码示例(如有) 注意事项 每月计划5篇文章,时间在19:00-23:33之间,不出现整点时间 遵循统一的文件命名和格式规范 确保内容质量和准确性
友元机制与其他访问控制机制的区别与联系
导言在 C++ 面向对象编程中,访问控制机制是实现封装性的核心手段。友元机制和继承是两种主要的访问控制方式,它们既有联系又有显著区别。 一、本质区别 特性 友元机制 继承机制 关系性质 单向的 "授权" 关系 父子间的 "派生" 关系 访问目的 临时突破封装边界 实现代码复用与扩展 关系方向 非对称(A 是 B 的友元≠B 是 A 的友元) 可传递(间接继承) 生命周期 编译期静态确定 运行期动态体现(多态) 代码耦合度 低到中等(仅需声明) 高(子类依赖父类实现) 二、访问权限差异1. 友元机制的访问特点 可以直接访问所有私有成员(private)和保护成员(protected) 无需通过类的接口(public 成员)进行访问 访问权限是单向且不可传递的 示例: 12345678910111213141516171819class A {private: int x; friend class B; // B可以直接访问A的私有成员};class B...
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...

