![](https://img-blog.csdnimg.cn/20190927151117521.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
Algorithm
基本算法
哲米
深挖程序世界的各种疑难杂症
展开
-
算法:链表相关
题目把2个有序单调递增的链表 合并到一个链表倒序输出。要求不产生dummy node解法涉及链表的插入操作,要考虑当前的node 和前一个node#include <iostream>using namespace std;struct ListNode { int _data; ListNode* _next; ListNode(int data):_data(data) { _next = NULL; }};cl原创 2020-08-20 18:25:05 · 118 阅读 · 0 评论 -
算法 - CPU 大小端
为什么会有大小端模式之分在计算机系统中,以字节为单位寻址,每个地址单元都对应着一个字节,一个字节为8bit。但是在C语言中除了8bit的char之外,还有16bit的short型,32bit的int型。另外,对于位数大于8位的处理器,例如16位或者32位的处理器,由于寄存器宽度大于一个字节,那么必然存在着一个如果将多个字节安排的问题。因此就导致了大端存储模式和小端存储模式。例如一个16bit的short型x,在内存中的地址为0x0010,x的值为0x1122,那么0x11为高字节,0x22为低字原创 2020-08-07 11:46:29 · 143 阅读 · 0 评论 -
算法 - Ip转换
题目字符ip -> int int->ip目的主要训练字符串的处理解法#include <vector>#include <string>#include <iostream>using namespace std;int ip2int(string& str) { int start = 0; vector<int> ipdata; str.append("."); for...原创 2020-08-06 23:32:16 · 149 阅读 · 0 评论 -
算法 - 找颜色
题目1亿个球有1000种颜色,其中有一半的球是同一种颜色,采用一种方式能尽快的选出这个颜色。思路按照不同色的抵消思路,1亿个球放在A桶,B桶是个空桶,将A桶的球一个一个转移到B桶,如果放进桶里颜色的球不同,就清除该色的球,剩余的桶里的球就是这个颜色解法#include <iostream>#include <vector>using namespace std;int getColor(vector<int> boll) { if (原创 2020-08-06 23:50:33 · 278 阅读 · 0 评论 -
算法 - 单调栈
定义从栈底元素到栈顶元素呈单调递增或单调递减,栈内序列满足单调性的栈。适用问题单调栈分为单调递增栈和单调递减栈,通过使用单调栈我们可以访问到下一个比他大(小)的元素(或者说可以)。也就是说在队列或数组中,我们需要通过比较前后元素的大小关系来解决问题时我们通常使用单调栈。题目一数组中的下个比当前大的节点示例 1:输入:[2,1,5]输出:[5,5,0]示例 2:输入:[2,7,4,3,5]输出:[7,0,5,5,0]示例 3:输入:[1,7,5,1,9,2,5,1]原创 2020-07-28 20:45:58 · 755 阅读 · 0 评论 -
算法 - 动态规划
题目:给定两个数组,如下:low = [2, 3, 1, 7], high = [4, 6, 5, 8], 数组长度一样第i次只能从low[i] 和 hight[i] 里面取一个数。如果从low[i] 取,则第i+1次依然可以从low[i]取;如果第i次从hight[i]取,则第i+1次只能从low[i+1]中取,求最后一次取得的...原创 2020-07-23 23:50:21 · 122 阅读 · 0 评论 -
算法 - Top-K
题目从100w个数中找出前100个大的数思路取前m个元素(例如m=100),建立一个小顶堆。保持一个小顶堆得性质的步骤,运行时间为O(lgm);建立一个小顶堆运行时间为m*O(lgm)=O(m lgm);顺序读取后续元素,直到结束。每次读取一个元素,如果该元素比堆顶元素小,直接丢弃如果大于堆顶元素,则用该元素替换堆顶元素,然后保持最小堆性质。最坏情况是每次都需要替换掉堆顶的最小元素,因此需要维护堆的代价为(N-m)*O(lgm);最后这个堆中的元素就是前最大的10W个。时间复杂度原创 2020-07-14 23:10:56 · 206 阅读 · 0 评论 -
算法 - C字符串函数
// 字符串长度int strlen(const char *s){ int n; for (n = 0; *s != '\0'; s++) n++; return n;}// 字符串复制char *strcpy(char *dst, const char *src){ char *ret; ret = dst; while ((*dst++ = *src++) != '\0') /* do nothing ...原创 2020-07-14 22:22:01 · 131 阅读 · 0 评论 -
算法 - 求公共最大子串
最大公共子串有两个字符串,这两个字符串可能会存在公共的部分,如字符串"abcdef" 和字符串"defg",这两个字符串之间有共同的字符串,“d”,“e”,“f”,“de”,“ef”,“def” 等。最长的公共子串就是"def"。思路:LCS[i,j] = 0 (i<0||j<0) LCS(i-1,j-1) (i>=0 && b>0 && (str1[i]==str2[j]))解法:LCS (Longest Common Subsequ原创 2020-07-14 22:13:57 · 525 阅读 · 0 评论 -
算法 - 在两个有序数组中寻找第k大元素
题目:数组A,A的长度m,数组B,B的长度n,需要找第K大的数。要求:时间复杂度log(m+n)思路:确定第K大的数前面的K-1个数,同时要考虑极端情况解法:#include <iostream>#include <vector>using namespace std;int findTheKthElementInTwoSortedArray01(int A[], int m, int B[], int n, int kth){ if (kth原创 2020-07-13 18:03:26 · 1246 阅读 · 0 评论 -
算法 - 位运算类
格雷编码原创 2020-07-06 21:54:42 · 202 阅读 · 0 评论 -
算法 - 排列和组合【回溯法】
题目给定两个整数n和k。n表示从1到n的n个自然数,要求随机从这n个数中抽取k个的所有组合。示例Input: n = 4, k = 2Output:[[2,4],[3,4],[2,3],[1,2],[1,3],[1,4],]扩展给定一个没有重复元素的int型数组,要求返回所有的子集,要求子集当中没有重复项,每一项当中也没有重复的元素。...原创 2020-07-02 23:10:32 · 439 阅读 · 0 评论 -
算法 - 螺旋矩阵
题目:遍历一个螺旋数组Given a matrix ofmxnelements (mrows,ncolumns), return all elements of the matrix in spiral order.Example 1:Input:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]Output: [1,2,3,6,9,8,7,4,5]Example 2:Input:[ [1, 2, 3, 4], [5,...原创 2020-06-27 22:25:33 · 287 阅读 · 0 评论 -
算法 - 字符串相关
题目:单词匹配给定两个字符串S和T。要求设计一个复杂度为的算法,在S串当中找到一个子串,能够包含T串当中的所有字符。要求返回合法且长度最小的窗口的内容。示例Input: S = "ADOBECODEBANC", T = "ABC"Output: "BANC"思路要统计Target串中字母的个数,一要考虑target串的长度,二要考虑target串中每个字符出现的次数。遍历一遍target串,用map保存每个字符出现的次数;对于S中的每个遍历到的字母,都在 HashMap 中的映射值减1,原创 2020-06-27 22:01:49 · 328 阅读 · 0 评论 -
算法 - 链表移动k个元素
题目给定一个链表和一个整数K,要求将链表当中的所有元素向右移动K位。示例Input: 1->2->3->4->5->NULL, k = 2Output: 4->5->1->2->3->NULLExplanation:rotate 1 steps to the right: 5->1->2->3->4->NULLrotate 2 steps to the right: 4->5->原创 2020-06-27 19:16:13 · 1174 阅读 · 0 评论 -
算法 - 求路径和
题目给定一个矩形的迷宫,左上角有一个机器人,右下角是目的地。这个机器人只能向下走或者是向右走,请问这个机器人走到目的地的路径一共有多少种?矩形的长和宽都不超过100.示例Input: m = 3, n = 2Output: 3Explanation:From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:1. Right -> Right -> Dow原创 2020-06-27 17:02:35 · 528 阅读 · 1 评论 -
算法 - Regular Expression Matching
Given an input string (s) and a pattern (p), implement regular expression matching with support for'.'and'*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover theentireinput string (not...原创 2020-06-08 23:29:06 · 110 阅读 · 0 评论 -
算法 - 二叉树的序列化和反序列化
二叉树的序列化和反序列化原创 2020-06-07 11:19:41 · 135 阅读 · 1 评论 -
算法 - Plus one
Given anon-emptyarray of digitsrepresenting a non-negative integer, plus one to the integer.The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.You may assume th...原创 2020-06-04 00:04:25 · 138 阅读 · 0 评论 -
算法 - 在旋转有序数组中搜索
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e.,[0,1,2,4,5,6,7]might become[4,5,6,7,0,1,2]).You are given a target value to search. If found in the array return its index, otherwise return-1.You...原创 2020-06-01 23:16:26 · 166 阅读 · 0 评论 -
算法 - 回文数
Palindrome NumberDetermine whether an integer is a palindrome. An integerisapalindrome when itreads the same backward as forward.Example 1:Input: 121Output: trueExample 2:Input: -121Output: falseExplanation: From left to right, it reads...原创 2020-05-31 16:54:48 · 123 阅读 · 0 评论 -
算法 - 去除有序数组的重复元素
题目把一个有序数组里面的重复数字去掉,最终数组是无重复的有序数列,要求时间复杂度O(n), 空间复杂度O)(1)。思路对于数组来说,在尾部插入、删除元素是比较高效的,时间复杂度是 O(1),但是如果在中间或者开头插入、删除元素,就会涉及数据的搬移,时间复杂度为 O(N),效率较低。对于数组相关的算法问题,有一个通用的技巧:要尽量避免在中间删除元素,那我就想先办法把这个元素换到最后去。这样的话,最终待删除的元素都拖在数组尾部,一个一个 pop 掉就行了,每次操作的时间复杂度也就降低到 O(1)原创 2020-05-24 22:15:08 · 492 阅读 · 0 评论 -
算法 - 判定括号合法性
题目输入一个字符串,其中包含[](){}六种括号,请你判断这个字符串组成的括号是否合法。示例Input: "()[]{}"Output: trueInput: "([)]"Output: falseInput: "{[]}"Output: true思路:使用栈先进后出的特征在栈顶匹配解法:#include <string>#include <stack>#include <iostream>using namespa..原创 2020-05-24 22:04:14 · 807 阅读 · 0 评论 -
算法 - 滑动窗口最大值
问题给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口 k 内的数字。滑动窗口每次只向右移动一位。返回滑动窗口最大值。原创 2020-05-16 20:26:45 · 5068 阅读 · 1 评论 -
算法:收集雨水
问题Givennnon-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of..原创 2020-05-15 10:02:14 · 499 阅读 · 0 评论 -
算法 - 盛最多水的容器
问题给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点(i,ai) 。在坐标内画 n 条垂直线,垂直线 i的两个端点分别为(i,ai) 和 (i, 0)。找出其中的两条线,使得它们与x轴共同构成的容器可以容纳最多的水。说明:你不能倾斜容器,且n的值至少为 2。算法:#include <iostream>#include <vector>using namespace std;#define min(x,y) ((...原创 2020-05-14 23:00:53 · 106 阅读 · 0 评论 -
算法 - skiplist
简介SkipList(跳表)这种数据结构是由William Pugh于1990年在在Communications of the ACMJune 1990, 33(6) 668-676 发表了Skip lists: a probabilistic alternative to balanced trees,在其中详细描述了他的工作。由论文标题可知,SkipList的设计初衷是作为替换平衡树的...转载 2020-05-07 13:17:59 · 375 阅读 · 0 评论 -
算法 - 按规律输出小于N的序列
题目给定一个自然数,按照如下序列格式输出。输入: N输出:0110100101...10911110...11912120...12913130..9909192939495969798解法:int dump(long long n) { long long total = 0; long long ...原创 2020-04-25 23:06:26 · 349 阅读 · 0 评论 -
算法 - 最小交换次数
问题描述:给出一个具有N个不同值的数组A[],找出将数组排序所需的最小交换次数。示例:数组A[]: 1 4 6 3 7 9 5 2解题思路:原创 2020-04-25 22:58:53 · 2456 阅读 · 0 评论 -
算法 - 线程池
问题描述:实现一个简单的线程池代码实现:#include <pthread.h>#include <unistd.h>#include <deque>#include <iostream>#include <vector>#include <errno.h>#include <string.h&...原创 2020-02-05 09:06:33 · 147 阅读 · 0 评论 -
算法 - 012
问题描述:给定任意长度的一个0、1、2序列,要求排序成0在左边,1在中间,2在右边的有序序列,时间复杂度为O(n),空间复杂度为O(1).解决思路:1. 定义2个标志位i0、i2,i0是数组头,i2是数组尾;2. 从左向右遍历一遍数组,循环变量为i,如果a[i]是0, 则a[i0]和a[i]交换,i0++;如果a[i]是2,则a[i2]和a[i]交换,i2--;否则循环变量i+...原创 2020-02-05 08:56:49 · 281 阅读 · 0 评论 -
算法 - 迷宫问题
Open the lockYou have a lock in front of you with 4 circular wheels. Each wheel has 10 slots:'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. The wheels can rotate freely and wrap around: for exam...原创 2020-05-31 16:25:26 · 186 阅读 · 0 评论 -
算法 - Random Flip Matrix
Random Flip MatrixYou are given the number of rowsn_rowsand number of columnsn_colsof a2Dbinary matrixwhere all values are initially 0.Write a functionflipwhich choosesa 0 valueuniforml...原创 2019-11-01 13:41:49 · 317 阅读 · 0 评论 -
算法 - Partition Labels
Partition LabelsA string S of lowercase letters is given. We want to partition this string into as many parts as possible so that each letter appears in at most one part, and return a list of intege...原创 2019-10-30 12:02:16 · 185 阅读 · 0 评论 -
算法 - Tree
树的相关算法 1 / \ 2 3 / \ \ 9 10 4 \ / / \ 12 11 5 6 / \ \ / \ 13 14 15 7 81. 根据先序和中序构建二叉树主要...原创 2019-10-24 19:49:38 · 450 阅读 · 0 评论 -
算法 - LRU
LRUDesign and implement a data structure forLeast Recently Used (LRU) cache. It should support the following operations:getandput.get(key)- Get the value (will always be positive) of the key ...原创 2019-10-16 19:15:56 · 100 阅读 · 0 评论