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;
}
};