27. 移除元素
class Solution {
public : int removeElement ( vector< int > & nums, int val) { int slowIndex = 0 ; for ( int fastIndex = 0 ; fastIndex < nums. size ( ) ; fastIndex++ ) { if ( nums[ fastIndex] != val) { nums[ slowIndex] = nums[ fastIndex] ; slowIndex++ ; } } return slowIndex; }
} ;
344. 反转字符串
class Solution {
public : void reverseString ( vector< char > & s) { for ( int i = 0 , j = s. size ( ) - 1 ; i < s. size ( ) / 2 ; i++ , j-- ) { swap ( s[ i] , s[ j] ) ; } }
} ;
剑指 Offer 05. 替换空格
class Solution {
public : string replaceSpace ( string s) { int count = 0 ; int oldSize = s. size ( ) ; for ( int i = 0 ; i < s. size ( ) ; i++ ) { if ( s[ i] == ' ' ) { count++ ; } } s. resize ( oldSize+ 2 * count) ; int newSize = s. size ( ) ; for ( int i = oldSize- 1 , j = newSize- 1 ; i>= 0 ; i-- , j-- ) { if ( s[ i] == ' ' ) { s[ j] = '0' ; s[ j- 1 ] = '2' ; s[ j- 2 ] = '%' ; j = j- 2 ; } else { s[ j] = s[ i] ; } } return s; }
} ;
151. 翻转字符串里的单词
class Solution {
public : void removespacing ( string & s) { int slowIndex = 0 , fastIndex = 0 ; while ( s[ fastIndex] == ' ' ) { fastIndex++ ; } for ( ; fastIndex < s. size ( ) ; fastIndex++ ) { if ( fastIndex- 1 > 0 && s[ fastIndex- 1 ] == s[ fastIndex] && s[ fastIndex] == ' ' ) { continue ; } else { s[ slowIndex] = s[ fastIndex] ; slowIndex++ ; } } if ( slowIndex- 1 > 0 && s[ slowIndex - 1 ] == ' ' ) { s. resize ( slowIndex- 1 ) ; } else { s. resize ( slowIndex) ; } } void reverseStr ( string & s, int start, int end) { for ( int i = start, j = end; i < j; i++ , j-- ) { swap ( s[ i] , s[ j] ) ; } } string reverseWords ( string s) { removespacing ( s) ; reverseStr ( s, 0 , s. size ( ) - 1 ) ; for ( int i = 0 ; i < s. size ( ) ; i++ ) { int j = i; while ( j < s. size ( ) && s[ j] != ' ' ) { j++ ; } reverseStr ( s, i, j- 1 ) ; i = j; } return s; }
} ;
206. 反转链表
class Solution {
public : ListNode* reverseList ( ListNode* head) { ListNode* temp; ListNode* cur = head; ListNode* pre = NULL ; while ( cur) { temp = cur-> next; cur-> next = pre; pre = cur; cur = temp; } return pre; }
} ;
19. 删除链表的倒数第 N 个结点
class Solution {
public : ListNode* removeNthFromEnd ( ListNode* head, int n) { ListNode* fakeNode = new ListNode ( 0 ) ; fakeNode-> next = head; ListNode* slowNode = fakeNode; ListNode* fastNode = fakeNode; while ( n-- && fastNode != NULL ) { fastNode = fastNode-> next; } fastNode = fastNode-> next; while ( fastNode != NULL ) { fastNode = fastNode-> next; slowNode = slowNode-> next; } slowNode-> next = slowNode-> next-> next; return fakeNode-> next; }
} ;
18. 四数之和
class Solution {
public : vector< vector< int >> fourSum ( vector< int > & nums, int target) { vector< vector< int > > ans; vector< int > v; sort ( nums. begin ( ) , nums. end ( ) ) ; for ( int i = 0 ; i < nums. size ( ) ; i++ ) { if ( i > 0 && nums[ i- 1 ] == nums[ i] ) { continue ; } for ( int j = i+ 1 ; j < nums. size ( ) ; j++ ) { if ( j- 1 > i && nums[ j- 1 ] == nums[ j] ) { continue ; } int left = j+ 1 ; int right = nums. size ( ) - 1 ; while ( left < right) { if ( nums[ i] + nums[ j] > target - ( nums[ left] + nums[ right] ) ) right-- ; else if ( nums[ i] + nums[ j] < target - ( nums[ left] + nums[ right] ) ) left++ ; else { v. push_back ( nums[ i] ) ; v. push_back ( nums[ j] ) ; v. push_back ( nums[ left] ) ; v. push_back ( nums[ right] ) ; ans. push_back ( v) ; while ( left < right && nums[ right- 1 ] == nums[ right] ) right-- ; while ( left > right && nums[ left+ 1 ] == nums[ left] ) left++ ; right-- ; left++ ; v. clear ( ) ; } } } } return ans; }
} ;
15. 三数之和
class Solution {
public : vector< vector< int >> threeSum ( vector< int > & nums) { vector< vector< int > > ans; vector< int > v; sort ( nums. begin ( ) , nums. end ( ) ) ; for ( int i = 0 ; i < nums. size ( ) ; i++ ) { if ( nums[ i] > 0 ) { return ans; } if ( i > 0 && nums[ i] == nums[ i- 1 ] ) { continue ; } int left = i+ 1 ; int right = nums. size ( ) - 1 ; while ( left < right) { int sum = nums[ i] + nums[ left] + nums[ right] ; if ( sum > 0 ) right-- ; else if ( sum < 0 ) left++ ; else { v. push_back ( nums[ i] ) ; v. push_back ( nums[ left] ) ; v. push_back ( nums[ right] ) ; ans. push_back ( v) ; while ( left < right && nums[ right- 1 ] == nums[ right] ) right-- ; while ( left < right && nums[ left+ 1 ] == nums[ left] ) left++ ; right-- ; left++ ; v. clear ( ) ; } } } return ans; }
} ;
142. 环形链表 II
class Solution {
public : ListNode * detectCycle ( ListNode * head) { ListNode* slowNode = head; ListNode* fastNode = head; while ( fastNode != NULL && fastNode-> next != NULL ) { fastNode = fastNode-> next-> next; slowNode = slowNode-> next; if ( slowNode == fastNode) { ListNode* temp1 = fastNode; ListNode* temp2 = head; while ( temp1 != temp2) { temp1 = temp1-> next; temp2 = temp2-> next; } return temp1; } } return NULL ; }
} ;
面试题 02.07. 链表相交
class Solution {
public : ListNode * getIntersectionNode ( ListNode * headA, ListNode * headB) { ListNode* node1 = headA; ListNode* node2 = headB; int lenA = 0 ; int lenB = 0 ; while ( node1 != NULL ) { node1 = node1-> next; lenA++ ; } while ( node2 != NULL ) { node2 = node2-> next; lenB++ ; } node1 = headA; node2 = headB; if ( lenB > lenA) { swap ( lenA, lenB) ; swap ( node1, node2) ; } int poor = lenA - lenB; while ( poor-- ) { node1 = node1-> next; } while ( node1 != NULL ) { if ( node1 == node2) { return node1; } node1 = node1-> next; node2 = node2-> next; } return NULL ; }
} ;