LeetCode刷题笔记3

1. c++的取整函数,题目:2020春季赛,拿硬币     cnblogs.com/zjutlitao/p/3558218.html   

头文件:

fix,朝零方向取整,如fix(-1.3)=-1; fix(1.3)=1;
floor,朝负无穷方向取整,如floor(-1.3)=-2; floor(1.3)=1;
ceil,朝正无穷方向取整,如ceil(-1.3)=-1; ceil(1.3)=2;
round,四舍五入到最近的整数,如round(-1.3)=-1;round(-1.52)=-2;round(1.3)=1;round(1.52)=2

2. back函数, 题目:4.19周赛,重新格式化字符    https://www.cnblogs.com/huyao/p/6664830.html

返回当前vector容器中末尾元素的引用。

3. +=不能用的地方,题目:重新格式化字符

for(int i = 0; i < n; i++)
            res = res + s1[i] + s2[i];  //s1和s2是字符串,
                                        //res += ...是错误的

4. memset函数,题目:2020春季赛,传递信息    https://www.cnblogs.com/anthow/p/3392233.html

memset 函数是内存赋值函数,用来给某一块内存空间进行赋值的;

包含在头文件中,可以用它对一片内存空间逐字节进行初始化;

原型为 :   void *memset(void *s, int v, size_t n);  

这里s可以是数组名,也可以是指向某一内在空间的指针;v为要填充的值;n为要填充的字节数。.

5. sizeof   题目:传递信息            https://www.cnblogs.com/felove2013/articles/3524477.html

sizeof操作符的作用是返回一个对象或类型名的长度,长度的单位是字节。

6. 树的层次遍历,题目:二叉树的序列化与反序列化   https://blog.csdn.net/tianjindong0804/article/details/87359564

层次遍历要用队列

为什么用前序遍历???????

7. 降低时间复杂度,例子:962最大宽度坡

//超出时间限制
class Solution {
public:
    int maxWidthRamp(vector& A) {
        int res = 0;
        for(int i = 0; i < A.size() - 1; i++)
            for(int j = i + 1; j < A.size(); j++)
                if(A[i] <= A[j])
                    res = max(res, j - i);
        return res;                
    }
};

//正确答案
class Solution {
public:
    int maxWidthRamp(vector& A) {
        stack s;
        int res = 0, n = A.size();

        for(int i=0; ires; i--)
            while(!s.empty() && A[i] >= A[s.top()]){
                res = max(res, i-s.top());
                s.pop();
            }
        return res;
    }
};

思考它是如何降维的,    单调递减栈?

8. 不知道哪里不对,题目:移动零

class Solution {
public:
    void moveZeroes(vector& nums) {
        vector ans;
        for(int i = 0; i < nums.size(); i++)
            if(nums[i] != 0)
                ans.push_back(nums[i]);

        for(int i = 1; i <= nums.size() - ans.size(); i++)
            ans.push_back(0);

        nums.assign(ans.begin(), ans.end());   
    }
};

9. 编译错误:fatal error: control may reach end of non-void function [-Wreturn-type]     }     ^

https://blog.csdn.net/jiary5201314/article/details/50936921

10. 哈希表的使用,理想情况下,哈希表进行查找的时间复杂度为O(1),题目:两数之和

题目:形成两个异或相等数组的三元组数目,还未解

你可能感兴趣的:(LeetCode刷题笔记3)