LeetCode刷题笔记

简介: LeetCode刷题笔记

1937.扣分后的最大分

/*********************************************
状态转移方程:
f[i][j]=max{f[i−1][x]-∣j−x∣}+points[i][j]
优化:
j>=x时
f[i][j]=max{f[i−1][x]+x∣}+points[i][j]-j
j<=x时
f[i][j]=max{f[i−1][x]-x∣}+points[i][j]+j
**********************************************/
class Solution {
public:
    long long maxPoints(vector<vector<int>>& points) {
        int m=points.size();                //行数
        int n=points[0].size();             //列数
        vector<long long> end(n);           //建立保存状态的数组
        for(int i=0;i<m;i++){               //遍历每一行
            vector<long long> temp(n);      //保存某一行的数据
            long long best=LLONG_MIN;  
            //best存储上一行,并且小于等于当前列的最大值,即max{f[i−1][x]+x∣}
            for(int j=0;j<n;j++){            //正序遍历,每次j++保证j>x;
                best=max(best,end[j]+j);
                temp[j]=best+points[i][j]-j;  //第i行第j列正序的最大值
            }
            best=LLONG_MIN;
            for(int j=n-1;j>=0;j--){           //倒叙序遍历,每次j--保证j<x;
                best=max(best,end[j]-j);
                //best存储上一行,并且大于等于当前列的最大值,即max{f[i−1][x]-x∣}
                temp[j]=max(temp[j],best+points[i][j]+j);  
                                                //第i行第j列的最大值(正序倒叙取最大值)
            }
            end=move(temp);                     //把temp状态转移到end,end在本次循环存储上一行的状态
        }
        return *max_element(end.begin(), end.end());
    }
};

LLONG_MIN:long long的最小值

move():状态转移

max_element():algorithm库中的函数,用于求[first,last)迭代器中的最大值,返回值是一个迭代器,需要解引用获得其值。

1483.树节点的第K个祖先

class TreeAncestor {
public:
    vector<vector<int>> ance;
    const static int MAX_MAX=16;
    TreeAncestor(int n, vector<int>& parent) {
        ance=vector<vector<int>>(n,vector(MAX_MAX,-1));
        for(int i=0;i<n;i++){
            ance[i][0]=parent[i];    //初始化父节点
        }
        for(int j=1;j<MAX_MAX;j++){
            for(int i=0;i<n;i++){
                if(ance[i][j-1]!=-1){
                    ance[i][j]=ance[ance[i][j-1]][j-1];  //倍增
                     //ance[i][j]=ance[ance[i][j-1]][0]; //下一位是父节点
                }
            }
        }
    }
    int getKthAncestor(int node, int k) {
        for(int i=0;i<MAX_MAX;i++){
            if((k>>i)&1){
                node=ance[node][i];
                if(node==-1){
                    return -1;
                }
            }
        }
        return node;
    }
};
/**
 * Your TreeAncestor object will be instantiated and called as such:
 * TreeAncestor* obj = new TreeAncestor(n, parent);
 * int param_1 = obj->getKthAncestor(node,k);
 */

709.转换成小写字母

class Solution {
public:
    string toLowerCase(string s) {
        int len=s.size();
        for(int i=0;i<len;i++){
            if('A'<=s[i]&&s[i]<='Z'){
                s[i]|=32;
            }
        }
        return s;
    }
};
/* 位运算(解题区的思路
        大写变小写、小写变大写 : 字符 ^= 32;
        大写变小写、小写变小写 : 字符 |= 32;  
        小写变大写、大写变大写 : 字符 &= -33;
        eg:
        65(A)->二进制表示为100 0001
        32的二进制表示为 010 0000 
        100 0001|010 0000=110 0001->97(a)
 */


相关文章
|
4天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
8 0
|
4天前
|
算法 索引
【刷题】滑动窗口精通 — Leetcode 30. 串联所有单词的子串 | Leetcode 76. 最小覆盖子串
经过这两道题目的书写,相信大家一定深刻认识到了滑动窗口的使用方法!!! 下面请大家继续刷题吧!!!
9 0
|
4天前
|
算法
【刷题】 leetcode 面试题 08.05.递归乘法
递归算法是一种在计算机科学和数学中广泛应用的解决问题的方法,其基本思想是利用问题的自我相似性,即将一个大问题分解为一个或多个相同或相似的小问题来解决。递归算法的核心在于函数(或过程)能够直接或间接地调用自身来求解问题的不同部分,直到达到基本情况(也称为基础案例或终止条件),这时可以直接得出答案而不必再进行递归调用。
21 4
【刷题】 leetcode 面试题 08.05.递归乘法
|
4天前
|
存储 算法 安全
【刷题】 leetcode 面试题 01.06 字符串压缩
来看效果: 非常好!!!过啦!!!
25 5
【刷题】 leetcode 面试题 01.06 字符串压缩
|
4天前
|
存储 算法 测试技术
|
4天前
|
算法 C语言 C++
|
存储 算法 C语言
C语言刷题~Leetcode与牛客网简单题
C语言刷题~Leetcode与牛客网简单题
|
21天前
刷题之Leetcode160题(超级详细)
刷题之Leetcode160题(超级详细)
13 0
|
21天前
|
Java
刷题之Leetcode19题(超级详细)
刷题之Leetcode19题(超级详细)
14 0