- 博客(42)
- 收藏
- 关注
原创 C++中实现全排列方法
在 C++ 中实现全排列(Permutations)有多种方法,这里我将介绍两种常用的方法:递归方法和使用 STL 的 next_permutation 方法。这种方法利用 C++ STL 中的 next_permutation 函数,它可以生成下一个排列直到所有的排列都被生成。递归方法 更直观地展示了全排列的生成过程,通过固定一个元素的位置并递归处理剩余元素。// 先排序,确保从最小的排列开始。
2025-01-31 14:25:07
817
原创 C++中vector追加vector
在C++中,如果你想将一个vector追加到另一个vector的后面,可以使用std::vector的成员函数insert或者std::copy,或者简单地使用std::vector的push_back方法逐个元素添加。通常,推荐使用insert方法或std::copy方法,因为它们在内部实现上通常更高效,尤其是在处理大量数据时。#include <algorithm> // 可能需要包含此头文件以使用std::copy_if(此处仅为示例,实际上不需要)// 使用范围for循环(不推荐,仅用于演示)
2025-01-31 14:24:30
664
原创 C++ vector 结构体排序
class Solution {public: /** * @param A: an integer array * @param target: An integer * @param k: An integer * @return: an integer array */ struct data{ int ...
2020-04-12 14:39:41
424
原创 有n个柱子,现在要给柱子染色,有k种颜色可以染,求有多少种染色方案
一、思路使用递推式二、代码这是 www.lintcode.com 上面的 第514道题有n个柱子,现在要给柱子染色,有k种颜色可以染,求有多少种染色方案?题目要求说,不存在超过2个相邻的柱子颜色相同思路::::当 只有 1 个柱子的时候k 种染色方案当 有2 个柱子的时候k*k 中染色方案...
2020-04-06 16:25:44
675
1
原创 1851. 购买通行证 亚历克斯计划参观博物馆,并在柜台上购买通行证。
算法1:过程模拟(算法复杂度O(N),其中N是最终的时间)class Solution {public: /** * @param arr: the line * @param k: Alex place * @return: the time when Alex requires to buy all passes */ int ...
2020-03-28 19:26:06
1313
原创 109. 数字三角形 中文English 给定一个数字三角形,找到从顶部到底部的最小路径和。每一步可以移动到下面一行的相邻数字上。
算法1:递归dfs(结果超时)class Solution {public: /** * @param triangle: a list of lists of integers * @return: An integer, minimum path sum */ int minimumTotal(vector<vector<int...
2020-03-28 17:30:10
738
原创 DFS和BFS
一、实现方法DFS用递归实现;BFS用队列实现;二、应用问题BFS可以用于求解路径长度/节点深度;DFS可以用于求解有向无环图的拓扑序;
2020-03-28 00:14:39
173
原创 IF语句陷阱
921.统计唯一值子树个数中文English给定一棵二叉树,统计唯一值子树的数目.唯一值子树意味着子树的所有节点都具有相同的值.错误代码实例(错误原因if语句实际执行会”偷懒”,如果if的结果已经确定就不会算剩下的表达式了)/** * Definition of TreeNode: * class TreeNode { * public: * int val...
2020-03-27 15:26:07
237
原创 机器学习算法导论(摘自赵卫东《机器学习》)
一、算法分类 机器学习算法是一类从数据中自动分析获得规律,并利用规律对未知数据进行预测的方法,可以分成下面几种类别:监督学习、无监督学习、强化学习。 (1)监督学习是从有标记的训练数据中学习一个模型,然后根据这个模型对未知样本进行预测。其中,模型的输入是某一样本的特征,函数的输出是这一样本对应的标签。常见的监督学习算法包括回归分析和统计分类。监督学习包括分类和数字预测两大类,前者包括逻...
2020-03-25 16:12:58
2177
原创 Hadoop分布式存储和计算
1.Hadoop是什么Hadoop是由java语言编写的,在分布式服务器集群上存储海量数据并运行分布式分析应用的开源框架,其核心部件是HDFS与MapReduce。HDFS是一个分布式文件系统:引入存放文件元数据信息的服务器Namenode和实际存放数据的服务器Datanode,对数据进行分布式储存和读取。MapReduce是一个计算框架:MapReduce的核心思想是把计算任务分配给集群内的...
2020-02-29 20:39:43
1842
原创 监督学习1
#一、统一模型Hypothesis― The hypothesis is notedhθhθand is the model that we choose. For a given input datax(i)x(i)the model prediction output ishθ(x(i))hθ(x(i)).Loss function― A loss function is...
2020-02-23 17:49:42
227
原创 强化学习1
一、前言强化学习又称增强学习,相关关键词:动态规划、最优控制。一般获得最优控制的过程可以理解为强化学习的过程。二、强化学习的历史 强化学习历史有两条主线,漫长而丰富,在交织成为现代强化学习之前各自独立发展。一条主线考虑通过始于动物学习心理学的试错方法进行学习。这条线贯穿人工智能的一些早期工作,引发了20世纪80年代的强化学习复兴。另一条主线考虑优化控制问题及使用价值函数和动态规划解决...
2020-02-15 13:49:20
507
原创 LintCode 1210 给定一个整数数组,找到所有不同的可能的升序子序列,一个升序子序列的长度至少应为2。
因为数据有重复所以回溯法会给出重复的结果,需要set去重复。class Solution {public: /** * @param nums: an integer array * @return: all the different possible increasing subsequences of the given array */ ...
2019-09-19 23:49:38
572
原创 LintCode 1278 给定一个非空的二维矩阵 matrix 和一个整数 k,找出 matrix 中的一个矩形,该矩形内的元素之和最大并且不超过 k,返回这个最大和。
TLEclass Solution {public: /** * @param matrix: a 2D matrix * @param k: an integer * @return: the max sum of a rectangle in the matrix such that its sum is no larger than k ...
2019-09-19 16:10:37
388
原创 LintCode 680 给一个字符串,你可以选择在一个字符或两个相邻字符之后拆分字符串,使字符串由仅一个字符或两个字符组成,输出所有可能的结果
class Solution {public: /* * @param : a string to be split * @return: all possible split string array */ vector<vector<string>> ans; vector<vector<string...
2019-09-12 20:54:53
333
原创 LintCode 375 深度复制一个二叉树。 给定一个二叉树,返回一个他的 克隆品 。
解一、返回值在return/** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this-&...
2019-09-12 20:37:58
341
原创 LintCode 570 给一个由 1 - n 的整数随机组成的一个字符串序列,其中丢失了一个整数,请找到它。
情况特殊,考虑逻辑即可class Solution {public: /** * @param n: An integer * @param str: a string with number from 1-n in random order and miss one number * @return: An integer */ i...
2019-09-12 20:13:10
313
原创 LintCode 778 给定一个m×n的非负矩阵代表一个大洲,矩阵的每个单元格的值代表此处的地形高度,矩阵的左边缘和上边缘是“太平洋”,下边缘和右边缘是“大西洋”。
一、遍历每个点,每个点来一次dfs,结果超时class Solution {public: /** * @param matrix: the given matrix * @return: The list of grid coordinates */ vector<vector<int>> globalans; ...
2019-09-12 16:31:47
403
原创 LintCode 677 给一个布尔类型的二维数组, 0 表示海, 1 表示岛。如果两个1是相邻的,那么我们认为他们是同一个岛.我们只考虑 上下左右 相邻. 找到大小在 k 及 k 以上的岛屿的数量
DFSclass Solution {public: /** * @param grid: a 2d boolean array * @param k: an integer * @return: the number of Islands */ int numsofIsland(vector<vector<bool>...
2019-09-11 23:30:55
948
原创 LintCode 941 在一块大小为 2x3 的板上,有 5 块瓦片,分别用整数 1 到 5 表示,还有一块空地用 0 表示。
BFS求最小步数class Solution {public: /** * @param board: the given board * @return: the least number of moves required so that the state of the board is solved */ string vector2...
2019-09-11 22:12:41
200
原创 LintCode 889 给定一个行rows x cols屏幕和一个由非空单词列表表示的句子sentence,找出在屏幕上可以安装多少次给定的语句。
class Solution {public: /** * @param sentence: a list of string * @param rows: an integer * @param cols: an integer * @return: return an integer, denote times the given sente...
2019-09-10 17:25:10
757
原创 LintCode 515 这里有n个房子在一列直线上,现在我们需要给房屋染色,分别有红色蓝色和绿色。每个房屋染不同的颜色费用也不同,你需要设计一种染色方案使得相邻的房屋颜色不同,并且费用最小
class Solution {public: /** * @param costs: n x 3 cost matrix * @return: An integer, the minimum cost to paint all houses */ int minCost(vector<vector<int>> &c...
2019-09-10 17:02:59
414
原创 LintCode 669 给出不同面额的硬币以及一个总金额. 写一个方法来计算给出的总金额可以换取的最少的硬币数量. 如果已有硬币的任意组合均无法与总金额面额相等, 那么返回 -1.
class Solution {public: /** * @param coins: a list of integer * @param amount: a total amount of money amount * @return: the fewest number of coins that you need to make up *...
2019-09-10 16:31:26
1457
原创 LintCode 107 给定字符串 s 和单词字典 dict,确定 s 是否可以分成一个或多个以空格分隔的子串,并且这些子串都在字典中存在。
代码一、使用递归算法,超时class Solution {public: /* * @param s: A string * @param dict: A dictionary of words dict * @return: A boolean */ bool wordBreak(string &s, unordered_s...
2019-09-10 16:08:39
1792
原创 LintCode 94 给出一棵二叉树,寻找一条路径使其路径和最大,路径可以在任一节点中开始和结束(路径和为两个节点之间所在路径上的节点权值之和)
/** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this-...
2019-09-10 14:28:09
683
原创 LintCode 77 给出两个字符串,找到最长公共子序列(LCS),返回LCS的长度。
class Solution {public: /** * @param A: A string * @param B: A string * @return: The length of longest common subsequence of A and B */ int longestCommonSubsequence(strin...
2019-09-10 13:42:58
1004
原创 LintCode 29 给出三个字符串:s1、s2、s3,判断s3是否由s1和s2交叉构成。
class Solution {public: /** * @param s1: A string * @param s2: A string * @param s3: A string * @return: Determine whether s3 is formed by interleaving of s1 and s2 */ ...
2019-09-10 10:58:48
1136
原创 LintCode 669 给出不同面额的硬币以及一个总金额. 写一个方法来计算给出的总金额可以换取的最少的硬币数量. 如果已有硬币的任意组合均无法与总金额面额相等, 那么返回 -1.
class Solution {public: /** * @param coins: a list of integer * @param amount: a total amount of money amount * @return: the fewest number of coins that you need to make up *...
2019-09-10 10:17:07
730
原创 LintCode 95 在n个物品中挑选若干物品装入背包,最多能装多满?假设背包的大小为m,每个物品的大小为A[i]
class Solution {public: /** * @param m: An integer m denotes the size of a backpack * @param A: Given n items with size A[i] * @return: The maximum size */ int backPack(i...
2019-09-09 23:38:50
1547
原创 LintCode 91 给一个整数数组,调整每个数的大小,使得相邻的两个数的差不大于一个给定的整数target,调整每个数的代价为调整前后的差的绝对值,求调整代价之和最小是多少。
class Solution {public: /* * @param A: An integer array * @param target: An integer * @return: An integer */ int MinAdjustmentCost(vector<int> &A, int target) {...
2019-09-09 22:59:01
764
原创 LintCode 90 给定n个不同的正整数,整数k以及一个目标数字。在这n个数里面找出K个数,使得这K个数的和等于目标数字,你需要找出所有满足要求的方案.
class Solution {public: /* * @param A: an integer array * @param k: a postive integer <= length(A) * @param target: an integer * @return: A list of lists of integer *...
2019-09-09 22:12:53
2481
原创 LintCode 33 n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击(任意两个皇后不能位于同一行,同一列,同一斜线)。
class Solution {public: /* * @param n: The number of queens * @return: All distinct solutions */ vector<vector<string> > ans; vector<vector<string>> solveNQueens(int...
2019-09-09 21:23:24
5614
原创 LintCode 1101 给定一颗二叉树, 编写一个函数求该树的最大宽度, 即该树每一层的宽度的最大值. 该二叉树具有与满二叉树相同的结构, 但是一些节点为空 null. 注
/** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this-...
2019-09-09 16:19:34
530
原创 LintCode 70 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历)
/** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this-...
2019-09-09 15:01:41
704
原创 联想服务器System X 3650 M5 raid配置和U盘装win7系统
这台服务器需要先进行raid配置,把实际的三块硬盘(共900G)通过raid(RAID5)配置成一个虚拟硬盘(500多G)。可以看出raid 通过牺牲总的空间来提高读取速度和防止数据丢失。只有配置好了虚拟硬盘,才能通过U盘安装系统,其他的和PC安装系统一样,把虚拟硬盘当成普通硬盘,然后把系统安装进去即可。...
2019-03-16 21:25:29
12221
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人