0
点赞
收藏
分享

微信扫一扫

leecode Top100算法(反转链表、最大子序和、最后一个单词的长度、数组加1、环形链表、将有序数组转换成二叉搜索树、二叉树中的中序遍历、相交链表、excel表序列号)

《算法笔记》专栏主要是督促自己每天刷几道算法题,并分享给大家,希望您喜欢。

目录

反转链表

在这里插入图片描述

class Solution {
public:
    ListNode* reverseList(ListNode *head) {
    ListNode *p;   #定义一个返回的ListNode*
    for (p=NULL;head;swap(head,p))
        swap(p,head->next);
    return p;
    }
};

最大子序和

 class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int res = nums[0];
        int sum = 0;
        for (int num: nums){
            if(sum>0){
                sum += num;
            
            }else{
                sum = num;
            }
            res =max(res,sum);

        }
        return res;

    }
};

最后一个单词的长度

class Solution {
public:
    int lengthOfLastWord(string s) {
        int length = 0;
        for (int i = s.length() - 1; i >= 0; i--){
            if (s[i] != ' '){
                length++;
            }else if(length != 0){
                return length;
            }
        }
        return length;

    }
};

数组加1,返回数组

class Solution{
public:
    vector<int> plusOne(vector<int>& digits){
        int n=digits.size();
        for(int i=n-1;i>=0;i--){
            digits[i]+=1;
            digits[i]%=10;
            if(digits[i]){
                return digits;
            }
        }
        #数字为9,返回[1,0]
        vector<int> ans(n+1);
        ans[0]=1;
        return ans;
    }
};

环形链表

// 快慢指针
class Solution {
public:
    bool hasCycle(ListNode *head) {
    //这里需要注意判断非空
        if(!head)
        {
            return false;
        }
        ListNode* fast = head;
        ListNode* slow = head;
        do
        {
        //这里需要注意第二个元素和第三个元素是否是空的,如果第二个是空地,只有一个元素,不可能形成环,如果只有两个元素,第二个应该指向第一个也不为空
            if(!fast || !fast->next)
                return false;
            slow = slow->next;
            fast = fast->next->next;
        }while(slow != fast);
        return true;
    }
};

将有序数组转换成二叉搜索树

二叉数的中序遍历

中序遍历的顺序是左、根、右。

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> data;
        if(root==nullptr) return data;//无左孩子,无右孩子
        helper(data,root);
        return data;

    }
private:
    void helper(vector<int> & res,TreeNode* root){
        if(root==nullptr) return ;
        helper(res,root->left);
        res.push_back(root->val);//函数将一个新的元素加到vector的最后面,位置为当前最后一个元素的下一个元素
        helper(res,root->right);
        return ;
    }
};

相交链表

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        // int dataA=0,dataB=0;
        if (headA==NULL|| headB==NULL)
            return NULL;
        ListNode *pA=headA;
        ListNode *pB=headB;
          // 在这里第一轮体现在pA和pB第一次到达尾部会移向另一链表的表头, 而第二轮体现在如果pA或pB相交就返回交点, 不相交最后就是null==null
        while (pA!=pB){            
            pA = pA == NULL ? headB : pA->next;
            pB = pB == NULL ? headA : pB->next;    
        }
         return  pA ;        
    }
};

excel表序列号

class Solution {
public:
    int titleToNumber(string columnTitle) {
//26进制转10进制
    int index = 0, result = 0; 
    for (int i = columnTitle.size()-1;i>=0;i--){
        result+=(columnTitle[i]-64)*pow(26,index++);
    }
    return result;
    }
};
举报

相关推荐

二叉树和中序遍历

0 条评论