lintcode

1、单链表翻转
/**
 * Definition of singly-linked-list:
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *        this->val = val;
 *        this->next = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param head: n
     * @return: The new head of reversed linked list.
     */
    ListNode* reverse(ListNode *head) {
        // write your code here
        if (head == nullptr || head->next == nullptr)
            return head;
        ListNode* pPre = head;
        ListNode* pCur = head->next;
        head->next = nullptr;
        while (pCur) {
            ListNode* pTemp = pCur->next;
            pCur->next = pPre;
            pPre = pCur;
            pCur = pTemp;
        }
        return pPre;
    }
};

核心要点:一定要注意将首节点的next指针赋值为nullptr,否则会导致死循环。
题目链接:35 · 翻转链表(一) - LintCode

1、链表翻转2
/**
 * Definition of singly-linked-list:
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *        this->val = val;
 *        this->next = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param head: ListNode head is the head of the linked list 
     * @param m: An integer
     * @param n: An integer
     * @return: The head of the reversed ListNode
     */
    ListNode* reverseBetween(ListNode *head, int m, int n) {
        // write your code here
        if (!head || !head->next || m == n)
            return head;
            
        ListNode tempHead(0);
        tempHead.next = head;
        ListNode* pPre = &tempHead;
        ListNode* pCur = head;
        ListNode* pMPre = nullptr;
        int count = 1;
        while (pCur) {
            if (count == m) {
                pMPre = pPre;
                break;
            }
            pPre = pCur;
            pCur = pCur->next;
            ++count;
        }
        
        pMPre->next = nullptr;
        ListNode* pNPre = pCur;
        while (pCur) {
            if (count == n) {
                pNPre->next = pCur->next;
            }           
            ListNode* pNext = pCur->next;
            pCur->next = pMPre->next;
            pMPre->next = pCur;
            pCur = pNext;
            if (count == n)
                break;
            ++count;
        }
        
        return tempHead.next;
    }
};

核心要点:
1)创建一个哨兵节点指向头结点,可以方便处理 m==1的情况,哨兵节点可以使用栈对象,使用new还需要delete;
2)两次while循环,第一次找到m节点的上一个节点pMPre,此时pCur节点是n节点的上一个节点,需要记录此节点的指针ListNode* pNPre = pCur,还要注意pMPre->next = nullptr;

  1. 第二次while循环翻转链表,并查找n节点,找到n节点停止,并pNPre->next = pCur->next;
  2. 注意此时头结点的指针是tempHead.next,而不是head;
  3. 当只有一个节点,或m==n时不做任何操作,直接返回head

题目链接:36 · 翻转链表(二) - LintCode

3、回文链表
/**
 * Definition of singly-linked-list:
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *        this->val = val;
 *        this->next = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param head: A ListNode.
     * @return: A boolean.
     */
    bool isPalindrome(ListNode *head) {
        // write your code here
        if (!head || !head->next)
            return true;
        
        ListNode* pSlow = head;
        ListNode* pFast = head;
        while (pFast->next && pFast->next->next) {
            pFast = pFast->next->next;
            pSlow = pSlow->next;
        }
        
        ListNode* pCur = pSlow->next;
        ListNode tempHead(0);
        tempHead.next = nullptr;
        while (pCur) {
            ListNode* pNext = pCur->next;
            pCur->next = tempHead.next;
            tempHead.next = pCur;
            pCur = pNext;
        }
        
        pSlow->next = nullptr;
        ListNode* pLeft = head;
        ListNode* pRight = tempHead.next;
        while (pLeft && pRight) {
            if (pLeft->val != pRight->val)
                return false;
            pLeft = pLeft->next;
            pRight = pRight->next;
        }
        return true;
    }
};

核心要点:一定要翻转后半部分,如果翻转前半部分,需要考虑节点个数是奇数还是偶数,逻辑会非常复杂,翻转后半部分不需要考虑这些,因为不管节点个数是奇数还是偶数,后半部分的第一个节点都是pSlow的下一个节点。
题目链接:223 · 回文链表 - LintCode

4、第k大元素
class Solution {
public:
    /**
     * @param k: An integer
     * @param nums: An array
     * @return: the Kth largest element
     */
    int kthLargestElement(int k, vector &nums) {
        // write your code here
        int begin = 0;
        int end = nums.size() - 1;
        return quicksort(nums, begin, end, k);
    }
    int quicksort(vector &nums, int begin, int end, int k) {
        if (begin == end)
            return nums[begin];
        int partion = nums[end];
        int i = begin;
        for (int j = begin; j < end; j++) {
            if (nums[j] >= partion) {
                int temp = nums[j];
                nums[j] = nums[i];
                nums[i] = temp;
                ++i;
            }
        }
        nums[end] = nums[i];
        nums[i] = partion;
        
        if (k == i + 1)
            return nums[i];
        else if (i < k)
            return quicksort(nums, i + 1, end, k);
        else
            return quicksort(nums, begin, i - 1, k);
    }
};

核心要点:i之前的元素都是大于分割点的元素,j遍历的终点是end-1,最后第i个元素要和end元素交换。
题目链接:5 · 第k大元素 - LintCode

5、在排序数组中查找元素的第一个和最后一个位置
class Solution {
public:
    /**
     * @param nums: the array of integers
     * @param target: 
     * @return: the starting and ending position
     */
    vector searchRange(vector &nums, int target) {
        // Write your code here.
        vector result;
        int low = 0;
        int high = nums.size() - 1;
        int tmp_low = 0;
        int tmp_high = high;
        bool first = true;
        while (low <= high) {
            int mid = low + ((high - low) >> 1);
            if (nums[mid] > target) {
                high = mid - 1;
            } else if (nums[mid] < target) {
                low = mid + 1;
            } else {
                if (first) {
                    tmp_low = low;
                    tmp_high = high;
                    first = false;
                }
                if (mid == 0 || nums[mid - 1] < target) {
                    result.push_back(mid);
                    break;
                }
                high = mid - 1;
            }
        }
        
        if (result.empty()) {
            result.push_back(-1);
            result.push_back(-1);
            return result;
        }
        
        low = tmp_low;
        high = tmp_high;
        while (low <= high) {
            int mid = low + ((high - low) >> 1);
            if (nums[mid] > target) {
                high = mid - 1;
            } else {
                if (mid == nums.size() - 1 || nums[mid + 1] > target) {
                    result.push_back(mid);
                    break;
                }
                low = mid + 1;
            }

        }
        return result;
    }
};

核心要点:注意运算符的优先级 int mid = low + ((high - low) >> 1); 而不是 int mid = low + (high - low) >> 1; 否则会死循环。
题目链接:1536 · 在排序数组中查找元素的第一个和最后一个位置 - LintCode

你可能感兴趣的:(lintcode)