Leetcode 刷题之链表和位操作

reverse Linked List

146. LRU Cache

class LRUCache {
public:
    LRUCache(int capacity) {
        capacity_=capacity;    
    }

    int get(int key) {
        const auto it = m_.find(key);
        if(it==m_.cend()){
            return -1;
        }
        cache_.splice(cache_.begin(),cache_,it->second);
        return it->second->second;
    }

    void put(int key, int value) {
        const auto it=m_.find(key);
        if(it!=m_.cend()){
            it->second->second=value;
            cache_.splice(cache_.begin(),cache_,it->second);
            return;
        }
        if(cache_.size()==capacity_){
            const auto& node=cache_.back();
            m_.erase(node.first);
            cache_.pop_back();
        }
        cache_.emplace_front(key,value);
        m_[key]=cache_.begin();
    }
private:
    int capacity_;
    list<pair<int,int>> cache_;
    unordered_map<int,list<pair<int,int>>::iterator> m_;
};

/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache obj = new LRUCache(capacity);
 * int param_1 = obj.get(key);
 * obj.put(key,value);
 */

141. Linked List Cycle

/**
 * 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) {
        ListNode* two=head;
        ListNode* one=head;
        while(two){
            if(two->next){
                two=two->next->next;
            }    
            else {
                break;
            }    
            one=one->next;
            if(one==two){
                return true;
            }   
        }

        return false;
    }
};
/**
 * 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) {
        ListNode* cur=head;
        unordered_set<ListNode*> rec;
        while(cur){
            if(rec.find(cur)!=rec.end()){
                return true;
            }   
            rec.insert(cur);
            cur=cur->next;
        }

        return false;
    }
};

725. Split Linked List in Parts

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    vector<ListNode*> splitListToParts(ListNode* root, int k) {
        int len=0;
        ListNode* cur=root;
        while(cur) {
            len++;
            cur=cur->next;
        }
        int ave=len/k;
        int rem=len%k;
        vector<ListNode*> ans(k,NULL);
        cur=root;
        for(int i=0;i<k;i++){
            ans[i]=cur;
            ListNode* prev=NULL;
            for(int j=0;j<ave+(rem>0);j++){
                prev=cur;
                cur=cur->next;
            }    
            if(prev) {
                prev->next=NULL;
            }
            rem--;    
        }
        return ans;
    }
};

206. Reverse Linked List

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(!head) {
            return NULL;
        }    
        ListNode* pre = NULL;
        ListNode* cur = head;
        while(cur != NULL) {
            ListNode* net = cur->next;
            cur->next = pre;
            pre = cur;
            cur = net;
        }    
        return pre;
    }
};

bit operation

461. Hamming Distance

class Solution {
public:
    int hammingDistance(int x, int y) {
        int res=x^y;
        int ans=0;
        for(int i=0;i<32;i++){
            ans+=1&res;
            res=res>>1;
        }
        return ans;
    }
};
class Solution {
public:
    int hammingDistance(int x, int y) {
        int res=x^y;
        int ans=0;
        while(res>0){
            ans+=1&res;
            res=res>>1;
        }
        return ans;
    }
};

477. Total Hamming Distance

class Solution {
public:
    int totalHammingDistance(vector<int>& nums) {
        vector<int> rec(32,0);
        for(auto num:nums){
            for(int i=0;i<32;i++){
                rec[i]+=num&1;
                num>>=1;
            }    
        }   
        int ans=0;    
        for(int idx=0;idx<32;idx++){
            ans+=rec[idx]*(nums.size()-rec[idx]);
        }
        return ans;    
    }
};

476. Number Complement

class Solution {
public:
    int findComplement(int num) {
        for(int i=31;i>=0;i--){
            if(num&(1<<i)){
                break;
            }
            num |= 1<<i;
        }
        return ~num;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值