英雄哥《算法零基础100讲》01 学习第一天

目录

第一讲 幂和对数

一、知识点

二、题目

1.  4的幂

2.  2的幂

3.   3的幂

第二讲  数列

一、知识点

二、题目

1.斐波那契数列

2.泰波那契数列

3.求1+2+······+n

4.单调数列

5.解压缩编码列表

6.连续整数求和

7.连续整数求和(difficult)


第一讲 幂和对数

一、知识点

①pow(n,m)计算n的m次方;

②log2(T)计算以2为底,T的对数;T>0

③换底公式 loga(b)=logc(b)/logc(a);在C语言中,如果我们要求以a为底b的对数,只需要将底换成2求解即可。

二、题目

1.  4的幂

342. 4的幂 - 力扣(LeetCode) (leetcode-cn.com)

class Solution {
public:
    bool isPowerOfFour(int n) {
if(n<=0){
    return false;
}
int x=(int )(log2(n)/2);
return true;
return false;
    }
};

问题:直接给x赋值,没有任何判断。

解决方案:x=log4(n)。

相等的判定是建立在浮点数之间的,当两个数的差的绝对小于某个精度才认为它们相等。

fabs(a-b)<0.00000001//fabs求绝对值函数

改正后:

class Solution {
public:
    bool isPowerOfFour(int n) {
if(n<=0){
    return false;
}
int x=(int )(log2(n)/2);  /*或int x=(int)(log2(n)/2+1e-8); 加上一个精度,避免精度损失导致取整出错。*/
return fabs(n-pow(4,x))<1e-8;//判断时,若小于即为true,若大于即为false
    }
};

同理

2.  2的幂

231. 2 的幂 - 力扣(LeetCode) (leetcode-cn.com)

class Solution {
public:
    bool isPowerOfTwo(int n) {
if(n<=0){
    return false;
}
int x=(int)(log2(n)+1e-8);
return fabs(n-pow(2,x))<1e-8;
    }
};

3.   3的幂

326. 3 的幂 - 力扣(LeetCode) (leetcode-cn.com)

class Solution {
public:
    bool isPowerOfThree(int n) {
if(n<=0){
    return false;
}
int x=(int)(log2(n)/log2(3)+1e-8);
return fabs(n-pow(3,x))<1e-8;
    }
};

第二讲  数列

一、知识点

1.数学上,数列。程序中,数组。

a[i]代表数组第i个元素,下标从0开始记。

2.等差数列  a_{i}=a_{i-1}+d,i>0   i=0时,ai=a0

3.等比数列 a_{i}=a_{i-1}*q,i>0 ; a_{i}=a_{0},i=0

4.斐波那契数列 除了第0项和第一项之外,任何一项等于前两项之和。

f(n)=0,n=0;  1,n=1;   f(n-1)+f(n-2),n>1;

二、题目

1.斐波那契数列

509. 斐波那契数 - 力扣(LeetCode) (leetcode-cn.com)

注意提示:0<=n<=30;

class Solution {
public:
    int fib(int n) {
int f[31];
f[0]=0,f[1]=1;
for(int i=2;i<=n;i++){
    f[i]=f[i-1]+f[i-2];
}
return f[n];
    }
};

2.泰波那契数列

1137. 第 N 个泰波那契数 - 力扣(LeetCode) (leetcode-cn.com)

class Solution {
public:
    int tribonacci(int n) {
int t[38];
t[0]=0,t[1]=1,t[2]=1;
for(int i=3;i<=n;i++){
    t[i]=t[i-3]+t[i-2]+t[i-1];
}
return t[n];
    }
};

3.求1+2+······+n

剑指 Offer 64. 求1+2+…+n - 力扣(LeetCode) (leetcode-cn.com)

class Solution {
public:
    int sumNums(int n) {
 n && (n += sumNums(n-1));
        return n;
    }
};
class Solution {
public:
    int sumNums(int n) {
n==1 || (n+=sumNums(n-1));
        return n;
    }
};

4.单调数列

896. 单调数列 - 力扣(LeetCode) (leetcode-cn.com)

如果既有nums[i] > nums[i + 1]又有nums[i] < nums[i + 1],说明不是递增的,

满足两个条件时各自都为false,在最终return值,利用  ||  运算符 ,若false||false,return值必然为false,若false||true,return值为true,两个true,return值也为true。

class Solution {
public:
    bool isMonotonic(vector<int>& nums) {
bool inc =true,dec=true;
int n=nums.size();
for(int i=0;i<n-1;i++){
    if(nums[i]>nums[i+1])
  inc=false;
  if(nums[i]<nums[i+1])
  dec=false;
}
return inc||dec;
    }
};

5.解压缩编码列表

1313. 解压缩编码列表 - 力扣(LeetCode) (leetcode-cn.com)

class Solution {
public:
    vector<int> decompressRLElist(vector<int>& nums) {
         vector<int> res;
        for(int i = 0; i < nums.size(); i+=2 ){  //i为偶数时,a【i】为a[i+1]的个数
            int n = nums[i];    //个数
            while(n--){
                res.push_back(nums[i+1]);  //n个nums[i+1]
            }
        }
        return res;
    }
};

6.连续整数求和

剑指 Offer 57 - II. 和为s的连续正数序列 - 力扣(LeetCode) (leetcode-cn.com)

连续正整数序列的左边界i和右边界j,则此序列的元素和

target=元素平均值(i+j)/2 *元素数量(j-i+1)。

j= (-1 + sqrt(1 + 4 * (2 * target + (long) i * i - i))) / 2;因为j>i,  所以j为正数。j计算的结果有一个为负数舍去。

push_back()函数的用法

函数将一个新的元素加到vector的最后面,位置为当前最后一个元素的下一个元素

push_back() 在Vector最后添加一个元素(参数为要插入的值)

class Solution {
public:
    vector<vector<int>> findContinuousSequence(int target) {
int i = 1;
        double j = 2.0;
        vector<vector<int>> res;
        while(i < j) {
            j = (-1 + sqrt(1 + 4 * (2 * target + (long) i * i - i))) / 2;
            if(i < j && j == (int)j) {
                vector<int> ans;
                for(int k = i; k <= (int)j; k++)
                    ans.push_back(k); //每一组i——j
                res.push_back(ans); //所有i——j
            }
            i++;
        }
        return res;
    }
};

7.连续整数求和(difficult)

829. 连续整数求和 - 力扣(LeetCode) (leetcode-cn.com)

N=(x+1)+(x+2)+(x+3)+``````+(x+k)= kx+ k *(k+1)/2

2N=k*(2x+k+1),k<sqrt(2N)  //sqrt求平方根。

class Solution {
public:
    int consecutiveNumbersSum(int N) {
int res=1;
for(int i=2;i<sqrt(2*N);i++){  //以x+1为头,k至少为2
    if((N-i*(i+1)/2)%i==0)//可以整除,满足公式  N=kx+ k *(k+1)/2
    ++res;
}
return res;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值