LeetCode 圖解演算法資料結構
阿新 • • 發佈:2022-04-07
替換空格
class Solution {
public:
string replaceSpace(string s) {
string res;
for(auto c:s){
if(c!=' '){
res+=c;
continue;
}
res+="%20";
}
return res;
}
};
從尾到頭列印連結串列
方法一:遞迴
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: vector<int> reversePrint(ListNode* head) { vector<int> res; if(head==NULL){ return res; } res=reversePrint(head->next); res.push_back(head->val); return res; } };
方法二:棧
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: vector<int> reversePrint(ListNode* head) { vector<int> res; stack<int> stack; while(head!=NULL){ stack.push(head->val); head=head->next; } while(!stack.empty()){ res.push_back(stack.top()); stack.pop(); } return res; } };
用兩個棧實現佇列
class CQueue { private: stack<int> stack1; stack<int> stack2; public: CQueue() { } void appendTail(int value) { stack1.push(value); } int deleteHead() { int res=-1; if(stack1.empty() && stack2.empty()){ return res; } if(!stack2.empty()){ res=stack2.top(); stack2.pop(); return res; } while(!stack1.empty()){ stack2.push(stack1.top()); stack1.pop(); } res=stack2.top(); stack2.pop(); return res; } }; /** * Your CQueue object will be instantiated and called as such: * CQueue* obj = new CQueue(); * obj->appendTail(value); * int param_2 = obj->deleteHead(); */
表示數值的字串
class Solution {
private:
bool scanUnsignedInteger(string &s,int &index){
int pos=index;
while(s[index]>='0' && s[index]<='9'){
index++;
}
return index>pos;
}
bool scanInteger(string &s,int &index){
if(s[index]=='+' || s[index]=='-'){
index++;
}
return scanUnsignedInteger(s,index);
}
public:
bool isNumber(string s) {
int index=0;
bool numeric=false;
while(s[index]==' '){
index++;
}
numeric=scanInteger(s,index);
if(s[index]=='.'){
index++;
numeric=(scanUnsignedInteger(s,index)||numeric);
}
if(s[index]=='e' || s[index]=='E'){
index++;
numeric=numeric&&scanInteger(s,index);
}
while(s[index]==' '){
index++;
}
return numeric&&(index==s.size());
}
};
反轉連結串列
方法一:遞迴
/**
* 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==NULL || head->next==NULL){
return head;
}
ListNode *tail=reverseList(head->next);
head->next->next=head;
head->next=NULL;
return tail;
}
};
方法二:雙指標
/**
* 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==NULL || head->next==NULL){
return head;
}
ListNode *cur=head;
ListNode *pre=NULL;
while(cur!=NULL){
ListNode *temp=cur->next;
cur->next=pre;
pre=cur;
cur=temp;
}
return pre;
}
};
包含min函式的棧
class MinStack {
private:
stack<int> stack;
vector<int> minNums;
public:
/** initialize your data structure here. */
MinStack() {
}
void push(int x) {
stack.push(x);
if(minNums.empty()){
minNums.push_back(x);
}
else {
minNums.push_back(std::min(minNums[minNums.size()-1],x));
}
}
void pop() {
stack.pop();
minNums.pop_back();
}
int top() {
return stack.top();
}
int min() {
return minNums[minNums.size()-1];
}
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->min();
*/