1. leetcode 739 每日温度
class Solution {
public :
vector< int > dailyTemperatures ( vector< int > & temperatures) {
int n = temperatures. size ( ) ;
vector< int > res ( n, 0 ) ;
stack< int > st;
for ( int i = 0 ; i < temperatures. size ( ) ; ++ i) {
while ( ! st. empty ( ) && temperatures[ i] > temperatures[ st. top ( ) ] ) {
auto t = st. top ( ) ; st. pop ( ) ;
res[ t] = i - t;
}
st. push ( i) ;
}
return res;
}
} ;
2. leetcode 316 去除重复字母
class Solution {
public :
string removeDuplicateLetters ( string s) {
string res;
unordered_map< char , int > hash, h;
for ( int i = 0 ; i < s. size ( ) ; i++ )
hash[ s[ i] ] ++ ;
stack< char > stk;
for ( int i = 0 ; i < s. size ( ) ; i++ )
{
if ( stk. size ( ) && h[ s[ i] ] )
{
hash[ s[ i] ] -- ;
continue ;
}
while ( stk. size ( ) && stk. top ( ) > s[ i] && hash[ stk. top ( ) ] )
{
h[ stk. top ( ) ] -- ;
stk. pop ( ) ;
}
stk. push ( s[ i] ) ;
h[ s[ i] ] ++ ;
hash[ s[ i] ] -- ;
}
while ( stk. size ( ) )
{
res + = stk. top ( ) ;
stk. pop ( ) ;
}
reverse ( res. begin ( ) , res. end ( ) ) ;
return res;
}
} ;
3. leetcode 402 移除K位元素
class Solution {
public :
string removeKdigits ( string num, int k) {
int n = num. size ( ) ;
stack< char > s;
string ans;
for ( int i = 0 ; i < n; i++ ) {
while ( ! s. empty ( ) && num[ i] < s. top ( ) && k) {
s. pop ( ) ;
k-- ;
}
s. push ( num[ i] ) ;
}
while ( k) {
s. pop ( ) ;
k-- ;
}
while ( ! s. empty ( ) ) {
ans + = s. top ( ) ;
s. pop ( ) ;
}
if ( ans== "" ) return "0" ;
reverse ( ans. begin ( ) , ans. end ( ) ) ;
int cnt = 0 ;
while ( cnt < ans. size ( ) && ans[ cnt] == '0' ) {
cnt++ ;
}
return cnt == ans. size ( ) ? "0" : ans. substr ( cnt, ans. size ( ) - cnt) ;
}
} ;
4. leetcode 59 螺旋矩阵
class Solution {
public :
vector< vector< int >> generateMatrix ( int n) {
int num = 1 ;
int left = 0 , right = n - 1 , top = 0 , bottom = n - 1 ;
vector< vector< int >> res ( n, vector< int > ( n, 0 ) ) ;
while ( num <= n* n) {
for ( int i = left; i <= right; i++ ) {
res[ top] [ i] = num++ ;
}
++ top;
for ( int i = top; i <= bottom; i++ ) {
res[ i] [ right] = num++ ;
}
-- right;
for ( int i = right; i >= left; -- i) {
res[ bottom] [ i] = num++ ;
}
-- bottom;
for ( int i = bottom; i >= top; -- i) {
res[ i] [ left] = num++ ;
}
++ left;
}
return res;
}
} ;
5. leetcode 54 螺旋矩阵
class Solution {
public :
vector< int > spiralOrder ( vector< vector< int >> & matrix) {
vector< int > res;
if ( matrix. empty ( ) ) return res;
int left = 0 , right = matrix[ 0 ] . size ( ) - 1 , top = 0 , bottom = matrix. size ( ) - 1 ;
while ( true ) {
for ( int i = left; i <= right; ++ i) {
res. push_back ( matrix[ top] [ i] ) ;
}
if ( ++ top > bottom) break ;
for ( int i = top; i <= bottom; ++ i) {
res. push_back ( matrix[ i] [ right] ) ;
}
if ( -- right < left) break ;
for ( int i = right; i >= left; -- i) {
res. push_back ( matrix[ bottom] [ i] ) ;
}
if ( -- bottom < top) break ;
for ( int i = bottom; i >= top; -- i) {
res. push_back ( matrix[ i] [ left] ) ;
}
if ( ++ left > right) break ;
}
return res;
}
} ;
6. leetcode 203 移除链表元素
class Solution {
public :
ListNode* removeElements ( ListNode* head, int val) {
ListNode* dummyhead = new ListNode ( ) ;
dummyhead- > next = head;
ListNode* cur = dummyhead;
while ( cur- > next != NULL ) {
if ( cur- > next- > val == val) {
ListNode* tmp = cur- > next;
cur- > next = cur- > next- > next;
delete tmp;
} else {
cur = cur- > next;
}
}
return dummyhead- > next;
}
} ;
class Solution {
public :
ListNode* removeElements ( ListNode* head, int val) {
if ( head == nullptr ) {
return head;
}
head- > next = removeElements ( head- > next, val) ;
return head- > val == val ? head- > next : head;
}
} ;
7. leetcode 707 设计链表
class MyLinkedList {
public :
struct ListNode {
int val;
ListNode* next;
ListNode ( int val) : val ( val) , next ( nullptr ) {
}
} ;
MyLinkedList ( ) {
ListNode* dummyhead = new ListNode ( 0 ) ;
int size = 0 ;
}
int get ( int index) {
if ( index < 0 || index > ( size - 1 ) ) {
return - 1 ;
}
ListNode* cur = dummyhead- > next;
while ( index-- ) {
cur = cur- > next;
}
return cur- > val;
}
void addAtHead ( int val) {
ListNode* newNode = new ListNode ( val) ;
newNode- > next = dummyhead- > next;
dummyhead- > next = newNode;
size++ ;
}
void addAtTail ( int val) {
ListNode* newNode = new ListNode ( val) ;
ListNode* cur = dummyhead;
while ( cur- > next != NULL ) {
cur = cur- > next;
}
cur- > next = newNode;
size++ ;
}
void addAtIndex ( int index, int val) {
if ( idx > index) {
return ;
}
ListNode* newNode = ListNode ( val) ;
ListNode* cur = dummyhead;
while ( index-- ) {
cur = cur- > next;
}
newNode- > next = cur- > next;
cur- > next = newNode;
size++ ;
}
void deleteAtIndex ( int index) {
if ( index >= size || index < 0 ) {
return ;
}
ListNode* cur = dummyhead;
while ( index-- ) {
cur = cur- > next;
}
ListNode* tmp = cur- > next;
cur- > next = cur- > next- > next;
delete tmp;
size-- ;
}
} ;
#include <iostream>
using namespace std;
class MyListForward
{
private :
struct ListNode
{
int val;
ListNode * next;
ListNode ( int x) : val ( x) , next ( nullptr ) {
}
} ;
ListNode* head;
public :
MyListForward ( ) : head ( nullptr ) {
}
int get ( int index) {
int i= 0 ;
ListNode * p= head;
while ( p&& i< index) {
p= p- > next;
i++ ;
}
if ( p) return p- > val;
else return - 1 ;
}
void addAtHead ( int val) {
ListNode * p= new ListNode ( val) ;
p- > next= head;
head= p;
}
void addAtTail ( int val) {
ListNode * p= new ListNode ( val) ;
if ( head== nullptr ) {
head= p;
return ;
}
ListNode * q= head;
while ( q- > next) {
q= q- > next;
}
q- > next= p;
}
void addAtIndex ( int index, int val) {
ListNode * node= new ListNode ( val) ;
if ( index<= 0 )
{
node- > next= head;
head= node;
return ;
}
int i= 0 ;
ListNode * p= head;
while ( p&& i< index- 1 )
{
p= p- > next;
++ i;
}
if ( p)
{
node- > next= p- > next;
p- > next= node;
}
}
void deleteAtIndex ( int index) {
if ( index== 0 && head!= nullptr )
{
ListNode * del= head;
head= head- > next;
delete del;
return ;
}
int i= 0 ;
ListNode* p= head;
while ( p&& i< index- 1 )
{
p= p- > next;
i++ ;
}
if ( ! p) return ;
if ( p- > next)
{
ListNode * del= p- > next;
p- > next= del- > next;
delete del;
}
}
int length ( ) {
int i= 0 ;
ListNode * p= head;
while ( p) {
i++ ;
p= p- > next;
}
return i;
}
} ;
int main ( )
{
MyListForward mlf;
mlf. addAtIndex ( 0 , 10 ) ;
mlf. addAtIndex ( 0 , 20 ) ;
mlf. addAtIndex ( 1 , 30 ) ;
for ( int i= 0 ; i< mlf. length ( ) ; ++ i) {
cout<< mlf. get ( i) << " " ;
}
cout<< endl;
system ( "pause" ) ;
}
#include <iostream>
using namespace std;
class MyList
{
private :
struct ListNode
{
int val;
ListNode * next, * prev;
ListNode ( int x) : val ( x) , next ( nullptr ) , prev ( nullptr ) {
}
} ;
private :
ListNode * head, * tail;
int size= 0 ;
public :
MyList ( ) : size ( 0 ) , head ( nullptr ) , tail ( nullptr ) {
}
int get ( int index) {
int i= 0 ;
ListNode * p= head;
while ( p&& i< index) {
p= p- > next;
i++ ;
}
if ( p) return p- > val;
else return - 1 ;
}
void addAtHead ( int val) {
if ( head!= nullptr ) {
ListNode * node= new ListNode ( val) ;
node- > next= head;
head- > prev= node;
head= node;
}
else {
head= new ListNode ( val) ;
tail= head;
}
++ size;
}
void addAtTail ( int val) {
if ( tail!= nullptr ) {
ListNode * node= new ListNode ( val) ;
node- > prev= tail;
tail- > next= node;
tail= node;
}
else {
tail= new ListNode ( val) ;
head= tail;
}
++ size;
}
void addAtIndex ( int index, int val) {
if ( index<= 0 ) {
addAtHead ( val) ;
return ;
}
if ( index== size) {
addAtTail ( val) ;
return ;
}
if ( index> size) return ;
ListNode * p= nullptr , * cur= head;
int i= 0 ;
while ( cur&& i< index) {
p= cur;
cur= cur- > next;
i++ ;
}
ListNode * node= new ListNode ( val) ;
p- > next= node;
node- > prev= p;
node- > next= cur;
cur- > prev= node;
size++ ;
}
void deleteAtIndex ( int index) {
if ( ! head) return ;
if ( index== 0 )
{
ListNode * del= head;
head= head- > next;
if ( head) {
head- > prev= nullptr ;
}
else {
tail= nullptr ;
}
delete del;
size-- ;
return ;
}
if ( index== size- 1 ) {
ListNode * del= tail;
tail= tail- > prev;
if ( tail) {
tail- > next= nullptr ;
}
delete del;
size-- ;
return ;
}
int i= 0 ;
ListNode * p= nullptr , * cur= head;
while ( cur) {
if ( i== index) {
ListNode * del= cur;
p- > next= cur- > next;
if ( cur- > next) {
cur- > next- > prev= p;
}
delete del;
size-- ;
return ;
}
p= cur;
cur= cur- > next;
++ i;
}
}
int length ( ) {
return size;
}
} ;
int main ( )
{
MyList ml;
ml. addAtHead ( 1 ) ;
ml. addAtTail ( 3 ) ;
ml. addAtHead ( 4 ) ;
ml. addAtHead ( 5 ) ;
ml. addAtIndex ( 1 , 2 ) ;
for ( int i= 0 ; i< ml. length ( ) ; ++ i) {
cout<< ml. get ( i) << " " ;
}
cout<< endl;
ml. deleteAtIndex ( 1 ) ;
for ( int i= 0 ; i< ml. length ( ) ; ++ i) {
cout<< ml. get ( i) << " " ;
}
cout<< endl;
system ( "pause" ) ;
}
8. leetcode 206 反转链表
class Solution {
public :
ListNode* reverseList ( ListNode* head) {
ListNode* tmp;
ListNode* cur = head;
ListNode* pre = NULL ;
while ( cur) {
tmp = cur- > next;
cur- > next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
} ;
class Solution {
public :
ListNode* reverseList ( ListNode* head) {
ListNode* tmp;
ListNode* cur = head;
ListNode* pre = NULL ;
while ( cur) {
tmp = cur- > next;
cur- > next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
} ;
9. leetcode 24 两两交换链表节点
class Solution {
public :
ListNode* swapPairs ( ListNode* head) {
ListNode* dummyhead = new ListNode ( 0 ) ;
dummyhead- > next = head;
ListNode* cur = dummyhead;
while ( cur- > next != nullptr && cur- > next- > next != nullptr ) {
ListNode* tmp = cur- > next;
ListNode* tmp1 = cur- > next- > next- > next;
cur- > next = cur- > next- > next;
cur- > next- > next = tmp;
cur- > next- > next- > next = tmp1;
cur- > next- > next;
}
return dummyhead- > next;
}
} ;
class Solution {
public :
ListNode* swapPairs ( ListNode* head) {
if ( head == nullptr || head- > next == nullptr ) {
return head;
}
ListNode* newhead = head- > next;
head- > next = swapPairs ( newhead- > next) ;
newhead- > next = head;
return newhead;
}
} ;
class Solution {
public :
ListNode* swapPairs ( ListNode* head) {
ListNode* dummyhead = new ListNode ( 0 ) ;
dummyhead- > next = head;
ListNode* cur = dummyhead;
while ( cur- > next != nullptr && cur- > next- > next != nullptr ) {
ListNode* tmp = cur- > next;
ListNode* tmp1 = cur- > next- > next;
cur- > next = tmp1;
tmp- > next = tmp1- > next;
tmp1- > next = tmp;
cur = tmp;
}
return dummyhead- > next;
}
} ;
10. leetcode 19 删除链表倒数第N个节点
class Solution {
public :
ListNode* removeNthFromEnd ( ListNode* head, int n) {
ListNode* dummyhead = new ListNode ( 0 ) ;
dummyhead- > next = head;
ListNode* slow = dummyhead;
ListNode* fast = dummyhead;
while ( n-- && fast != nullptr ) {
fast = fast- > next;
}
fast = fast- > next;
while ( fast != nullptr ) {
fast = fast- > next;
slow = slow- > next;
}
slow- > next = slow- > next- > next;
return dummyhead- > next;
}
} ;
class Solution {
public :
int getLength ( ListNode* head) {
int length = 0 ;
while ( head) {
++ length;
head = head- > next;
}
return length;
}
ListNode* removeNthFromEnd ( ListNode* head, int n) {
ListNode* dummyhead = new ListNode ( 0 ) ;
dummyhead- > next = head;
int length = getLength ( head) ;
ListNode* cur = dummyhead;
for ( int i = 1 ; i < length - n + 1 ; ++ i) {
cur = cur- > next;
}
cur- > next = cur- > next- > next;
ListNode* res = dummyhead- > next;
delete dummyhead;
return res;
}
} ;
class Solution {
public :
ListNode* removeNthFromEnd ( ListNode* head, int n) {
ListNode* dummyhead = new ListNode ( 0 , head) ;
stack< ListNode* > st;
ListNode* cur = dummyhead;
while ( cur) {
st. push ( cur) ;
cur = cur- > next;
}
for ( int i = 0 ; i < n; ++ i) {
st. pop ( ) ;
}
ListNode* pre = st. top ( ) ;
pre- > next = pre- > next- > next;
ListNode* res = dummyhead- > next;
delete dummyhead;
return res;
}
} ;
11. 面试题 02.07. 链表相交
class Solution {
public :
ListNode * getIntersectionNode ( ListNode * headA, ListNode * headB) {
ListNode* A = headA, * B = headB;
while ( A != B) {
A = A != nullptr ? A- > next : headB;
B = B != nullptr ? B- > next : headA;
}
return A;
}
} ;
12. leetcode 142 环形链表II
class Solution {
public :
ListNode * detectCycle ( ListNode * head) {
ListNode* fast = head;
ListNode* slow = head;
while ( fast != NULL && fast- > next != NULL ) {
slow = slow- > next;
fast = fast- > next- > next;
if ( slow == fast) {
ListNode* index1 = fast;
ListNode* index2 = head;
while ( index1 != index2) {
index1 = index1- > next;
index2 = index2- > next;
}
return index2;
}
}
return NULL ;
}
} ;
class Solution {
public :
ListNode * detectCycle ( ListNode * head) {
unordered_set< ListNode * > visited;
while ( head != nullptr ) {
if ( visited. count ( head) ) {
return head;
}
visited. insert ( head) ;
head = head- > next;
}
return nullptr ;
}
} ;
class Solution {
public :
ListNode * detectCycle ( ListNode * head) {
ListNode * slow = head, * fast = head;
while ( fast != nullptr ) {
slow = slow- > next;
if ( fast- > next == nullptr ) {
return nullptr ;
}
fast = fast- > next- > next;
if ( fast == slow) {
ListNode * ptr = head;
while ( ptr != slow) {
ptr = ptr- > next;
slow = slow- > next;
}
return ptr;
}
}
return nullptr ;
}
} ;