LeetCode經典題目筆記(二)


九、判斷鏈表是否是迴文鏈表
解法:O(N)\O(N)的解法。
class Solution { 
public
    ListNode* temp;
    bool isPalindrome(ListNode* head) { 
        temp = head; 
        return check(head); 
    } 
   bool check(ListNode* p) { 
       if(NULL== p) return true;
       bool isPal = check(p->next) & (temp->val == p->val);
       temp = temp->next;
       return isPal;
    } 
};

十、找出無序數組中出現次數大於n/2的數
解法一、用庫函數 nth_element(nums.begin(), nums.begin() + nums.size() /2, nums.end());
class Solution {
public:
    int majorityElement(vector<int>& nums) {
        nth_element(nums.begin(), nums.begin() + nums.size() / 2, nums.end());
        return nums[nums.size() / 2];
    } 
};
解法二、隨機枚舉(居然是最快的-_-
class Solution {
public:
    int majorityElement(vector<int>& nums) {
        int n = nums.size();
        srand(unsigned(time(NULL)));
        while (true) {
            int idx = rand() % n;
            int candidate = nums[idx];
            int counts = 0
            for (int i = 0; i < n; i++)
                if (nums[i] == candidate) counts++; 
            if (counts > n / 2
                return candidate;
        }
    }
};
解法三、
class Solution { 
public
    int majorityElement(vector<int>& nums) { 
        int major, counts = 0, n = nums.size(); 
        for (inti =0; i < n; i++) { 
            if (!counts) { 
                 major = nums[i]; counts = 1
            } 
            else counts += (nums[i] == major) ?1: -1
        } 
        return major;
 } 
};

十一、刪除排序數組中重複的元素
解法:遍歷一遍,記錄下已遍歷的元素中重複的個數,把元素向前移動重複個數位置就行了。
class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int count = 0;
        for (int i = 1; i < nums.size(); i++){
            if (nums[i] == nums[i-1]) count++;
            else nums[i-count] = nums[i];
        }
        return nums.size()-count;
    }
};

十二、一個字符串只含大小寫字符和空格,返回字符串中最後一個單詞的長度
解法:遍歷一遍,統計每個單詞的長度,若遇到空格則把前一個單詞的長度置0.
class Solution {
public:
    int lengthOfLastWord(string s) {
        int len = 0;
        for (int i = 0;i < s.size();) {
            if (s[i++] != ' ') len++;
            else if (s[i] != '\0'&&s[i] != ' ') len = 0;
        }
        return len;
    }
};

十三、二叉樹的鏡像
解法一:遞歸。
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if (!root)
            return root;
        swap(root->left,root->right);
        invertTree(root->left);
        invertTree(root->right);
        return root;
    }
};
解法二:非遞歸。
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        stack<TreeNode*> s;
        s.push(root);
        while (!s.empty()) {
            TreeNode* temp = s.top();
            s.pop();
            if (temp) {     
            swap(temp->left,temp->right);
                s.push(temp->right);
                s.push(temp->left);
            }
        }
        return root;
    }
};

十四、Happy數的判斷

解法:利用鏈表判環方法,快慢”指針“。Happy數最終會以1結束,非Happy數會無限循環,所以只需要判斷是否出現環即可。
class Solution {
public:
    int digitSquareSum(int n) {
        int sum = 0, tmp;
        while (n) {
            tmp = n % 10;
            sum += tmp * tmp;
            n /= 10;
        }
        return sum;
    }
    bool isHappy(int n) {
        int slow, fast;
        slow = fast = n;
        do {
            slow = digitSquareSum(slow);
            fast = digitSquareSum(fast);
            fast = digitSquareSum(fast);
            if (fast == 1return 1;
        }while (slow != fast);
        return 0;
}
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章