【leetcode学习计划】编程能力入门(12 / 12)完结!

太简单的题就不po了

目录

单调栈

自定义排序

用栈实现队列


976. 三角形的最大周长

如果最大边的前两条边都不能满足a+b>c,那么更往前的边值更小,也比不可能满足条件,就无需考虑了,只需要考虑连续的3条边即可

class Solution {
public:
    int largestPerimeter(vector<int>& nums) {
        int n=nums.size();
        sort(nums.begin(),nums.end());
        for(int i=n-1;i>1;i--)
            if(nums[i]<nums[i-1]+nums[i-2]) 
                   return nums[i]+nums[i-1]+nums[i-2];
        return 0;
    }
};

1779. 找到最近的有相同 X 或 Y 坐标的点

要返回最小的下标,则 d<min 时记录下标就可以了,因为后面如果还有曼哈顿距离等于该点时,因为不满足 d<min这个条件所以不记录下标

class Solution {
public:
    int nearestValidPoint(int x, int y, vector<vector<int>>& points) {
        int n=points.size();
        int d=0,min=0x3f3f3f3f,idx=-1;
        for(int i=0;i<n;i++)
            if(points[i][0]==x||points[i][1]==y)
            {
                d=abs(points[i][0]-x)+abs(points[i][1]-y);
                if(d<min) min=d,idx=i;
            }
        return idx;
    }
};

202. 快乐数

用哈希表来判断

如果不是快乐数,则必然形成循环,如果res之前存在,则说明进入循环,return false

如果循环中res=1,则说明是快乐数

class Solution {
public:
    bool isHappy(int n) {
        set<int>mp;
        while(1)
        {
            int res=0;
            while(n)
            {
                res+=(n%10)*(n%10);
                n/=10;
            }
            if(res==1) return true;
            if(mp.count(res)) return false;
            mp.insert(res);
            n=res;
        }
    }
};

1790. 仅执行一次字符串交换能否使两个字符串相等

记录两个不相等的字母位置 如果交换这两个位置 s1==s2 则说明一次交换可以做到

class Solution {
public:
    bool areAlmostEqual(string s1, string s2) {
        if(s1.size()!=s2.size()) return false;
        int st=-1,ed=-1;
        for(int i=0;i<s1.size();i++)
        {
            if(s1[i]!=s2[i]&&st==-1) st=i;
            else if(s1[i]!=s2[i]) ed=i;
        }
        if(st!=-1&&~ed) swap(s1[st],s1[ed]);
        return s1==s2;
    }
};

589. N 叉树的前序遍历

递归法

因为输入是层序遍历 因此只要按输入顺序依次递归输出孩子就可以了

class Solution {
public:

    void dfs(vector<int>&res,Node* root)
    {
        if(root==NULL) return;
        res.push_back(root->val);
        for(auto &t:root->children)
            dfs(res,t);
    }

    vector<int> preorder(Node* root) {
        vector<int>res;
        dfs(res,root);
        return res;
    }
};

非递归法

用栈 先入根节点 存进res中 然后先入右孩子再入左孩子 这样出栈顺序就是LR

因为题目所给输入是层序遍历 所以倒着入栈刚好满足要求

class Solution {
public:
    vector<int> preorder(Node* root) {
        vector<int>res;
        if(!root) return res;
        stack<Node*>st;
        st.push(root);
        while(!st.empty())
        {
            Node* t=st.top();
            st.pop();
            res.push_back(t->val);
            for(int i=t->children.size()-1;i>=0;i--)//先入右孩子再入左孩子 这样出栈就是LR了
                st.push(t->children[i]);
        }
        return res;
    }
};

496. 下一个更大元素 I

单调栈

栈内维护一个递减的序列 按顺序遍历nums2序列

一旦存在一个破坏递减序列的数 则栈中所有数的最近的最大数就是该数

用map存下每个数对应的最近最大数

遍历nums1数组查询输出

class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
        stack<int>st;
        vector<int>res;
        unordered_map<int,int>mp;
        for(auto x:nums2)
        {
            while(st.size()&&st.top()<x) 
            {
                mp[st.top()]=x;
                st.pop();
            }
            st.push(x);
        }
        for(auto x:nums1)
            if(mp.count(x)) res.push_back(mp[x]);
            else res.push_back(-1);

        return res;
    }
};

1588. 所有奇数长度子数组的和

暴力

class Solution {
public:
    int sumOddLengthSubarrays(vector<int>& arr) {
        int sum=0,n=arr.size();
        for(int st=0;st<n;st++)
            for(int len=1;st+len<=n;len+=2)
            {
                int ed=st+len-1;
                for(int i=st;i<=ed;i++)
                    sum+=arr[i];
            }
        return sum;
    }
};

1768. 交替合并字符串

