![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
【 C + + 】
文章平均质量分 58
残阳摧雪
行有不得,反求诸己!
展开
-
改试卷(美团点评)
题目描述学生分为n组,每个组si个人,老师收取第一组所有试卷,发给第二组同学,同时收取第二组同学的试卷放在手中试卷的底下………一直到收取最后一组试卷,再将手中试卷发给第一组学生。这样存在两个问题:1,手中试卷不够发给下一组同学的;2,有的同学改自己的试卷这两个问题与老师收取试卷的顺序有一定关系,你能否设计老师收取试卷的顺序从而避免上述两个问题,若存在一定顺序,输出“Yes”,否则,输出“No”。输入原创 2017-09-02 10:32:26 · 431 阅读 · 0 评论 -
string的erase用法
erase有3种用法:(1)string& erase ( size_t pos = 0, size_t n = npos );(2)iterator erase ( iterator position );(3)iterator erase ( iterator first, iterator last );erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)原创 2017-08-29 20:20:12 · 1621 阅读 · 0 评论 -
异或运算
异或性质任何一个数字异或其自身都等于0,也就是,如果我们从头到尾依次异或数组中的每一个数字,那么最终的结果刚好是那个只出现一次的数字,因为那些出现两次的数字全部在异或中抵消掉了。例子:从N个数(每个数范围为1→N+11 \rightarrow N+1,这个N个数没有排好序)中,找出第一个miss的数。#include <iostream>#include <vector>using namespa原创 2017-09-07 11:54:30 · 348 阅读 · 0 评论 -
最长数字串
在一个字符串中找出最长的数字串,并把长度返回,如果存在长度相同的连续数字串,返回最后一个连续数字串。#include <iostream>#include <string>using namespace std;int test(){ string str; cin >> str; string max_str; int max = 0; int i = 0;原创 2017-09-06 21:26:27 · 222 阅读 · 0 评论 -
报数
n个人围成一个圈循环报数,数到interval的出圈,求报数序列。#include <iostream>#include <vector>using namespace std;int test(){ int n, interval; // 编号1..n,数到interval的退出 cin >> n >> interval; vector<bool> exit(n + 1, fa原创 2017-09-06 20:57:41 · 324 阅读 · 0 评论 -
C++ 基础练习[1]
1.定义一个”数据类型” datatype类,能处理包含字符型、整型、浮点型三种类型的数据,给出其构造函数。#include <iostream>using namespace std;class datatype{ enum{ character, integer, floating_point } vartype; uni原创 2017-08-27 11:11:58 · 1517 阅读 · 0 评论 -
最长的回文子串
Catcher 是MCA国的情报员,他工作时发现敌国会用一些对称的密码进行通信,比如像这些ABBA,ABA,A,123321,但是他们有时会在开始或结束时加入一些无关的字符以防止别国破解。比如进行下列变化 ABBA->12ABBA,ABA->ABAKK,123321->51233214 。因为截获的串太长了,而且存在多种可能的情况(abaaab可看作是aba,或baaab的加密形式),Cathcer原创 2017-09-05 13:26:09 · 230 阅读 · 0 评论 -
美图2017A卷第一题
时间限制:C/C++语言 1000MS;其他语言 3000MS内存限制:C/C++语言 65536KB;其他语言 589824KB题目描述:若[i,j]和[i’,j’]是矩阵的两个位置, 他们的距离定义为max(|i-i’|, |j-j’|)。已知: 1. 整数 n > r >= 0; 2. F为 n * n 的矩阵, 矩阵内元素属于集合{0, 1}; 3. 矩阵第i列第j行的元素用F原创 2017-09-04 21:23:28 · 383 阅读 · 0 评论 -
0-1背包问题
状态 &转移状态:f(i,C)f(i,C)表示考虑将[0..i]的背包放入容量为C的背包的最大价值 状态转移: f(i,C)=max{f(i−1,C),v[i]+f(i−1,C)}f(i,C)=\max \{f(i-1,C),v[i]+f(i-1,C) \}解题代码class Solution {private: vector<vector<int>> memo; // [0原创 2017-08-23 10:57:09 · 228 阅读 · 0 评论 -
416. Partition Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.Note:Each of the array element wi原创 2017-08-23 11:18:52 · 197 阅读 · 0 评论 -
Index Heap(索引堆)
数据和索引分开存储,其核心思想在于,shiftUp与shiftDown操作时,数据比较是比较的数据部分,做数据交换时则只是交换了索引。#include <iostream>#include <cassert>#include "SortTestHelper.h"using namespace std;// 最大索引堆template<typename Item>class IndexMaxH原创 2017-08-25 16:02:18 · 542 阅读 · 3 评论 -
K-Means聚类
什么是K-Means一种最简单的无监督聚类算法。步骤1. 初始化聚类中心最简单的确定初始类簇中心点的方法是随机选择K个点作为初始的类簇中心点,如下面k为要聚成几类,dataCount为数据量。for (int i = 0; i<k; i++){ centroidx[i] = x[rand() % dataCount]; centroidy[i] = y[rand() % dataCo原创 2017-09-03 18:13:52 · 397 阅读 · 0 评论 -
并查集(Union Find)
并查集能做什么并查集能高效回答一类类似连接的问题,具体的如迷宫中两点是否连通,图论中求最小生成树时判断是否形成环等。基本操作对于一组数据,主要支持两个操作:union(p,q):将p和q合并在一起;find(p):查p是属于哪个组的。用来回答一个问题:isConnected(p,q):回答p和q是否连在一起。Quick Find最初级版本的并查集,能高效的进行find操作,只需要O(1)的原创 2017-06-27 20:38:05 · 320 阅读 · 0 评论 -
string
头文件#include <string>using std::string;初始化#include <iostream>#include <string>using std::string;using namespace std;int main(){ // 几种不同的声明和初始化方式 string s1; string s2(s1); string s3("va原创 2017-08-18 10:25:02 · 198 阅读 · 0 评论 -
满足要求的连续子序列个数(大疆)
题目描述给出n个数,要求找出序列中有多少个连续子序列满足一下要求:子序列中任意一个数字出现的次数小于k输入: 第一行输入n和k,然后输入n个数字输出: 满足要求的子序列个数样例输入:4 11 2 2 3输出0思路分析此题可用滑动窗口解决,考察子串[i..j]是否满足要求,d[j]中记录以j结尾的最长子串长度,其实这个长度也就是以j结尾的满足要求的子串个数。最后将d[0]到d[n-1]加起来即为原创 2017-09-02 11:54:57 · 3679 阅读 · 11 评论 -
待深入理解
401. Binary Watch原创 2017-08-19 20:56:47 · 187 阅读 · 0 评论 -
求最后一个单词的长度
需要注意的点1)cin与getline的读取字符串的区别:cin>>s [第一个单词] 读取并忽略开头所有的空白字符(如空格、换行、制表符) 读取字符直到再次遇到空白字符,读取截止。getline(cin,s) [读一行] 不忽略开头的换行符,getline函数返回时会丢弃换行符,因此换行符不会存储在string对象中。2)注意处理末尾空格的情况。代码#include <iostream原创 2017-08-30 09:06:05 · 311 阅读 · 0 评论 -
ID3算法
关于ID3算法这里有一篇文章写的很通俗,本文转载至http://blog.csdn.net/acdreamers/article/details/44661149,原文内容大致如下。今天,我来讲解的是决策树。对于决策树来说,主要有两种算法:ID3算法和C4.5算法。C4.5算法是对ID3算法的改进。今天主要先讲ID3算法,之后会讲C4.5算法和随机森林等。 Contents 1. 决策树的转载 2017-09-08 15:58:46 · 924 阅读 · 0 评论 -
string的find操作
find//从pos开始查找字符c在当前字符串的位置int find(char c, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置int find(const char *s, int pos = 0) const;//从pos开始查找字符串s中前n个字符在当前串中的位置int find(const char *s, int pos, int n) con原创 2017-08-31 10:17:56 · 495 阅读 · 0 评论 -
求水的容积(锐明科技)
输入一个数组表示积木的高度,求其能容纳的水的容积是多少?输入:// 数组的长度n,以及每个元素的值nums[i]110 1 0 2 1 0 1 3 1 0 2输出:// 能容纳的水的容积8解题代码#include <iostream>#include <vector>#include <string>#include <algorithm>using namespace std;int原创 2017-10-10 19:31:09 · 696 阅读 · 0 评论 -
归并排序
排序算法(3)-归并排序原创 2017-07-15 11:04:33 · 218 阅读 · 0 评论 -
堆排序
一些概念普通队列:先进先出,后进先出(顺序已经确定) 优先队列:出队顺序与入队顺序无关,和优先级相关(动态确定顺序)优先队列在处理动态问题上很有优势,同时处理静态问题也是比较好的,比如在100000000个元素中选出前100名?也就是,在N个元素中选出前M个元素。不同方法的时间复杂度分别是排序:NlogNN \log N ,优先队列:NlogMN \log M。动态有新的元素会加入数据结构,同时又原创 2017-08-25 11:41:05 · 309 阅读 · 0 评论 -
利用查找表的顺序性
c++实现了lower_bound,即一个数的ceil操作,可以用来实现一个范围的查找问题,需要注意的是lower_bound(v),返回会的是查找数v的ceil的迭代器(指针),返回的这个ceil的值就是set中大于v的最小的那个数。void example(){ int v = 0; set<int> record; // 一个set // 在set中查找大于v的最小的值原创 2017-09-03 14:58:08 · 224 阅读 · 0 评论 -
217. Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is原创 2017-09-03 09:55:20 · 181 阅读 · 0 评论 -
49. Group Anagrams
Given an array of strings, group anagrams together.For example, given: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”], Return:[ ["ate", "eat","tea"], ["nat","tan"], ["bat"]]这道题使用哈希表,要灵活选择键值。解题代码ve原创 2017-09-03 09:34:13 · 182 阅读 · 0 评论 -
排序很重要
算法与数据结构中,有时候需要先排序在做其他事情,这里总结一些比较有趣的题目,说明排序在解题过程中的重要性。242. Valid Anagram [LeetCode] #include <iostream>#include <string>#include <algorithm>using namespace std;// 类似于华为的兄弟单词那个题bool isAnagram(string原创 2017-09-02 14:29:11 · 260 阅读 · 0 评论 -
排序+对撞指针(15. 3Sum )
以leetcode题目为例:Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.For example, given array S = [-1原创 2017-09-02 20:33:14 · 467 阅读 · 0 评论 -
查找表问题(c++)
分类查找有无 查找’a’是否存在?使用集合:set,unordered_set查找对应关系(键值对应) 元素’a’出现了几次?使用字典:map,unordered_mapset & map基本用法insertfinderasechange (map)举例说明(LeetCode)349. Intersection of Two Arrays求两个集合的交集元素,相同元素只记录一次。原创 2017-09-02 14:09:45 · 1213 阅读 · 0 评论 -
451. Sort Characters By Frequency
Given a string, sort it in decreasing order based on the frequency of characters. 【原题描述】例子1:Input:"tree"Output:"eert"Explanation:'e' appears twice while 'r' and 't' both appear once.So 'e' must app原创 2017-09-02 19:55:39 · 222 阅读 · 0 评论 -
219. Contains Duplicate II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k. 本原创 2017-09-02 13:10:54 · 214 阅读 · 0 评论 -
fork
引言一个进程,包括代码、数据和分配给进程的资源。fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不同,两个进程也可以做不同的事。一个进程调用fork()函数后,系统先给新的进程分配资源,例如存储数据和代码的空间。然后把原来的进程的所有值都复制到新的新进程中,只有少数值与原来的进程的值不同。相当于克隆了一个自己。#incl原创 2017-09-01 17:56:41 · 449 阅读 · 0 评论 -
字符串排序
题目描述 编写一个程序,将输入字符串中的字符按如下规则排序。规则 1 :英文字母从 A 到 Z 排列,不区分大小写。 如,输入: Type 输出: epTy规则 2 :同一个英文字母的大小写同时存在时,按照输入顺序排列。 如,输入: BabA 输出: aABb规则 3 :非英文字母的其它字符保持原来的位置。 如,输入: By?e 输出: Be?y原创 2017-09-01 12:04:17 · 397 阅读 · 0 评论 -
queue
queue用法empty() 判空,为空返回turesize() 返回队列的大小front() 队头元素back() 队尾元素测试#include <iostream> #include <queue> using namespace std;void printQueue(queue<int>& q){ cout << endl << "--------- Que原创 2017-09-01 00:36:15 · 202 阅读 · 0 评论 -
k均值聚类
什么是K均值聚类K-means算法是最简单的一种聚类算法。算法的目的是使各个样本与所在类均值的误差平方和达到最小(这也是评价K-means算法最后聚类效果的评价标准)一般步骤初始化 输入基因表达矩阵作为对象集X,输入指定聚类类数N,并在X中随机选取N个对象作为初始聚类中心。设定迭代中止条件,比如最大循环次数或者聚类中心收敛误差容限。进行迭代。根据相似度准则将数据对象分配到最接近的聚类中心,从而原创 2017-08-31 18:36:20 · 428 阅读 · 0 评论 -
91. Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total number of w原创 2017-08-21 14:07:39 · 185 阅读 · 0 评论 -
198. House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses原创 2017-08-21 11:35:40 · 241 阅读 · 0 评论 -
快速排序
排序算法(4)-快速排序原创 2017-07-15 23:22:28 · 525 阅读 · 0 评论 -
235. Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two原创 2017-08-18 17:40:34 · 272 阅读 · 0 评论 -
78. Subsets
Given a set of distinct integers, nums, return all possible subsets.Note: The solution set must not contain duplicate subsets.For example, If nums = [1,2,3], a solution is: [ [3], [1], [2], [1原创 2017-08-19 16:51:31 · 168 阅读 · 0 评论 -
216. Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.Example 1:Input: k = 3, n =原创 2017-08-19 16:20:15 · 170 阅读 · 0 评论