LeetCode算法刷题
一、删除链表的倒数第N个结点
- 题目:
- 解题方法:
1、计算链表长度
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n)
{
//计算链表长度
ListNode*dummy=new ListNode(0,head);
int length=ListNodeLength(head);
ListNode*current=dummy;
for(int i=0;i<length-n;i++){
current=current->next;
}
current->next=current->next->next;
ListNode*ans=dummy->next;
delete dummy;
return ans;
}
private:
int ListNodeLength(ListNode*head){
int length=0;
while(head!=nullptr){
head=head->next;
length++;
}
return length;
}
};
2、利用栈
{
//栈
ListNode*dummy=new ListNode(0,head);
stack<ListNode*>sta;
ListNode*current=dummy;
while(current!=nullptr){
sta.push(current);
current=current->next;
}
for(int i=0;i<n;i++){
sta.pop();
}
ListNode*preList=sta.top();
preList->next=preList->next->next;
ListNode*ans=dummy->next;
delete dummy;
return ans;
}
3、双指针
{
//双指针
ListNode*dummy=new ListNode(0,head);
ListNode*fast=head;
ListNode*slow=dummy;
for(int i=0;i<n;i++){
fast=fast->next;
}
while(fast!=nullptr){
fast=fast->next;
slow=slow->next;
}
slow->next=slow->next->next;
ListNode*ans=dummy->next;
delete dummy;
return ans;
}
4、递归
{
//递归
if(head==nullptr) return nullptr;
head->next=removeNthFromEnd(head->next,n);
index++;
if(index==n) return head->next;
return head;
}
int index=0;
二、有效的括号
- 题目:
- 解题方法:
1、哈希表
class Solution {
public:
bool isValid(string s) {
int n = s.size();
if (n % 2 == 1) {
return false;
}
unordered_map<char, char> pairs = {
{')', '('},
{']', '['},
{'}', '{'}
};
stack<char> stk;
for (char ch: s) {
if (pairs.count(ch)) {
if (stk.empty() || stk.top() != pairs[ch]) {
return false;
}
stk.pop();
}
else {
stk.push(ch);
}
}
return stk.empty();
}
};
2、遍历
bool isValid(string s) {
stack<char> stack;
for(int i=0; i<s.length(); ++i){
char c = s[i];
if(!stack.empty()){
char t = stack.top();
if(t=='(' && c==')'
|| t=='[' && c==']'
|| t=='{' && c=='}'){
stack.pop();
continue;
}
}
stack.push(c);
}
return stack.empty();
}
三、合并两个有序链表
- 题目:
- 解题方法:
1、递归
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1==nullptr&&l2==nullptr) return nullptr;
if(l1==nullptr&&l2!=nullptr) return l2;
if(l1!=nullptr&&l2==nullptr) return l1;
if(l1->val > l2->val) {
l2->next=mergeTwoLists(l1,l2->next);
return l2;
}
else {
l1->next=mergeTwoLists(l1->next,l2);
return l1;
}
}
};
2、迭代
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1==nullptr&&l2==nullptr) return nullptr;
if(l1==nullptr&&l2!=nullptr) return l2;
if(l1!=nullptr&&l2==nullptr) return l1;
ListNode*newListNode=new ListNode(-1);
ListNode*prev=newListNode;
while(l1!=nullptr&&l2!=nullptr){
if(l1->val>l2->val){
prev->next=l2;
l2=l2->next;
}else{
prev->next=l1;
l1=l1->next;
}
prev=prev->next;
}
prev->next=l1!=nullptr?l1:l2;
return newListNode->next;
}
四、整数转换英文表示
- 题目:
- 解题方法:
class Solution {
public:
string numberToWords(int num) {
if(num==0) return "Zero";
string str;
for(int i =3,unit=1000000000;i>=0;i--,unit/=1000){
int currentNum=num/unit;
if(currentNum){
num-=currentNum*unit;
string strNum;
recusion(strNum,currentNum);
str+=strNum+thousands[i];
}
}
while(str.back()==' ') str.pop_back();
return str;
}
void recusion(string&str,int num){
if(num==0) return ;
if(num<10) str+=singles[num%10];
else if(num<20) str+=teens[num-10];
else if(num<100) str+=tens[num/10],recusion(str,num%10);
else{
str+=singles[num/100]+"Hundred ";
recusion(str,num%100);
}
}
private:
vector<string> singles = {"","One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine "};
vector<string> teens = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ","Seventeen ", "Eighteen ", "Nineteen "};
vector<string> tens = {"", "Ten ", "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "};
vector<string> thousands = {"", "Thousand ", "Million ", "Billion "};
};