lc 21~30
单链表
class Solution {
public :
ListNode* mergeTwoLists ( ListNode* list1, ListNode* list2) {
auto dummy = new ListNode ( - 1 ) , head = dummy;
while ( list1 || list2) {
int x = list1 != nullptr ? list1-> val : INT_MAX;
int y = list2 != nullptr ? list2-> val : INT_MAX;
if ( x <= y)
head = head-> next = list1, list1 = list1-> next;
else
head = head-> next = list2, list2 = list2-> next;
if ( x == INT_MAX || y == INT_MAX) break ;
}
return dummy-> next;
}
} ;
DFS
class Solution {
public :
vector< string> ans;
vector< string> generateParenthesis ( int n) {
dfs ( "" , 0 , 0 , n) ;
return ans;
}
void dfs ( string temp, int lc, int rc, int n) {
if ( lc == n && rc == n) {
ans. push_back ( temp) ;
return ;
}
if ( lc < n) dfs ( temp + '(' , lc + 1 , rc, n) ;
if ( rc < n && lc > rc) dfs ( temp + ')' , lc, rc + 1 , n) ;
}
} ;
1.暴力遍历就完事了,584ms,O(n * k),n为总节点数,k为链表个数
class Solution {
public :
ListNode* mergeKLists ( vector< ListNode* > & lists) {
auto dummy = new ListNode ( - 1 ) , head = dummy;
int len = lists. size ( ) ;
while ( 1 ) {
int minValm = INT_MAX, seq = 0 ;
for ( int i = 0 ; i < len; i++ ) {
if ( lists[ i] && lists[ i] -> val < minValm) {
minValm = lists[ i] -> val;
seq = i;
}
}
if ( minValm == INT_MAX) break ;
head = head-> next = lists[ seq] ;
lists[ seq] = lists[ seq] -> next;
}
return dummy-> next;
}
} ;
2.优先队列, O(n * logk)
class Solution {
public :
struct Cmp {
bool operator ( ) ( ListNode* x, ListNode* y) {
return x-> val > y -> val;
}
} ;
ListNode* mergeKLists ( vector< ListNode* > & lists) {
auto dummy = new ListNode ( - 1 ) , tail = dummy;
priority_queue< ListNode* , vector< ListNode* > , Cmp> heap;
for ( auto node : lists)
if ( node) heap. push ( node) ;
while ( heap. size ( ) ) {
auto temp = heap. top ( ) ;
heap. pop ( ) ;
tail = tail-> next = temp;
if ( temp-> next) heap. push ( temp-> next) ;
}
return dummy-> next;
}
} ;
单链表,三点记录更新,迭代
class Solution {
public :
ListNode* swapPairs ( ListNode* head) {
if ( ! head || ! head-> next) return head;
auto dummy = new ListNode ( - 1 ) ;
dummy-> next = head;
auto pre = dummy, cur = head, ne = head-> next;
while ( cur && ne) {
cur-> next = ne-> next;
ne-> next = cur;
pre-> next = ne;
pre = cur;
cur = cur-> next;
if ( cur) ne = cur-> next;
}
return dummy-> next;
}
} ;
y总的代码
class Solution {
public :
ListNode* swapPairs ( ListNode* head) {
auto dummy = new ListNode ( - 1 ) ;
dummy-> next = head;
for ( auto p = dummy; p-> next && p-> next-> next; ) {
auto a = p-> next, b = a-> next;
p-> next = b;
a-> next = b-> next;
b-> next = a;
p = a;
}
return dummy-> next;
}
} ;
单链表,k反转
class Solution {
public :
ListNode* reverseKGroup ( ListNode* head, int k) {
auto dummy = new ListNode ( - 1 ) ;
dummy-> next = head;
for ( auto p = dummy; ; ) {
auto pp = p;
for ( int i = 0 ; i < k && pp; i++ ) pp = pp-> next;
if ( ! pp) break ;
auto a = p-> next, b = a-> next;
for ( int j = 0 ; j < k - 1 ; j++ ) {
auto c = b-> next;
b-> next = a;
a = b, b = c;
}
pp = p-> next;
pp-> next = b;
p-> next = a;
p = pp;
}
return dummy-> next;
}
} ;
双指针
class Solution {
public :
int removeDuplicates ( vector< int > & nums) {
int len = nums. size ( ) ;
int j = 0 ;
for ( int i = 0 ; i < len; i++ ) {
if ( ! i || nums[ i] != nums[ i - 1 ] )
nums[ j++ ] = nums[ i] ;
}
return j;
}
} ;
双指针,简单
class Solution {
public :
int removeElement ( vector< int > & nums, int val) {
int index = 0 , len = nums. size ( ) ;
for ( int i = 0 ; i < len; i++ )
if ( nums[ i] != val)
nums[ index++ ] = nums[ i] ;
return index;
}
} ;
KMP模板
class Solution {
public :
int strStr ( string haystack, string needle) {
if ( haystack. length ( ) < needle. length ( ) ) return - 1 ;
int len1 = haystack. length ( ) , len2 = needle. length ( ) ;
string s = ' ' + haystack, p = ' ' + needle;
vector< int > ne ( len2 + 1 ) ;
next ( ne, p) ;
for ( int i = 1 , j = 0 ; i <= len1; i++ ) {
while ( j && s[ i] != p[ j + 1 ] ) j = ne[ j] ;
if ( s[ i] == p[ j + 1 ] ) j++ ;
if ( j == len2) return i - j;
}
return - 1 ;
}
void next ( vector< int > & ne, string p) {
int len = p. length ( ) - 1 , j = 0 ;
for ( int i = 2 ; i <= len; i++ ) {
while ( j && p[ i] != p[ j + 1 ] ) j = ne[ j] ;
if ( p[ i] == p[ j + 1 ] ) j++ ;
ne[ i] = j;
}
}
} ;
二进制位
class Solution {
public :
int divide ( int dividend, int divisor) {
typedef long long LL;
bool is_minus = false ;
if ( ( dividend < 0 && divisor > 0 ) || ( dividend > 0 && divisor < 0 ) )
is_minus = true ;
LL a = abs ( ( LL) dividend) , b = abs ( ( LL) divisor) , res = 0 ;
vector< LL> exp;
for ( LL c = b; c <= a; c += c) exp. push_back ( c) ;
for ( int i = exp. size ( ) - 1 ; i >= 0 ; i-- ) {
if ( a >= exp[ i] ) {
a -= exp[ i] ;
res += 1LL << i;
}
}
if ( is_minus) res = - res;
if ( res > INT_MAX || res < INT_MIN) return INT_MAX;
return res;
}
} ;
哈希表 + 滑动窗口
class Solution {
public :
vector< int > findSubstring ( string s, vector< string> & words) {
vector< int > ans;
if ( words. empty ( ) ) return ans;
int len = s. length ( ) , n = words. size ( ) , w = words[ 0 ] . length ( ) , cnt = 0 ;
unordered_map< string, int > tot;
for ( auto & s : words) tot[ s] ++ ;
for ( int i = 0 ; i < w; i++ ) {
cnt = 0 ;
unordered_map< string, int > window;
for ( int j = i; j + w <= len; j += w) {
if ( j >= i + n * w) {
auto word = s. substr ( j - n * w, w) ;
if ( tot[ word] >= window[ word] ) cnt-- ;
window[ word] -- ;
}
auto word = s. substr ( j, w) ;
window[ word] ++ ;
if ( window[ word] <= tot[ word] ) cnt++ ;
if ( cnt == n) ans. push_back ( j - ( n - 1 ) * w) ;
}
}
return ans;
}
} ;