每日一题02

每日一题 双指针算法 leetcode 27 移出元素 https://leetcode.cn/problems/remove-element/ 代码: cpp: class Solution { public: int removeElement(vector<int>& nums, int val) { int fast = 0; int slow = 0; for (fast = 0; fast < nums.size(); fast++){ if(nums[fast] != val){ nums[slow++] = nums[fast]; } } return slow; } }; python: class Solution: def removeElement(self, nums: List[int], val: int) -> int: slow, fast = 0, 0 for fast in range(len(nums)): if nums[fast] != val: nums[slow] = nums[fast] slow += 1 return slow leetcode 26.删除有序数组中的重复项 https://leetcode.cn/problems/remove-duplicates-from-sorted-array/ 代码 cpp: class Solution { public: int removeDuplicates(vector<int>& nums) { int fast; int slow = 0; for(fast = 1; fast < nums.size(); fast++){ if(nums[fast] != nums[slow]){ slow++; nums[slow] = nums[fast]; } } return slow + 1; } }; python: ...

2023年04月03日 · 2 分钟 · 596 字 · ZhaoYang

每日一题01

每日一题 二分查找 leetcode 704 二分查找 https://leetcode.cn/problems/binary-search/ 题解: CPP: class Solution { public: int search(vector<int>& nums, int target) { int left = 0; int right = nums.size() - 1; while(left < right){ int middle = left + right >> 1; if(nums[middle] >= target){ right = middle; }else{ left = middle + 1; } } if(nums[left] == target) return left; return -1; } }; class Solution { public: int search(vector<int>& nums, int target) { int left = 0; int right = nums.size() - 1; while(left < right){ int middle = left + right + 1 >> 1; if(nums[middle] > target){ right = middle - 1; }else{ left = middle; } } if(nums[left] == target) return left; return -1; } }; python: class Solution: def search(self, nums: List[int], target: int) -> int: left, right = 0,len(nums) - 1 while left < right: middle = left + right + 1 >> 1 if nums[middle] > target: right = middle - 1 elif nums[middle] <= target: left = middle if nums[left] == target: return left return -1 class Solution: def search(self, nums: List[int], target: int) -> int: left, right = 0,len(nums) - 1 while left < right: middle = left + right >> 1 if nums[middle] >= target: right = middle elif nums[middle] < target: left = middle + 1 if nums[left] == target: return left return -1 leetcode 35.搜索插入位置 https://leetcode.cn/problems/search-insert-position/ ...

2023年04月02日 · 3 分钟 · 1028 字 · ZhaoYang

STL学习 07

十三、STL函数 beg为序列的初始地址 end为序列的尾地址 accumulate accumulate(beg, end, init) 复杂度: O(N) 作用:对一个序列的元素求和 init为对序列元素求和的初始值 返回值类型:与init 相同 基础累加求和: ...

2023年03月24日 · 8 分钟 · 3699 字 · ZhaoYang

STL学习 06

十一、array数组 11.1 介绍 头文件: #include<array> array是C++11新增的容器,效率与普通数据相差无几,比vector效率要高,自身添加了一些成员函数。 和其它容器不同,array 容器的大小是固定的,无法动态的扩展或收缩,只允许访问或者替换存储的元素。 ...

2023年03月23日 · 4 分钟 · 1511 字 · ZhaoYang

STL学习 04

六、map 6.1 介绍 映射类似于函数的对应关系,每一个x对应一个y,而map是每个键对应一个值。和python的字典非常相似。 //头文件 #include<map> //初始化定义 map<string,string> mp; map<string,int> mp; map<int,node> mp;//node是结构体类型 map特性:map会按照键的顺序从小到大自动排序,键的类型必须可以比较大小 ...

2023年03月22日 · 7 分钟 · 3249 字 · ZhaoYang

STL学习 05

九、string 9.1 介绍 string是一个字符串类,和char型字符串类似。 可以把string理解为一个字符串类型,像int一样可以定义。 9.2 初始化及定义 //头文件 #include<string> //1. string str1;//生成空字符串 //2. string str2("123456789");//生成"123456789"的复制品 //3. string str3("12345".0,3);//结果为"123",从0开始,长度为3 //4. string str4("12345",5);//结果为"12345",长度为5 //5. string str5(5,'2');//结果为"22222",构造5个字符'2'连接成的字符串 //6. string str6(str2,2);//结果为"3456789",截取第三个元素(2对应第三位)到最后 简单使用 ...

2023年03月22日 · 8 分钟 · 4003 字 · ZhaoYang

STL学习 02

二、Stack 2.1 介绍 栈作为数据结构中的一种,是STL实现的一个先进先出,后进后出的容器。 //头文件添加 #include<stack> //声明 stack<int> s; stack<string> s; stack<node> s;//node是结构体类型 2.2 方法函数 代码 含义 s.push(ele) 元素ele入列,增加元素O(1) s.pop() 移出栈顶元素O(1) s.top() 取得栈顶元素(但是不删除)O(1) s.empty() 检测栈内是否为空,空为真O(1) s.size() 返回栈内元素个数O(1) 2.3 栈遍历 2.3.1 栈遍历 栈只能对栈顶元素进行操作,如果想要进行遍历,只能将栈中元素一个个取出来存在数组中 ...

2023年03月21日 · 1 分钟 · 433 字 · ZhaoYang