备赛蓝桥杯--算法题目(1)

embedded/2024/11/24 10:14:51/
1. 链表求和

. - 力扣(LeetCode)

class Solution {
public:ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {ListNode *head = nullptr, *tail = nullptr;int carry = 0;while (l1 || l2) {int n1 = l1 ? l1->val: 0;int n2 = l2 ? l2->val: 0;int sum = n1 + n2 + carry;if (!head) {head = tail = new ListNode(sum % 10);} else {tail->next = new ListNode(sum % 10);tail = tail->next;}carry = sum / 10;if (l1) {l1 = l1->next;}if (l2) {l2 = l2->next;}}if (carry > 0) {tail->next = new ListNode(carry);}return head;}
};
2. 分割链表 

. - 力扣(LeetCode)

class Solution {
public:ListNode* partition(ListNode* head, int x) {ListNode* shead=new ListNode;ListNode* srear=shead;ListNode* bhead=new ListNode;ListNode* brear=bhead;ListNode* tmp=head;while(tmp){if((tmp->val)>=x){if(bhead==nullptr){bhead=brear=tmp;}else{brear->next=tmp;brear=brear->next;}}else{if(shead==nullptr){shead=srear=tmp;}else{srear->next=tmp;srear=srear->next;}}tmp=tmp->next;}brear->next=nullptr;srear->next=bhead->next;return shead->next;}
};
3. 最小栈

. - 力扣(LeetCode)

class MinStack {
public:/** initialize your data structure here. */MinStack() {s1=new stack<int>;s2=new stack<int>;}void push(int x) {s1->push(x);if(s2->empty()){s2->push(x);}else{if(x<=s2->top()){s2->push(x);}}}void pop() {if(s1->top()==s2->top()){s2->pop();}s1->pop();}int top() {return s1->top();}int getMin() {return s2->top();}
private:stack<int>* s1;stack<int>* s2;
};
4. 二叉树的前序,中序,后序遍历

. - 力扣(LeetCode)

#include<stack>
class Solution {
public:vector<int> preorderTraversal(TreeNode* root) {vector<int> tmp;if(root!=nullptr){TreeNode* head=root;stack<TreeNode*> s;s.push(head);while(!s.empty()){head=s.top();tmp.push_back(head->val);s.pop();if(head->right!=nullptr){s.push(head->right);}if(head->left!=nullptr){s.push(head->left);}}}return tmp;}
};

. - 力扣(LeetCode)

class Solution {
public:vector<int> inorderTraversal(TreeNode* root) {vector<int> tmp;stack<TreeNode*> s;TreeNode* head=root;while(!s.empty()||head!=nullptr){while(head!=nullptr){s.push(head);head=head->left;}head=s.top();tmp.push_back(head->val);s.pop();head=head->right;}return tmp;}
};

. - 力扣(LeetCode)

class Solution {
public:vector<int> postorderTraversal(TreeNode* root) {vector<int> tmp;stack<TreeNode*> s;TreeNode* head=root;TreeNode* ptr=root;while(!s.empty()||head!=nullptr){while(head!=nullptr){s.push(head);head=head->left;}head=s.top();s.pop();if(head->right==nullptr||head->right==ptr){tmp.push_back(head->val);ptr=head;head=nullptr;}else{s.push(head);head=head->right;}}return tmp;}
};
5. 设计循环队列

. - 力扣(LeetCode)

class MyCircularQueue {
private:int front;int rear;int capacity;vector<int> elements;public:MyCircularQueue(int k) {this->capacity = k + 1;this->elements = vector<int>(capacity);rear = front = 0;}bool enQueue(int value) {if (isFull()) {return false;}elements[rear] = value;rear = (rear + 1) % capacity;return true;}bool deQueue() {if (isEmpty()) {return false;}front = (front + 1) % capacity;return true;}int Front() {if (isEmpty()) {return -1;}return elements[front];}int Rear() {if (isEmpty()) {return -1;}return elements[(rear - 1 + capacity) % capacity];}bool isEmpty() {return rear == front;}bool isFull() {return ((rear + 1) % capacity) == front;}
};
6. 排序数组

. - 力扣(LeetCode)

