Linked List Cycle II

题目链接:https://leetcode.com/problems/linked-list-cycle-ii/description/

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

Note: Do not modify the linked list.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        if (head==NULL||head->next==NULL) {
            return NULL;
        }
        ListNode *slow = head;
        ListNode *fast = head;
        ListNode *entry = head;
        while (fast->next&&fast->next->next) {
            fast = fast->next->next;
            slow = slow->next;
            if (fast==slow) {
                while (slow!=entry) {
                    slow = slow->next;
                    entry = entry->next;
                }
                return entry;;
            }
        }
        return NULL;
    }
};
2017/12/16 posted in  leetcode

Linked List Cycle

Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?

使用快慢两个指针来判断是否有环,当有环的时候,快指针会停留在环内知道慢指针赶上与之相等。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        if (head==NULL||head->next==NULL) {
            return 0;
        }
        ListNode *slow = head;
        ListNode *fast = head;
        while (fast->next&&fast->next->next) {
            fast = fast->next->next;
            slow = slow->next;
            if (fast==slow) {
                return 1;
            }
        }
        return 0;
    }
};
2017/12/16 posted in  leetcode

 Convert a Number to Hexadecimal

class Solution {
public:
    string toHex(int num) {
        const string HEX ="0123456789abcdef";
        if (num==0) return "0";
        string res ="";
        int count  = 0;
        while (num&&count++<8) {
            //&0xf表示取二进制的低四位
            res = HEX[(num&0xf)]+res;
            num>>=4;
        }
        return res;
    }
};
2017/12/13 posted in  leetcode

Number of Boomerangs

class Solution {
public:
    int numberOfBoomerangs(vector<pair<int, int>>& points) {
        int res = 0;
        for (int i =0; i<points.size(); i++) {
            unordered_map<long, int> map(points.size());
            for (int j = 0; j<points.size(); j++) {
                if (j==i) continue;
                int dy = points[j].second - points[i].second;
                int dx = points[j].first - points[i].first;

                int dis = dy*dy + dx*dx;
                map[dis]++;
            }

            for (auto p : map) {
                if (p.second>1) {
                    /*
                     * for all the groups of points,
                     * number of ways to select 2 from n =
                     * nP2 = n!/(n - 2)! = n * (n - 1)
                     */
                    res+=p.second*(p.second-1);
                }
            }
        }
        return res;
    }
};
2017/12/13 posted in  leetcode

Longest Palindromic Substring

class Solution {
public:
    string longestPalindrome(string s) {
        if (s.empty()) return "";
        if (s.size() == 1) return s;
        int min_start = 0, max_len = 1;
        for (int i = 0; i < s.size();) {
            if (s.size() - i <= max_len / 2) break;
            int j = i, k = i;
            while (k < s.size()-1 && s[k+1] == s[k]) ++k; // Skip duplicate characters.
            i = k+1;
            while (k < s.size()-1 && j > 0 && s[k + 1] == s[j - 1]) { ++k; --j; } // Expand.
            int new_len = k - j + 1;
            if (new_len > max_len) { min_start = j; max_len = new_len; }
        }
        return s.substr(min_start, max_len);
    }
};
2017/12/12 posted in  leetcode

Palindrome Pairs

class Solution {
public:
    bool isPalindrome(string s) {
        if (s.size() <= 1) return true;
        int i = 0;
        int j = s.size() - 1;
        while (i < j) {
            if (s[i++] != s[j--]) return false;
        }

        return true;
    }

    vector<vector<int>> palindromePairs(vector<string>& words) {
        vector<vector<int>> res;
        if (!words.size()) return res;
        unordered_map<string, int> word_idx;
        for (int i = 0; i < words.size(); ++i) {
            word_idx[words[i]] = i;
        }
        vector<int> slu(2);
        for (int i = 0; i < words.size(); ++i) {
            int len = words[i].length();
            for (int l = 0; l <= len; ++l) {
                string left = words[i].substr(0, l);
                string right = words[i].substr(l);
                string rleft = left;
                string rright = right;
                reverse(rleft.begin(), rleft.end());
                reverse(rright.begin(), rright.end());
                if (word_idx.find(rleft) != word_idx.end()) {
                    if (word_idx[rleft] != i && isPalindrome(right)) {
                        res.push_back({i,word_idx[rleft]});
                    }

                }
                if (l != 0 && word_idx.find(rright) != word_idx.end()) {
                    if (word_idx[rright] != i && isPalindrome(left)) {
                        res.push_back({word_idx[rright],i});
                    }

                }

            }
        }
        return res;
    }
};
2017/12/12 posted in  leetcode