LeetCode: 继续easy题5

Contains Duplicate

"""简单
O(n), O(n)
"""
class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        unordered_map<int,int> map;
        for (int i=0; i<nums.size(); i++)
        {
            if(map.find(nums[i])!=map.end())
                return true;
            map.insert({nums[i],0});
        }
        return false;
    }
};

Contains Duplicate II

"""
"""
class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        unordered_map<int,int> map;
        for(int i=0;i<nums.size();i++){
            auto j = map.find(nums[i]);
            if(j!=map.end()){
                if(i-j->second<=k)
                    return true;
                j->second = i;
            }
            else
                map.insert({nums[i],i});
        }
        return false;
    }
};

Implement Stack using Queues

"""
"""
class MyStack {
public:
    /** Initialize your data structure here. */
    queue<int> q;

    MyStack() {
        ;
    }

    /** Push element x onto stack. */
    void push(int x) {
        int len = q.size();
        q.push(x);
        for(int i=0; i<len; i++){
            q.push(q.front());
            q.pop();
        }  
    }

    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int temp = q.front();
        q.pop();
        return temp;
    }

    /** Get the top element. */
    int top() {
        return q.front();
    }

    /** Returns whether the stack is empty. */
    bool empty() {
        return q.size()==0;
    }
};

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * bool param_4 = obj.empty();
 */

Invert Binary Tree

"""
"""
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(root==NULL)
            return root;

        TreeNode* temp = root->left;
        root->left = invertTree(root->right);
        root->right = invertTree(temp);

        return root;
    }
};

Power of Two

class Solution {
public:
    bool isPowerOfTwo(int n) {
        if(n==1)
            return true;
        if(n==0)
            return false;

        while(n%2==0){
            n = n/2;
            if(n==1)
                return true;
        }
        return false;
    }
};

Implement Queue using Stacks

"""Amortized Analysis
"""
class MyQueue {
public:
    /** Initialize your data structure here. */
    MyQueue() {

    }

    stack<int> a;
    stack<int> b;

    /** Push element x to the back of queue. */
    void push(int x) {
        a.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        if(b.empty())
            while(!a.empty()){
                b.push(a.top());
                a.pop();
            }
        int temp = b.top();
        b.pop();
        return temp;
    }

    /** Get the front element. */
    int peek() {
        if(b.empty())
            while(!a.empty()){
                b.push(a.top());
                a.pop();
            }
        return b.top();
    }

    /** Returns whether the queue is empty. */
    bool empty() {
        return a.empty() && b.empty();
    }
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * bool param_4 = obj.empty();
 */

Palindrome Linked List

"""边值条件啥的在纸上画画,不难通过==
"""
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverse(ListNode* head){
        ListNode* previous = NULL;
        ListNode* current = NULL;
        while(head){
            current = head;
            head = head->next;

            current->next = previous;
            previous = current;
        }
        return current;
    }
    bool isPalindrome(ListNode* head) {   
        if(head == NULL || head->next == NULL)
            return true;

        ListNode* slow = head;
        ListNode* fast = head;
        ListNode*reversed = NULL;

        while(fast){
            slow = slow->next;
            fast = fast->next;
            if(fast == NULL || fast->next == NULL){
                reversed = reverse(slow);
                while(reversed){
                    if(reversed->val != head->val)
                        return false;
                    reversed = reversed->next;
                    head = head->next;
                }
                return true;
            }
            else
                fast = fast->next;
        }

    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值