
算法练习
文章平均质量分 53
icebearzzz
学生
展开
-
2021-10-13高精度开n次方
具体原理:开平方#include <iostream>#include <vector>using namespace std;/** @brief 辅助函数,二分法求n次幂 @param x 底数 @param n 指数 @return 结果 */int getPow(int x, int n) { if (n == 1) { return x; } else if (n % 2) { int t = .原创 2021-10-13 20:04:50 · 314 阅读 · 0 评论 -
C++高精度开平方
#include <iostream>#include <vector>using namespace std;/* 主要思路:(x + 1)^2 = x^2 + 2x + 1 同样的的道理 (x + 10^i) = x^2 + 2*(10^i)*x + 10^(2i) 于是可以循环每次增加0.1,直到超界,确定第一个小数位 每次增加0.01,,确定第二个小数位 每次增加10^(-n),确定第n位小数 *//** * @brief 求一个数开方的正数部分原创 2021-10-13 13:43:37 · 1771 阅读 · 0 评论 -
【左神算法】基础班第一课(一)——对数机和有序数组中找一个无序数组元素
问题:在找出长度为m的无序数组b不存在于长度为n的有序数组a中的元素。a = [1, 2, 3, 4, 5] b= [1, 9, 3, 7, 5]输出 [9, 7]方法一:穷举 m*n方法二:在a中二分查找,m*logn方法三:b排序, 求差集 m*longm具体用方法二还是方法三,看数组长度。同时介绍对数器:在笔试中有一个保证正确且容易实现的方法ri...原创 2019-07-18 16:39:30 · 674 阅读 · 0 评论 -
动态规划的空间优化, 以最长公共子序列为例
最简单的直接按照递推式去写#include <stdio.h> #include <string.h> char a[1001], b[1001]; int tag[1001][1001]; #define max(a, b) a > b ? a : bint f(int i, int j) { if (i < 0 || j &l...原创 2018-11-07 15:15:01 · 1349 阅读 · 0 评论 -
八种排序的简单实现
冒泡排序及优化,参考匈牙利冒泡排序舞蹈//冒泡递归写法void f0(int a[], int len){ if (len > 1) { int Loop = 0; for (int i = 0; i < len - 1; i++) { Loop++; if (a[...原创 2018-10-19 09:50:10 · 174 阅读 · 0 评论 -
日期大小比较,不用if,while, switch、for、三目运算符等
#include <iostream>using namespace std;struct DATE{ int year, month, day; //三个变量的内存地址是连续的 DATE(int year, int month, int day) : year(year), month(month), day(day){}};int ...原创 2018-07-06 17:14:18 · 568 阅读 · 0 评论 -
利用栈和后缀表达式计算后缀表达式
原理不赘述,随便找个博客看看后缀表达式即其计算原理简单,实现起来有几个小问题:Q1:A+B*C的后缀变表达式为ABC*+,当ABC为具体的1、2、3时,后缀表达式为123*+, 123怎么理解,1和2和3还是12和3还是1和23...A1:用||间隔数字,如|1||2||3|*+就很明确了。Q2:输入是字符串,计算要int/doubleA2:所以需要自己写一个一个字符串转换为i...原创 2018-09-30 12:06:37 · 1259 阅读 · 0 评论 -
非递归实现树的前中后遍历
代码衔接BST树的代码,可以放在一起运行测试非递归先序:先根遍历的基本思想就是:输出自己,输出左子树,输出右子树。从根节点出发,向左依次输出并进栈,当左孩子为空时,指针指向栈顶的右子树, 然后出栈,对右子树进行遍历。void pre_output_stack(node* root){ stack<node*> s; node* p = root; ...原创 2018-10-03 18:33:42 · 139 阅读 · 0 评论 -
BST树的基本实现
基本操作:创建,销毁,清空,增删改查,先中后层遍历。都很简单,就是删除一个节点要分类讨论。在一个二叉搜索树中,一个基本性质,左子树的所有节点小于根节点,右子树的所有节点大于等于根节点,依据这个性质讨论删除操作。当被删除元素左子树为空、右子树为空,或者都为空,直接用左子树或者右子树替代。如删除15,设*root指向15这个节点 node* t = root;root = ...原创 2018-10-03 15:23:47 · 4506 阅读 · 2 评论 -
知先根、中根求后根
原理和上篇文章相似,不赘述#include <iostream>#include <cstring>using namespace std;void f(char s1[], char s2[], int len){ if (len == 1) { cout << s1[0]; } else if...原创 2018-10-03 15:04:29 · 584 阅读 · 0 评论 -
知后根、中根,求先根
设s1为树的后根遍历,s2为树的中根遍历,则s1[len - 1]为先根遍历的第一个节点,且s2中,s1[len - 1]左边的字符为s1[len - 1]的左子树,右边即是右子树。例:中根:DBEAFC 后根:DEBFCA此时A是树的根节点,根节点的左子树的中根遍历是BDE,右子树的中根遍历是FC,左子树的后根DEB,右子树的后根FC子树也是树,递归。#incl...原创 2018-10-03 15:00:10 · 693 阅读 · 1 评论 -
树状数组练习 POJ-2299 Ultra-QuickSort
题目链接求逆序数不能简单的构造一个长度为999999999的树状数组,内存爆炸,要转换为id的逆序数,长度50000,可以接受#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#defi...原创 2018-09-14 19:09:22 · 142 阅读 · 0 评论 -
2020年4-10日华为笔试第二题
给定一个字符串,如abc3(A),表示括号内的A输出3次,结果逆向输出,即AAAcba如3(A)3(F) 输出FFFAAA如ab2(cd3(ef)2gh)) 输出hghgfefefedchghgfefefedcba题目保证数字后面一定是括号,括号一定配对。#include <iostream>using namespace std;string s;int...原创 2019-04-10 22:03:03 · 726 阅读 · 1 评论 -
笔试题1
第一题有这样一道智力题:“某商店规定:三个空汽水瓶可以换一瓶汽水。小张手上有十个空汽水瓶,她最多可以换多少瓶汽水喝?”答案是5瓶,方法如下:先用9个空瓶子换3瓶汽水,喝掉3瓶满的,喝完以后4个空瓶子,用3个再换一瓶,喝掉这瓶满的,这时候剩2个空瓶子。然后你让老板先借给你一瓶汽水,喝掉这瓶满的,喝完以后用3个空瓶子换一瓶满的还给老板。如果小张手上有n个空汽水瓶,最多可以换多少瓶汽水喝?#i...原创 2019-04-02 23:56:53 · 603 阅读 · 0 评论 -
笔试题2
#include <iostream>using namespace std;int a[605][605];int dp[605][605];int M, N, bx, by, ex, ey;int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};int isOK(int x, int y){ return x >= ...原创 2019-04-16 19:40:15 · 142 阅读 · 0 评论 -
【左神算法】基础班第三课(二)——矩阵打印
顺时针顺序打印记录圈数, 然后一圈一圈的打印。注意最后一圈只有一行或者一列的情况#include <iostream>#include <algorithm>#include <vector>#include <set>#include <unordered_map>#include <list>usin...原创 2019-08-05 15:53:40 · 160 阅读 · 0 评论 -
【左神算法】基础班第三课——两个栈实现队列,两个队列实现栈,最小值栈
两个栈实现队列基本要求:入队元素全部往push_stack里加出队元素全部从pop_stack里出,如果pop_stack里没有元素,push_stack所有元素压入pop_stack。#include <iostream>#include <vector>#include <stack>using namespace std;...原创 2019-07-26 13:50:06 · 201 阅读 · 0 评论 -
【左神算法】基础班第三课——无序数组中排序后相邻两数最大差值
题意输入:[2, 0, 9, 3, 5]输出:4因为输入排序后, 数组为【0, 2, 3, 5, 9】,相邻两数的最大差值为9 - 5 = 4。思路:简单办法, 排序+遍历=nlogn优化办法:设有n个数,则建n+1个桶,第i个桶的数据范围为[min+ per_gap * (i - 1), min + per_gap * i), per_gap = (mx - ...原创 2019-07-26 13:13:44 · 313 阅读 · 0 评论 -
【算法练习】剑指offer——有序矩阵查找
思路一:通过mid_i, mid_j, 将矩阵划分为4份,矩阵划分为4份。根据大小情况丢弃左上或者右下角。时间复杂度log(m*n) / log(4/3), m, n为矩阵长宽#include <iostream>#include <vector>using namespace std;struct point{ int i, j; point...原创 2019-07-30 19:22:51 · 318 阅读 · 0 评论 -
链表逆置,递归实现
用递归遍历到最后一个元素,从最后一个元素开始操作。设P逆置链表的尾指针,在尾指针后面不断加入新的元素即可。#include <iostream>using namespace std;struct node{ int x; node *next; node(int x = 0, node *next = NULL) : x(x), next(nex...原创 2019-07-25 15:44:10 · 729 阅读 · 0 评论 -
【算法练习】含重复字符的全排列
思路:不重复字符的字符串,对字符进行遍历, 回溯解决。结果又N!种情况含有重复的字符串,普通做法N!种情况肯定不对。比如aa的全排列只有一种。所以此时不能对字符串进行遍历,而是字符的词频进行遍历,如果词频字符的词频大于0, 就可以用。#include <iostream>#include <vector>#include <string>...原创 2019-07-30 14:47:07 · 979 阅读 · 0 评论 -
【左神算法】基础班第二课(三)堆排序及对结构的应用
堆排序#include <iostream>#include <vector>using namespace std;void heapInsert(vector<int> &a, int value){ a.push_back(value); int index = (int)a.size() - 1; whil...原创 2019-07-19 15:31:36 · 275 阅读 · 0 评论 -
【左神算法】基础班第二课(二)——快速排序
传统快排,一次把一个元素放到正确的位置,然后递归#include <iostream>#include <vector>using namespace std;int Partition(int a[], int l, int r){ int key = a[r]; while (l < r) { wh...原创 2019-07-19 12:13:06 · 362 阅读 · 0 评论 -
【左神算法】基础班第二课(一)——荷兰国旗
问题:荷兰国旗问题,给定一个数组,让小于0的在左边,大于0的在右边,0在中间如输入 [1, -2, 3, 0, -3, 0]输出: [-2, -3, 0, 0, 3, 1]思路:设置3个游标l = 0, r = n - 1, current = 0;l的左边小于0,r的右边大于0,中间为0三种情况a[current] < 0时, swap(a[current++]...原创 2019-07-19 10:50:33 · 225 阅读 · 0 评论 -
【左神算法】基础班第一课(二)——归并解决逆序数
问题:小和或者逆序数问题求数组中,左数小于右数的和。输入[1 2 3 4 5]输出202的位置 res += 1 res=13的位置 res += 1 + 2 res = 44的位置res += 1 + 2 + 3 res = 105的位置res += 1 + 2 + 3 + 4 res = 20归并代码#include <iostream>...原创 2019-07-18 17:40:22 · 336 阅读 · 0 评论 -
树状数组练习 POJ-1990
题目链接 求一个点到另外点的距离不用dis[i]-dis[j]可以先求出其他点的个,其他点的个数 ,dis[i] * num - sum即为所求用两个数组数来维护num和sum#include <iostream>#include <algorithm>#include <cstdio>using namespace std;#...原创 2018-09-13 18:28:06 · 133 阅读 · 0 评论 -
树状数组练习 POJ-2481
题目链接 #include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;struct node{ int l, r; int id; friend bool operator &...原创 2018-09-13 15:37:40 · 168 阅读 · 0 评论 -
树状数组练习 POJ-2352 Stars
题目链接#include <stdio.h>#include <string.h>#define N 32010int t[N];int ans[N];int lowbit(int x){ return x & (-x);}int get_sum(int k){ int sum = 0; while (k ...原创 2018-09-13 14:46:08 · 175 阅读 · 0 评论 -
LeetCode-6. ZigZag Conversion
题目连接暴力O(n^2)找规律O(n)class Solution {public: string convert(string s, int numRows) { if (numRows == 1) return s; string res = ""; int all_step = (numRows -...原创 2018-06-20 14:51:24 · 110 阅读 · 0 评论 -
LeetCode-14 Longest Common Prefix
题目链接分析:以第一个字符串为基准,取其每个字符与后续字符串相应位置的字符比较,只要出现不同,公共前缀到此为止C++class Solution {public: string longestCommonPrefix(vector<string>& strs) { if (strs.size() == 0) return ""; if ...原创 2018-06-26 19:10:50 · 150 阅读 · 0 评论 -
LeetCode-13 Roman to Integer
题目链接分析:当I出现在V前,即-1 + 5 = 4所以当字符x代表的数字小于其后字符y代表的数字时,x所代表的数字取负C++class Solution {public: int romanToInt(string s) { map<char, int> mp; mp['I'] = 1; mp['V'] = 5; ...原创 2018-06-26 19:00:03 · 127 阅读 · 0 评论 -
LeetCode-12 Integer to Roman
题目链接找规律先表示个位,分两种情况,大于5,小于5,大于5时特殊处理9, 小于5时特殊处理4在表示十位,分两种情况,大于50,小于50,大于50特殊处理90, 小于50特殊处理40...C++class Solution {public: string intToRoman(int num) { map<int, string> mp; mp...原创 2018-06-26 14:23:38 · 131 阅读 · 0 评论 -
LeetCode-11 Container With Most Water
题目链接贪心策略class Solution {public: int maxArea(vector<int>& height) { int i = 0, j = height.size() - 1; int max_area = -1; while (i < j) { ...原创 2018-06-26 13:52:19 · 121 阅读 · 0 评论 -
LeetCode-5. Longest Palindromic Substring
点击打开链接解析:最长公共子串的变形,设有s、rs串,最长公共子串f(i,j)上述公式是求最长公共子串的公式如果要公共子串是回文串,设rs串是s串的reverse,还需要再加一个判定,即rs中的子串和s的子串位置一致如 s = "abcdefdcba" 则 rs = "abcdfedcba",虽然s和rs最长公共子串是"abcd",但是不回文,因为rs中的"abcd"是由s中的"dcba"得到。...原创 2018-06-17 11:51:35 · 181 阅读 · 0 评论 -
LeetCode-3 Longest Substring Without Repeating Characters
题目链接题目分析:暴力破解,O(n^3)动态规划 dp[i] = i - lastest_repeat(i) 即当前位置减去在此之前最后重复位置 leatest_repeat(i) = max({index}) {index}指重复出现的重复的下标理解版O(3n)#include <iostream>#include <opencv2/opencv.h...原创 2018-06-16 13:19:21 · 153 阅读 · 0 评论 -
LeetCode-9 Palindrome Number
题目链接C++class Solution {public: bool isPalindrome(int x) { char s[11]; sprintf(s, "%d", x); int len = strlen(s); int mid = len / 2; for (i...原创 2018-06-23 00:28:14 · 146 阅读 · 0 评论 -
LeetCode-8 String to Integer (atoi)
题目链接Pythonclass Solution(object): def myAtoi(self, str): """ :type str: str :rtype: int """ str = str.strip() _sum = 0 if len(...原创 2018-06-23 00:14:15 · 167 阅读 · 0 评论 -
LeetCode-2 Add Two Numbers
题目链接Python# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def addTwoNumbers(s...原创 2018-06-15 02:04:35 · 186 阅读 · 0 评论 -
LeetCode-1 Two Sum
问题链接C++class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { int ok = 0; vector<int> res; for (in...原创 2018-06-14 16:58:27 · 156 阅读 · 0 评论 -
LeetCode-7 Reverse Integer
题目链接关键就是int的边界处理,用C++写麻烦,Python支持大数class Solution(object): def reverse(self, x): """ :type x: int :rtype: int """ MAX = 2**31 - 1 MIN = -2**31 ...原创 2018-06-20 17:23:27 · 92 阅读 · 0 评论