![](https://img-blog.csdnimg.cn/20201014180756757.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法练习
虚竹777777
一粟,不粟
展开
-
多次求和构造目标数组
给你一个整数数组 target 。一开始,你有一个数组 A ,它的所有元素均为 1 ,你可以执行以下操作: 1、令 x 为你数组里所有元素的和 2、 选择满足 0 <= i < target.size 的任意下标 i ,并让 A 数组里下标为 i 处的值为 x 。 你可以重复该过程任意次如果能从 A 开始构造出目标数组 target ,请你返回 True ...原创 2020-02-19 11:45:06 · 195 阅读 · 0 评论 -
分裂二叉树的最大乘积-力扣周赛题
题目描述:给你一棵二叉树,它的根为 root 。请你删除 1 条边,使二叉树分裂成两棵子树,且它们子树和的乘积尽可能大。由于答案可能会很大,请你将结果对 10^9 + 7 取模后再返回题解:在题目里,将节点分为两组,但是这两组就是所有元素之和,是一定的,这会让我们想起来均值不等式当且仅当a=b的时候取等号。所以在本题中,ab的乘积是有上限的,我们应该使得a和b尽量的接...原创 2020-02-02 20:05:50 · 478 阅读 · 0 评论 -
搜素二叉树的权值为某值的路径+二叉树变双向链表
搜素二叉树的权值为某值的路径void findPath(node *root,int expect,int current,vector<int> &path){ if(root!=NULL) { current += root->val; path.push_back(root->val); ...原创 2018-11-06 23:33:06 · 103 阅读 · 0 评论 -
栈的压入和弹出序列是否正确
a为压栈序列,b为出栈序列bool check(int *a,int *b,int n){ stack<int> int_stack; bool isTrue = false; int * start = a; int top = 0; if(a!=NULL && b!=NULL && n>0) ...原创 2018-11-02 23:40:36 · 91 阅读 · 0 评论 -
剑指offer--复杂链表复制
O(n)复杂度struct node{ int val; node* next; node* sibling;};void cloneNodes(node *head){ node *tmp = head; while(tmp!=NULL) { node *cloneNode = new node; cl...原创 2018-11-07 16:28:40 · 149 阅读 · 0 评论 -
排列问题
全排列void permutation(char *str,char *Begin){ if((*Begin)=='\0') cout<<str<<endl; else { for(char *tmp = Begin;(*tmp)!='\0';tmp++) { char t = *Beg...原创 2018-11-07 23:17:05 · 144 阅读 · 0 评论 -
计算1到n中1的个数
方法一:时间复杂度高#include <iostream>#include <cstring>#include<vector>#include<queue>#include <math.h>using namespace std;int calculate(int n){ int num = 0; w...原创 2018-11-16 23:13:44 · 435 阅读 · 0 评论 -
数组中出现次数超过一半的数字
int partition(int a[],int start,int endd){ int small = start -1; for(int i=start;i<=endd-1;i++) { if(a[i]<a[endd]) { small++; if(a[i]!=a[sma...原创 2018-11-19 16:38:11 · 141 阅读 · 0 评论 -
判断一个序列是不是二叉搜索树的后序遍历序列
#include <iostream>#include <cstring>#include <queue>using namespace std;bool check(int a[],int start,int last){ int root = a[last]; int i; for(i=start;i<last;i+...原创 2018-11-03 10:02:05 · 209 阅读 · 0 评论 -
输出倒数第k个节点+反转链表+合并两个有序链表
要求:遍历链表一边#include <iostream>#include <cstring>using namespace std;struct node{ int val; node * next;};node * findNode(node *head,int k){ if(head==NULL ||k<=0) return ...原创 2018-10-26 22:42:11 · 163 阅读 · 0 评论 -
调整数组顺序使奇数位于偶数前面
bool isEvent(int n){ return ((n&0x1) ==1);}void adjust(int a[],int n,bool (*fun)(int)){ int mid = -1; int index; for(index=0;index<n;index++) { if(fun(a[index])...原创 2018-10-26 21:33:38 · 150 阅读 · 0 评论 -
旋转数组最小的数字
问题描述:将一个递增数组最开始若干个元素搬到数组的末尾,输出最小的元素int findMinIndex(int a[],int start,int endindex){ int target = start; for(int i=start+1;i<=endindex;i++) { if(a[i]<a[i-1]) { ...原创 2018-10-20 21:04:04 · 101 阅读 · 0 评论 -
二进制中1的个数
1、循环32次的int numofone(int a){ unsigned int flag = 1; int count=0; while(flag) { if(flag&a) count++; flag = flag <<1; } return count;}2、效率提高得i...原创 2018-10-20 22:25:02 · 99 阅读 · 0 评论 -
数值的整数次方
需要考虑 的情况1、0的0次方2、0的负数次方3、效率问题bool isEqual(double num1,double num2){ if((num1-num2>-0.00001)&&(num1-num2<0.00001))return true; else return false;}double withUnsignedEx...原创 2018-10-25 19:39:26 · 108 阅读 · 0 评论 -
二叉树的镜像+广度遍历
#include <iostream>#include <cstring>using namespace std;struct node{ int val; node* left; node* right;};void createTree(node *&root){ int n; cin>>n; ...原创 2018-10-28 23:20:26 · 152 阅读 · 0 评论 -
斐波那契--矩阵快速幂
void multify(int a[][2],int b[][2]){ int c[2][2]; for(int i=0;i<2;i++) for(int j=0;j<2;j++) c[i][j] = 0; for(int i=0;i<2;i++) for(int j=0;j<2;j++)...原创 2018-10-21 16:37:45 · 146 阅读 · 0 评论 -
螺旋矩阵
#include <iostream>#include <cstring>using namespace std;int visitAll(int a[][100],int start,int rows,int col,int num){ int endX = rows - start -1;; int endY = col -start -1;...原创 2018-10-29 09:20:15 · 103 阅读 · 0 评论 -
实现包含min函数的栈,使得min时间复杂度为O(1)
#include <iostream>#include <cstring>#include <stack>using namespace std;stack<int> a_stack;stack<int> min_stack;void push(int n){ a_stack.push(n); if(min...原创 2018-10-29 09:37:11 · 209 阅读 · 0 评论 -
打印1到最大的n位数
解法一:全排列过程void printnum(char number[],int n){ bool begining = true; int num = 0; for(int i=0;i<n;i++) { if(begining && number[i]!='0') begining = false; e...原创 2018-10-26 10:13:44 · 133 阅读 · 0 评论 -
快排--不一样的partition函数
#include <iostream>using namespace std;int partitions(int a[],int start,int endindex){ if(start>=endindex) return -1; int small = start-1;//small是所有比目标数字都小的最大索引 int index; ...原创 2018-10-19 21:02:07 · 257 阅读 · 0 评论