文章目录
入门
001 - 斐波那契数列(数组)
class Solution {
public:
int Fibonacci(int n) {
if(n==0)
return 0;
if(n==1 || n==2)
return 1;
else{
int i=1,j=1,sum;
for(int k=3;k<=n;k++){
sum=i+j;
i=j;
j=sum;
}
return sum;
}
}
};
简单
002-二叉搜索树的第k个结点(树)
中序遍历二叉树(这边使用了递归的方法)
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
class Solution {
public:
int index = 0;
TreeNode* KthNode(TreeNode* pRoot, int k)
{
if(pRoot!=NULL){
TreeNode* node = KthNode(pRoot->left,k);
if(node!=NULL)
return node;
index++;
if(index==k)
return pRoot;
node = KthNode(pRoot->right,k);
if(node!=NULL)
return node;
}
return NULL;
}
};
003-构建乘积数组(数组)
比较简单,复习一下vector容器用法。
class Solution {
public:
vector<int> multiply(const vector<int>& A) {
vector<int> B;
for(int i=0;i<(int)A.size();i++)
B.push_back(1);
for(int i=0; i<(int)A.size();i++){
for(int j=0;j<(int)A.size();j++){
if(i!=j)
B[i] *= A[j];
}
}
return B;
}
};
004-不用加减乘除做加法(数学)
https://www.jianshu.com/p/3b0a5033261a 这个说得很清楚
class Solution {
public:
int Add(int num1, int num2)
{
int sum;
int carry;
do
{
sum=num1^num2;
carry=(num1&num2)<<1;
num1=sum;
num2=carry;
}
while(num2!=0);
return num1;
}
};
005-二叉树的深度(树)
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
int TreeDepth(TreeNode* pRoot)
{
if(pRoot==NULL)
return 0;
int l = TreeDepth(pRoot->left);
int r = TreeDepth(pRoot->right);
return l>r?l+1:r+1;
}
};
006-平衡二叉树(树、dfs)
直接用了上一题的结果,但一开始忘了怎么从子树开始判断了。
class Solution {
public:
int TreeDepth(TreeNode* pRoot)
{
if(pRoot==NULL)
return 0;
int l = TreeDepth(pRoot->left);
int r = TreeDepth(pRoot->right);
return l>r?l+1:r+1;
}
bool IsBalanced_Solution(TreeNode* pRoot) {
if(pRoot==NULL) return true;
int l = TreeDepth(pRoot->left);
int r = TreeDepth(pRoot->right);
if(l-r>1 || r-l>1)
return false;
return IsBalanced_Solution(pRoot->left)&&IsBalanced_Solution(pRoot->right);
}
};
007-第一个只出现一次的字符(字符串)
明明很简单的一题但是踩了很多次坑,一个是忘了三次以上出现的情况处理,第二个是一开始以为只有a-z,还有大写字母没有考虑。
class Solution {
public:
int FirstNotRepeatingChar(string str) {
int words[58]={0};
for(int i=0;i<str.length();i++){
words[(int)str[i]-65] += 1;
}
for(int i=0;i<str.length();i++){
if(words[(int)str[i]-65]==1)
return i;
}
return -1;
}
};
008-连续子数组的最大和(动态规划、分治)
这个博写的很清楚 https://blog.csdn.net/m0_37925202/article/details/80816684
动态规划,将当前项与累积和比较,若当前项大于累积和,则舍弃累积和,从当前项继续累积和。
class Solution {
public:
int GetMax(int a, int b){
return (a)>(b)?(a):(b);
}
int FindGreatestSumOfSubArray(vector<int> array) {
if(array.size()==0)
return 0;
int sum = array[0];
int max = array[0];
for(int i=1;i<array.size();i++){
sum = GetMax(array[i], sum+array[i]);
if(sum > max)
max=sum;
}
return max;
}
};
009-数组中出现次数超过一半的数字(数组、哈希)
设置一个计数器,并记录当前数字,计数器设为1;
将下一个数字与当前数字比较,如果相等,计数器加1,如果不等计数器减1;
当计数器为0时,重新记录当前数字并设计数器为1;
如果有数字超过数组长度一半,那么最后的当前数字一定是该数字;
但有可能出现[1,2,3,2,4,2,5,2,3]这种情况,这样最后的数字是3,计数器为1但3只出现了一次;
因此还需要再次遍历计算当前数字出现次数,若出现次数超过数组一般则返回;否则返回0;
其他方法见 https://www.jianshu.com/p/14e003748ca2
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers) {
int cur=numbers[0];
int count = 1;
for(int i=1;i<numbers.size();i++){
if(numbers[i]==cur)
count++;
else
count--;
if(count==0){
cur = numbers[i];
count = 1;
}
}
if(count>0){
count = 0;
for(int i=0;i<numbers.size();i++){
if(numbers[i]==cur)
count++;
}
if(count>numbers.size()/2)
return cur;
else
return 0;
}
else return 0;
}
};
010-二叉树的镜像(树)
还比较简单,但一开始把if想成while了
class Solution {
public:
void Mirror(TreeNode *pRoot) {
if(pRoot==NULL)
return;
TreeNode *temp;
temp = pRoot->left;
pRoot->left = pRoot->right;
pRoot->right = temp;
Mirror(pRoot->left);
Mirror(pRoot->right);
}
};
011-合并两个排序的链表(链表)
归并,需要一个头指针和一个移动指针。
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
{
if(pHead1==NULL)
return pHead2;
if(pHead2==NULL)
return pHead1;
ListNode* head = NULL;
ListNode* cur=NULL;
while(pHead1!=NULL&&pHead2!=NULL){
if(pHead1->val<pHead2->val){
if(head==NULL){
cur = pHead1;
head = cur;
}
else{
cur->next = pHead1;
cur = cur->next;
}
pHead1=pHead1->next;
}
else{
if(head==NULL){
cur = pHead2;
head = cur;
}
else{
cur->next = pHead2;
cur = cur->next;
}
pHead2=pHead2->next;
}
}
if(pHead1 == NULL)
cur->next = pHead2;
if(pHead2 == NULL)
cur->next = pHead1;
return head;
}
};
012-**跳台阶(贪心)
题目是什么恶趣味?
详解 https://blog.csdn.net/weixin_43136158/article/details/106540587
class Solution {
public:
int jumpFloorII(int number) {
if(number==1)
return 1;
return 2*jumpFloorII(number-1);
}
};
013-旋转数组的最小数字(二分)
class Solution {
public:
int minNumberInRotateArray(vector<int> rotateArray) {
int size = (int)rotateArray.size();
if(size==0)
return 0;
for(int i=0;i<size;i++){
if(rotateArray[i]>rotateArray[i+1])
return rotateArray[i+1];
}
return rotateArray[size-1];
}
};
014-用两个栈实现队列(栈)
需要考虑的是入队列中途pop的情况,此时要保持队列先进先出,就要把已经压入stack2的值压回stack1,这样下一个输入后,pop才会按照之前顺序进行。
class Solution
{
public:
void push(int node) {
stack1.push(node);
}
int pop() {
while(!stack1.empty())
{
stack2.push(stack1.top());
stack1.pop();
}
int num=stack2.top();
stack2.pop();
while(!stack2.empty())
{
stack1.push(stack2.top());
stack2.pop();
}
return num;
}
private:
stack<int> stack1;
stack<int> stack2;
};