class Solution {
public:
    string mergeAlternately(string word1, string word2) {
        int n1=word1.size(),n2=word2.size();
        string s;
        int l=0,r=0,cnt=0;
        while(l<=n2-1&&r<=n1-1)
        {
            if(cnt&1) s+=word2[l++];
            else s+=word1[r++];
            cnt++;
        }
        while(l<n2) s+=word2[l++];
        while(r<n1) s+=word1[r++];
        return s;

    }
};

1309. 解码字母到整数映射

class Solution {
public:
    string freqAlphabets(string s) {
        string res;
        for(int i=0;i<s.size();i++)
            if(i+2<s.size()&&s[i+2]=='#')
                res+=(s[i]-'0')*10+(s[i+1]-'1')+'a',i+=2;
            else res+=(s[i]-'1')+'a';
        return res;
    }
};

953. 验证外星语词典

class Solution {
public:
    bool isAlienSorted(vector<string>& words, string order) {
        vector<int>h(26,0);
        int i=1;
        for(auto x:order)
            h[x-'a']=i++;
        for(int i=1;i<words.size();i++)
            if(!check(words[i-1],words[i],h)) return false;
        return true;
    }

    bool check(string s1,string s2,vector<int>h)
    {
        for(int i=0;i<s1.size();i++)
        {
            if(s1[i]==s2[i]) continue;
            if(i>=s2.size()) return false;
            if(h[s1[i]-'a']>h[s2[i]-'a']) return false;
            else return true;
        }
        return true;
    }
};

1290. 二进制链表转整数

class Solution {
public:
    int getDecimalValue(ListNode* head) {
        int len=-1,sum=0;
        ListNode *t=head;
        while(t!=NULL)
        {
            len++;
            t=t->next;
        }
        while(head!=NULL)
        {
            if(head->val)
            sum+=pow(2,len);
            len--;
            head=head->next;
        }
        return sum;
    }
};
class Solution {
public:
    int getDecimalValue(ListNode* head) {
        int sum=0;
        ListNode *t=head;
        while(t!=NULL)
        {
            sum=2*sum+t->val;
            t=t->next;
        }
        return sum;
    }
};

104. 二叉树的最大深度

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root) return max(maxDepth(root->left),maxDepth(root->right))+1;
        return 0;
    }
};

404. 左叶子之和

class Solution {
public:
    int sumOfLeftLeaves(TreeNode *root) {
        if(root==NULL) return 0;
        int sum=0;
        if(root->left!=NULL&&root->left->left==NULL&&root->left->right==NULL) 
            sum+=root->left->val;
        return sum+sumOfLeftLeaves(root->left)+sumOfLeftLeaves(root->right);
    }
};

1356. 根据数字二进制下 1 的数目排序

自定义排序

class Solution {
public:
    int get(int x)
    {
        int s=0;
        while(x)
        {
            s+=x%2;
            x/=2;
        }
        return s;
    }

    vector<int> sortByBits(vector<int>& arr) {
        
        vector<int>bit(10001,0);
        for(auto x:arr)
            bit[x]=get(x);
        sort(arr.begin(),arr.end(),[&](int x,int y)
        {
            if(bit[x]>bit[y]) return false;
            else if(bit[x]<bit[y]) return true;
            else return x<y;
        });
        return arr;
    }
};

​​​​​​232. 用栈实现队列

用栈实现队列

用两个栈实现 一个输入栈 一个输出栈

将输入栈放入输出栈 顺序刚好就是先进先出

class MyQueue {
public:

    stack<int> inst; //输入栈
    stack<int> outst; //输出栈
    
    void push(int x) {
        inst.push(x);
    }
    
    int pop() {
        if(outst.empty()) //只有输出栈不为空时 才能把输入栈里的元素往里放
        {
            while(!inst.empty())//把输入栈的元素都放进输出栈
            {
                outst.push(inst.top());
                inst.pop();
            }
        }
        int k=outst.top();
        outst.pop();
        return k;
    }
    
    int peek() { // peek 返回队头元素
        int res=this->pop();
        outst.push(res); //因为pop操作扔掉了栈顶元素 所以要再加回去
        return res;
    }
    
    bool empty() {
        return inst.empty()&&outst.empty();
    }
};

1603. 设计停车系统

class ParkingSystem {
public:
    int b,m,s;
    ParkingSystem(int big, int medium, int small):b(big) ,m(medium),s(small){}
    
    bool addCar(int carType) {
        if(carType==1) return b-->0;
        if(carType==2) return m-->0;
        if(carType==3) return s-->0;
        return false;
    }
};

303. 区域和检索 - 数组不可变

class NumArray {
public:

    vector<int>sum;
    NumArray(vector<int>& nums) {
        int n=nums.size();
        sum.resize(n+1);
        for(int i=1;i<=n;i++) sum[i]=sum[i-1]+nums[i-1];
    }
    
    int sumRange(int left, int right) {
        left++,right++;
        return sum[right]-sum[left-1];
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值