![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法
pynash123
这个作者很懒,什么都没留下…
展开
-
分支算法的时间复杂度
记录一下表示将原问题分解成 a 个 规模大小为 N/b 的子问题,合并这 a 个子问题的代价是 Θ(N^K) (N^k 表示 N 的 k 次方)T(N)=aT(N/b)+Θ(N^K)T(N)的解有以下三种情况:T(N)=O(N(logab)) 当 a > bk 时T(N)=O(Nk log^N) 当 a = bk 时T(N)=O(Nk) 当 a &l...原创 2019-12-16 18:17:01 · 2624 阅读 · 0 评论 -
leetcode work_break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, givens =“leetcode”,dict =[“leet”, “code...原创 2019-04-07 16:48:03 · 122 阅读 · 0 评论 -
scikit-learning k临近算法分类学习应用之肿瘤预测
k临近应用 : 肿瘤预测import matplotlib.pyplot as pltimport numpy as npimport pandas as pdfrom sklearn.model_selection import train_test_splitfrom sklearn.neighbors import KNeighborsClassifier, RadiusNeig...原创 2019-04-08 23:25:40 · 198 阅读 · 0 评论 -
滑动窗口的最大值
题目:给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,...原创 2019-04-09 14:33:35 · 71 阅读 · 0 评论 -
剑指offer:二叉搜索树的后序遍历序列
题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。思路:遍历sequence,直到遇到比最后一个值大的;此时sequence被最后一个值分为左右两个部分,若次数组是某二叉搜索树的后序遍历的结果,则右半部分(最后一个值除外)应该全部比最后一个值大,验证是否满足,不满足返回失败,否则递归验证左右两部分。...原创 2019-04-12 16:21:46 · 83 阅读 · 0 评论 -
scikit-learning 决策树用于泰坦尼克号生还数据
import matplotlib.pyplot as pltfrom sklearn.model_selection import train_test_splitimport numpy as npimport pandas as pdfrom sklearn.tree import DecisionTreeClassifierdef read_data(file_path): ...原创 2019-04-18 19:30:58 · 236 阅读 · 0 评论 -
剑指offer :翻转单词顺序列
题目:牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a student.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助他么?思路:方法1:借助一个栈...原创 2019-04-12 17:42:19 · 149 阅读 · 0 评论 -
剑指offer:字符串的排列
题目:输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。输入描述:输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。思路:按照字典序深度优先加回朔。注意:保存结果时先查看是否已经存在,因为有重复字符;也可以用一个set保存结果,直接插入,se...原创 2019-04-14 11:43:15 · 81 阅读 · 0 评论 -
scikit-learning 多项式回归应用房价预测
将sklearn.datasets中的load_boston房价数据用多项式回归进行训练,并画出学习曲线import matplotlib.pyplot as pltimport numpy as npfrom sklearn.datasets import load_bostonfrom sklearn.model_selection import train_test_splitfr...原创 2019-04-14 12:27:01 · 1055 阅读 · 0 评论 -
剑指offer 复杂链表的复制
题目:输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)思路:两次遍历,第一遍时复制链表(不管random指针的值),并用map<RandomListNode *, RandomListNode *> map_oldn...原创 2019-04-14 17:01:24 · 78 阅读 · 0 评论 -
scikit-learning 线性回归学习
import matplotlib.pyplot as pltimport numpy as npfrom sklearn.linear_model import LinearRegressionfrom sklearn.preprocessing import PolynomialFeaturesfrom sklearn.pipeline import Pipelinefrom skl...原创 2019-04-12 11:51:58 · 175 阅读 · 0 评论 -
剑指offer :最小的K个数
题目://输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。思路:采用优先级队列 默认大顶堆 其中元素个数超过k时,每次加入一个元素就删除一个头元素时间复杂度为O(nlog(k+1))class Solution {public: vector<int> GetLeastNumbers_So...原创 2019-04-08 19:57:36 · 121 阅读 · 0 评论 -
leetcode word-breakII
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.Return all such possible sentences.For example, givens =“catsandd...原创 2019-04-07 16:48:35 · 125 阅读 · 0 评论 -
leetcode Sort list
Sort a linked list in O(n log n) time using constant space complexity./**Definition for singly-linked list.struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}...原创 2019-04-07 16:49:02 · 75 阅读 · 0 评论 -
归并排序
归并排序class Solution {public: void mergesort(vector<int> &data) { } void merge1(vector<int> &data, int start, int end) { int ret = end ...原创 2019-04-07 16:49:23 · 80 阅读 · 0 评论 -
leetcode integer-to-roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.I = 1;V = 5X = 10L = 50C = 100D = 500M = 1000class Solution {public: string intT...原创 2019-04-07 16:49:50 · 83 阅读 · 0 评论 -
leetcode roman-to-integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.I = 1;V = 5X = 10L = 50C = 100D = 500M = 100class Solution {public: int romanToI...原创 2019-04-07 16:52:35 · 84 阅读 · 0 评论 -
leetcode Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with co...原创 2019-04-07 16:52:56 · 91 阅读 · 0 评论 -
leetcode remove-duplicates-from-sorted-array-ii
Follow up for “Remove Duplicates”:What if duplicates are allowed at most twice?For example,Given sorted array A =[1,1,1,2,2,3],Your function should return length =5, and A is now[1,1,2,2,3].//不是恒...原创 2019-04-07 16:53:18 · 124 阅读 · 0 评论 -
leetcode 4 sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note:Elements in a qua...原创 2019-04-07 16:53:50 · 112 阅读 · 0 评论 -
leetcode maximal-rectangle
题目 : maximal-rectangleGiven a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing all ones and return its area.class Solution {public:int maximalRectangle(vector&amp;amp;amp;amp;lt;vec...原创 2019-04-07 16:54:08 · 130 阅读 · 0 评论 -
scikit-learning k临近算法学习之k-d tree
k-d tree即k-dimensionaltree,常用来作空间划分及近邻搜索,是二叉空间划分树的一个特例。通常,对于维度为k,数据点数为N的数据集,k-d tree适用于N>>2^k的情形,kd树是基于欧式距离度量的。k-d树是每个节点都为k维点的二叉树。所有非叶子节点可以视作用一个超平面把空间分区成两个半空间。节点左边的子树代表在超平面左边的点,节点右边的子树代表在超平面右边的...原创 2019-04-11 20:01:19 · 413 阅读 · 0 评论 -
剑指offer:序列化二叉树
题目:请实现两个函数,分别用来序列化和反序列化二叉树思路:类似于层序遍历,以“,”分隔,以“#”代表空代码:class Solution {public: char* Serialize(TreeNode *root) { string strTmp; queue<TreeNode*> que; qu...原创 2019-05-06 14:35:24 · 85 阅读 · 0 评论 -
剑指offer:数据流中的中位数
题目:如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。我们使用Insert()方法读取数据流,使用GetMedian()方法获取当前读取数据的中位数。思路:1.用vector保存,get时先排序后取。插入复杂度O(1) ,get复杂度O(nlogn)2...原创 2019-04-18 19:05:32 · 98 阅读 · 0 评论 -
leetcode:是否是平衡二叉树
题目:输入一棵二叉树,判断该二叉树是否是平衡二叉树。思路1:首先算出每个子树的深度并保存,然后判断代码1:class Solution {public: bool IsBalanced_Solution(TreeNode* pRoot) { if(NULL == pRoot) { return true; ...原创 2019-07-15 09:58:27 · 100 阅读 · 0 评论 -
剑指offer:二叉树深度
题目:二叉树深度代码:class Solution {public: int TreeDepth(TreeNode* pRoot) { if(NULL == pRoot) { return 0; } int left = NULL != pRoot->left ? TreeDept...原创 2019-07-15 15:42:03 · 91 阅读 · 0 评论 -
剑指offer:合并两个排序的链表
题目:输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。代码:class Solution {public: ListNode* Merge(ListNode* pHead1, ListNode* pHead2) { if (NULL == pHead1) { return pHead2; } if...原创 2019-07-15 17:31:11 · 76 阅读 · 0 评论 -
leetcode n皇后问题
题目1:The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puzzle.Ea...原创 2019-07-12 13:47:58 · 100 阅读 · 0 评论 -
剑指offer:数组中的逆序对
剑指offer:题目:在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007思路:复用归并排序,在合并排序时稍加修改代码:class Solution {public: int InversePairs(vector<int...原创 2019-07-17 11:41:54 · 104 阅读 · 0 评论 -
剑指offer:两个链表的第一个公共结点
题目:输入两个链表,找出它们的第一个公共结点。思路:1、遍历一个链表并用map保存,然后再遍历另一个查找2、可以讲两个链表尾部相接,然后判断是否存在环,这个代码就不写了代码:/*struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { }};*/c...原创 2019-07-17 13:53:01 · 84 阅读 · 0 评论 -
剑指offer:数字在排序数组中出现次数
题目:统计数字在排序数组中出现的次数思路:二分查找找到位置,然后左右扩展代码:class Solution {public: int GetNumberOfK(vector<int> data ,int k) { if(data.empty() || data[0] > k || data[data.size()-1] < k) ...原创 2019-07-17 14:00:09 · 86 阅读 · 0 评论 -
不用加减乘数实现加减法
#include "iostream"using namespace std;int Add(int a, int b){ while(b) { int tmp = (a & b) << 1; a = a ^ b; b = tmp; } return a;}int Subtractio...原创 2019-07-27 11:15:01 · 170 阅读 · 0 评论 -
leetcode:链表中环的入口结点
题目://给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。/*struct ListNode {int val;struct ListNode *next;ListNode(int x) :val(x), next(NULL) {}};*/思路:快慢指针,快指针每次前进两个长度,慢指针每次前进一个长度,若存在环,则一定会在环内相遇。假设起点到环入...原创 2019-08-16 14:28:29 · 669 阅读 · 0 评论 -
剑指offer:把二叉树打印成多行
题目:从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。代码:class Solution {public: vector<vector<int> > Print(TreeNode* pRoot) { vector<vector<int> > res; if(NULL == pR...原创 2019-06-24 17:19:12 · 78 阅读 · 0 评论 -
剑指offer:二叉树的下一个结点
题目:给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针思路:若存在右孩子,直接在右孩子中寻找,若不存在右孩子,向上寻找第一个为左孩子的父节点代码:class Solution {public: TreeLinkNode* GetNext(TreeLinkNode* pNode) { ...原创 2019-06-21 17:32:40 · 65 阅读 · 0 评论 -
剑指Offer:数组中只出现一次的数字
题目:一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字思路:1、两次for循环,O(n2)2、哈希map,O(N),但是有额外空间3、从头到尾一次异或数组中的每一个数字,那最终结果就是两个只出现一次的数的异或。结果肯定不为0,在结果数组中找到第一个为1的位的位置,记为第n位。以第n位是不是1为标准把元数组中的数字分成两个子数组,第一个子数组中每个...原创 2019-06-21 14:37:36 · 89 阅读 · 0 评论 -
剑指offer:第一个只出现一次的字符
题目:在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).思路:map保存字符和index,set保存index,当字符出现超过两次,set删除该index,返回set中第一个代码:class Solution {public: int FirstNotRepeati...原创 2019-04-19 10:54:07 · 84 阅读 · 0 评论 -
剑指offer:把字符串转换成整数
题目:题目描述将一个字符串转换成一个整数(实现Integer.valueOf(string)的功能,但是string不符合数字要求时返回0),要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0。输入描述:输入一个字符串,包括数字字母符号,可以为空输出描述:如果是合法的数值表达则返回该数字,否则返回0代码:class Solution {public...原创 2019-04-19 11:33:25 · 98 阅读 · 0 评论 -
剑指offer:按之字形顺序打印二叉树
题目:请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。思路:和层次打印类似,只不过每次都将队列里面的元素取出,记录前层数,返回结果时当前层数的奇偶性判断返回正序还是逆序。代码:class Solution {public: vector<vector<int> &g...原创 2019-04-19 15:33:33 · 111 阅读 · 0 评论 -
剑指offer:丑数
题目:把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。思路:1.依次从零开始判断每个数是否是丑数,这样子复杂度过高2.利用丑数*2、3、5还是丑数,将当前结果保存在res中、维护index2 index3 index5 分别表示和2、3、5相乘的丑数对应...原创 2019-04-24 17:06:36 · 90 阅读 · 0 评论