static int first,last;
class Solution {vector<int> tmp;void mergeSort(vector<int>& nums, int l, int r) {if (l >= r) return;int mid = (l + r) >> 1;mergeSort(nums, l, mid);mergeSort(nums, mid + 1, r);int i = l, j = mid + 1;int cnt = 0;while (i <= mid && j <= r) {if (nums[i] <= nums[j]) {tmp[cnt++] = nums[i++];}else {tmp[cnt++] = nums[j++];}}while (i <= mid) {tmp[cnt++] = nums[i++];}while (j <= r) {tmp[cnt++] = nums[j++];}for (int i = 0; i < r - l + 1; ++i) {nums[i + l] = tmp[i];}}void randomized_quicksort(vector<int>& nums, int l, int r) {if (l < r) {int j = rand() % (r - l + 1) + l;first=l,last=r;int i=l,x=nums[j];while(i<=last){if(nums[i]==x){i++;}else if(nums[i]<x){swap(nums[first++],nums[i++]);}else{swap(nums[last--],nums[i]);}}int left=first,right=last;randomized_quicksort(nums, l, left - 1);randomized_quicksort(nums, right + 1, r);}}
public:vector<int> sortArray(vector<int>& nums) {srand((unsigned)time(NULL));tmp.resize((int)nums.size(), 0);//mergeSort(nums, 0, (int)nums.size() - 1);randomized_quicksort(nums,0,(int)nums.size() - 1);return nums;}
};
7. 求数组第k个最大元素

. - 力扣(LeetCode)

static int first,last;
class Solution {
public:int findKthLargest(vector<int>& nums, int k) {int m=nums.size()-k;srand((unsigned)time(NULL));return test(nums,m);}int test(vector<int>& nums,int i){int ans = 0;for (int l = 0, r =nums.size() - 1; l <= r;) {partition(nums, l, r, nums[l + rand()%(r-l+1)]);if (i < first) {r = first - 1;} else if (i > last) {l = last + 1;} else {ans = nums[i];break;}}return ans;}void partition(vector<int>& nums, int l, int r, int x) {first = l;last = r;int i = l;while (i <= last) {if (nums[i] == x) {i++;} else if (nums[i] < x) {swap(nums[first++],nums[i++]);} else {swap(nums[last--],nums[i]);}}}
};


http://www.ppmy.cn/embedded/140102.html

相关文章

Java编程,配置mongoUri连接mongodb时,需对特殊字符进行转义

一、背景 java程序连接mongo有两种方式&#xff1a; 用户名和密码方式uri方式 1、用户名和密码 以用户数据库为例&#xff0c;注意看它的密码 spring:data:mongodb:host: 192.168.10.17database: db_user_serviceport: 3717username: user_servicepassword: user_service3…

java基础概念37:正则表达式2-爬虫

一、定义 【回顾】正则表达式的作用 作用一&#xff1a;校验字符串是否满足规则作用二&#xff1a;在一段文本中查找满足要求的内容——爬虫 二、本地爬虫VS网络爬虫 2-1、本地爬虫 示例&#xff1a; 代码优化&#xff1a; public static void main(String[] args) {// 大…

Level DB --- SkipList

class SkipList class SkipList 是Level DB中的重要数据结构&#xff0c;存储在memtable中的数据通过SkipList来存储和检索数据&#xff0c;它有优秀的读写性能&#xff0c;且和红黑树相比&#xff0c;更适合多线程的操作。 SkipList SkipList还是一个比较简单的数据结构&a…

接口设计中的数据精简技巧:提升效率与优化传输

文章目录 摘要引言数据精简的核心技术字段筛选数据压缩数据分页数据缓存 使用 ArkUI 和 ArkTS 实战示例字段筛选接口设计后端接口代码&#xff08;伪代码&#xff09;前端调用代码&#xff08;ArkTS实现&#xff09;ArkUI 界面实现数据压缩 实现逻辑详解字段筛选的实现逻辑数据…

Redis的基本使用命令(GET,SET,KEYS,EXISTS,DEL,EXPIRE,TTL,TYPE)

目录 SET GET KEYS EXISTS DEL EXPIRE TTL redis中的过期策略是怎么实现的&#xff08;面试&#xff09; 上文介绍reids的安装以及基本概念&#xff0c;本章节主要介绍 Redis的基本使用命令的使用 Redis 是一个基于键值对&#xff08;KEY - VALUE&#xff09;存储的…

HarmonyOs鸿蒙开发实战(20)=>一文学会基础使用组件导航Navigation

敲黑板&#xff0c;以下是重点技巧。文章末尾有实战项目效果截图及代码截图可参考 1.概要 Navigation是路由导航的根视图容器Navigation组件主要包含​导航页&#xff08;NavBar&#xff09;和子页&#xff08;NavDestination&#xff09;&#xff0c;导航页不存在页面栈中&am…

Python数据结构之链表

一、链表 1、目的 解决顺序表存储数据有上限&#xff0c;并且插入和删除操作效率低的问题。 2、概念 链表&#xff1a;链式存储的线性表&#xff0c;使用随机的物理内存存储逻辑上连续的数据。 链表的组成&#xff1a;由一个个结点组成 结点&#xff1a;由数据域和链接域组…

用Python爬虫“偷窥”1688搜索词推荐:一场数据的奇妙冒险

在这个信息爆炸的时代&#xff0c;数据就像是藏在深海里的宝藏&#xff0c;等待着勇敢的探险家去发掘。今天&#xff0c;我们将化身为数据海盗&#xff0c;用Python作为我们的船只&#xff0c;航向1688的海域&#xff0c;去“偷窥”那些神秘的搜索词推荐。准备好了吗&#xff1…