
算法
文章平均质量分 56
刘好念
真诚、勇敢、善良
展开
-
[C++] C++生成随机数
本文给出了C++11中常用的用于生成等特定分布的随机数代码。原创 2024-03-11 14:16:24 · 884 阅读 · 0 评论 -
[算法] 使用位运算遍历集合的子集
位运算遍历使用状态压缩方法表示的子集。原创 2022-09-20 14:39:51 · 590 阅读 · 0 评论 -
[算法] 埃式筛和欧拉筛选法简要介绍
本文介绍了埃拉托斯特尼筛选法和欧拉筛选法基本算法流程。原创 2021-11-21 20:25:24 · 646 阅读 · 0 评论 -
[STL] 标准二分算法模板 && lower_bound() upper_bound()代码解析
一、摘要二分算法是经常使用的算法之一,熟练使用二分算法是一个程序员的基本素养。C++的<algorithm>头文件中存在lower_bound()和upper_bound()函数,支持在已排好序的容器中查找首个大于等于或者大于目标元素的迭代器位置。同时在有序容器类,例如set<>和map<>,中也存在类似功能的函数。熟练使用lower_bound()和upper_bound()函数可以方便地使用二分算法解决问题。本文基于< algorithm>,对lower原创 2021-08-29 11:18:27 · 702 阅读 · 0 评论 -
[Algorithm] Binary Indexed Tree 树状数组模板
一、背景这是我个人的树状数组模板记录,对于其他人可能没有借鉴意义。二、代码模板// 树状数组类// 下标从1开始class BinaryIndexedTree{public: // 构造函数,初始化数据数组c_,大小为总数据个数+1 BinaryIndexedTree(int n){ c_.resize(n+1); fill(c_.begin(), c_.end(), 0); } // 区间查询,查询[1,idx]范围内的数据和原创 2021-07-24 15:26:04 · 162 阅读 · 0 评论 -
[sort]O(n)复杂度查找中位数
前言使用快速排序算法思想,查找数组中的第k大的数。复杂度为O(n)O(n)O(n)。代码#include <iostream>#include <vector>#include <algorithm>// find medianusing namespace std;int getMedian(vector<int> &vec, int left, int right, int k) { int i = left; in原创 2021-02-26 09:32:33 · 758 阅读 · 1 评论 -
[PAT]1101 Quick Sort (25 分)(样例2段错误原因)
一、题意There is a classical process namedpartitionin the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are moved to ...原创 2019-08-30 18:58:57 · 468 阅读 · 2 评论 -
[leetcode]Leetcode中sort排序遇到的一些问题
转载自:https://www.cnblogs.com/flightless/p/10745318.htmlclass Solution {public: static bool cmp(vector<int>a,vector<int>b) { return a[0]-a[1]<b[0]-b[1]; } int...转载 2019-08-27 08:47:56 · 2978 阅读 · 1 评论 -
[LeetCode]1171. Remove Zero Sum Consecutive Nodes from Linked List
一、题意Given theheadof a linked list, we repeatedly delete consecutive sequences of nodes that sum to0until there are no such sequences.After doing so, return the head of the final linked list. ...原创 2019-08-26 21:45:33 · 536 阅读 · 0 评论 -
[PAT]1119 Pre- and Post-order Traversals (30 分)(样例1未过,运行时错误原因)
一、题目Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and in...原创 2019-08-15 10:54:00 · 408 阅读 · 0 评论 -
判断一个数是不是质数,3种方式介绍
本文参考博文判断一个数是不是质数(素数),3种方式介绍,原文章解释的已经很详细,本问增加部分博主自己的理解。一、概念介绍质数:质数是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数。特别的0,1不是质数。二、方法介绍1.最直接的方法bool isPrime(int n){ if(n<=3){ return n>1; }...转载 2019-08-14 16:51:19 · 4874 阅读 · 2 评论 -
[leetcode]32. Longest Valid Parentheses
题目描述Given a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (well-formed) parentheses substring.样例Example 1:Input: “(()”Output: 2Explanation: The longest...原创 2019-07-29 19:19:40 · 191 阅读 · 0 评论 -
[leetcode]10. Regular Expression Matching
题目给定一个字符串 (s) 和一个字符模式 (p)。实现支持 '.' 和 '*' 的正则表达式匹配。'.' 匹配任意单个字符。'*' 匹配零个或多个前面的元素。匹配应该覆盖整个字符串 (s) ,而不是部分字符串。说明s 可能为空,且只包含从 a-z 的小写字母。 p 可能为空,且只包含从 a-z 的小写字母,以及字符 . 和 *。示例 输入:s = "aa"...原创 2018-12-17 20:29:02 · 186 阅读 · 0 评论 -
Hanoi汉诺塔
汉诺塔代码#include <iostream>#include <stdio.h>using namespace std;//汉诺塔函数,参数,n剩余棋子数,prime棋子初始所在杆,help移动棋子需要借助的杆,target最终移动的目标杆void hanoi(int n, char prime, char help, char target){ if (n == 1){原创 2017-12-22 23:24:15 · 288 阅读 · 0 评论 -
Eight Queen
八皇后问题代码 #include <iostream>#include <stdio.h>#include <string.h>#include <math.h>#include <cmath>using namespace std;int p[8][8]; // 棋盘数组int cnt; //记录解的个数//判断(r,原创 2017-12-19 21:46:31 · 318 阅读 · 0 评论