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)
 */


相关文章
|
13天前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
26 6
|
13天前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
40 2
|
13天前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
15 7
|
13天前
|
算法 Python
【Leetcode刷题Python】 LeetCode 2038. 如果相邻两个颜色均相同则删除当前颜色
本文介绍了LeetCode 2038题的解法,题目要求在一个由'A'和'B'组成的字符串中,按照特定规则轮流删除颜色片段,判断Alice是否能够获胜,并提供了Python的实现代码。
23 3
|
13天前
|
算法 Python
【Leetcode刷题Python】剑指 Offer 33. 二叉搜索树的后序遍历序列
本文提供了一种Python算法,用以判断给定整数数组是否为某二叉搜索树的后序遍历结果,通过识别根节点并递归验证左右子树的值是否满足二叉搜索树的性质。
11 3
|
13天前
|
Python
【Leetcode刷题Python】50. Pow(x, n)
本文介绍了LeetCode第50题"Pow(x, n)"的解法,题目要求实现计算x的n次幂的函数,文章提供了递归分治法的详细解析和Python实现代码。
10 1
|
13天前
|
Python
【Leetcode刷题Python】LeetCode 478. 在圆内随机生成点
本文介绍了LeetCode 478题的解法,题目要求在给定圆的半径和圆心位置的情况下实现在圆内均匀随机生成点的功能,并提供了Python的实现代码。
11 1
|
13天前
|
算法 Python
【Leetcode刷题Python】295. 数据流的中位数
本文介绍了一种使用Python实现的数据结构,用以支持数据流中添加整数并返回当前所有元素的中位数,通过排序列表来计算中位数。
9 1
|
13天前
|
算法 Python
【Leetcode刷题Python】73. 矩阵置零
本文介绍了LeetCode第73题的解法,题目要求在给定矩阵中将所有值为0的元素所在的行和列全部置为0,并提供了一种原地算法的Python实现。
13 0
【Leetcode刷题Python】73. 矩阵置零
|
13天前
|
Python
【Leetcode刷题Python】1467. 两个盒子中球的颜色数相同的概率
本文介绍了LeetCode第50题"Pow(x, n)"的解法,题目要求实现计算x的n次幂的函数,文章提供了递归分治法的详细解析和Python实现代码。
13 0