admin 发布的文章

题目链接:回文链表

给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。

方法一:额外数组

class Solution {
public:
    bool isPalindrome(ListNode* head) {
        vector<int> vals;
        while (head != nullptr) {
            vals.emplace_back(head->val);
            head = head->next;
        }
        for (int i = 0, j = (int)vals.size() - 1; i < j; ++i, --j) {
            if (vals[i] != vals[j]) {
                return false;
            }
        }
        return true;
    }
};

方法二:反转链表+找中间节点

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
    ListNode* findmid(ListNode* head) {
        ListNode* slow = head,*fast = head;
        while (fast && fast->next) {
            slow = slow->next;
            fast = fast->next->next;
        }
        return slow;
    }
    ListNode* reserve(ListNode* head) {
        ListNode* ans = nullptr;
        ListNode* cur = head;
        while(cur) {
            ListNode* t = cur->next;
            cur->next = ans;
            ans = cur;
            cur = t;
        }
        return ans;
    }
public:
    bool isPalindrome(ListNode* head) {
        ListNode* head1 = findmid(head);
        ListNode* head2 = reserve(head1);
        while (head2) {
            if (head->val != head2->val) {
                return false;
            }
            head = head->next;
            head2 = head2->next;
        }
        return true;
    }
};

题目链接:反转链表

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

方法一:反转链表
/**

  • Definition for singly-linked list.
  • struct ListNode {
  • int val;
  • ListNode *next;
  • ListNode() : val(0), next(nullptr) {}
  • ListNode(int x) : val(x), next(nullptr) {}
  • ListNode(int x, ListNode *next) : val(x), next(next) {}
  • };
    */

class Solution {
public:

ListNode* reverseList(ListNode* head) {
    if (head == NULL || head->next == NULL) return head;
    ListNode* ans;
    ans = nullptr;
    while (head) {
        ListNode* t = head->next;
        head->next = ans;
        ans = head;
        head = t;
    }
    return ans;
}

};

题目链接:相交链表

给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。

方法一:哈希表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        unordered_set<ListNode *> m;
        while (headA) {
            m.insert(headA);
            headA = headA->next;
        }
        while (headB) {
            if (m.count(headB)) {
                return headB;
            }
            headB = headB->next;
        }
        return NULL;
    }
};

题目链接:搜索二维矩阵 II

编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性:

  • 每行的元素从左到右升序排列。
  • 每列的元素从上到下升序排列。

方法一:二分查找

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        bool ans = false;
        int m = matrix.size();
        int n = matrix[0].size();
        for (int i = 0;i < m;i++) {
            if (target < matrix[i][0]) continue;
            if (target > matrix[i][n-1]) continue;
            int l = -1,r = n;
            while (l + 1 < r) {
                int mid = (l+r)/2;
                if (matrix[i][mid] == target) return true;
                else if (matrix[i][mid] > target) r = mid;
                else l = mid;
            }
        }
        return false;
    }
};

方法二:旋转45度

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        int i = matrix.size() - 1, j = 0;
        while(i >= 0 && j < matrix[0].size())
        {
            if(matrix[i][j] > target) i--;
            else if(matrix[i][j] < target) j++;
            else return true;
        }
        return false;
    }
};

题目链接:旋转图像

给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。

你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。

方法一:使用额外数组

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        int n = matrix.size();
        // C++ 这里的 = 拷贝是值拷贝,会得到一个新的数组
        auto matrix_new = matrix;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                matrix_new[j][n - i - 1] = matrix[i][j];
            }
        }
        // 这里也是值拷贝
        matrix = matrix_new;
    }
};

方法二:原地旋转

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        int n = matrix.size();
        for (int i = 0;i < n/2;i++) {
            for (int j = 0;j < (n+1)/2;j++) {
                int t = matrix[i][j];
                matrix[i][j] = matrix[n-j-1][i];
                matrix[n-j-1][i] = matrix[n-i-1][n-j-1];
                matrix[n-i-1][n-j-1] = matrix[j][n-i-1];
                matrix[j][n-i-1] = t;
            }
        }
    }
};