Posts

Showing posts with the label 基本操作

103. Linked List Cycle II

ListNode * detectCycle (ListNode * head) { // write your code here if (head == NULL ){ return head; } ListNode *slow = head; ListNode *fast = head; while (fast != NULL && fast->next != NULL ){ slow = slow->next; fast = fast->next->next; if (slow == fast){ break ; } } if (fast == NULL || fast->next == NULL ){ return NULL ; } while (head != slow){ head = head->next; slow = slow->next; } return head; }

374. Spiral Matrix

class Solution { public : /** * @param matrix: a matrix of m x n elements * @return: an integer list */ vector < int > spiralOrder( vector < vector < int >> &matrix) { // write your code here vector < int > res; const int m = matrix.size(); if (m == 0 ){ return res; } const int n = matrix[ 0 ].size(); int count = 0 ; while (count * 2 < n && count * 2 < m){ for ( int j = count; j < n - count; j++){ res.push_back(matrix[count][j]); } for ( int i = count + 1 ; i < m - count; i++){ res.push_back(matrix[i][n - 1 - count]); } if (m - 2 * count == 1 || n - 2 * count == 1 ){ break ; } for ( int j = n - 2 - count; j >= count; j--){ res.push_back(matrix[m - 1 - count][j]); ...

161. Rotate Image

Code ( Language :C++) ( Judger :cloudjudge-cluster-5) Edit class Solution { public : /** * @param matrix: a lists of integers * @return: nothing */ void rotate ( vector < vector < int >> &matrix) { // write your code here const int n = matrix.size(); if (n <= 1 ){ return ; } int cnt = 0 ; // (n - 1, 0) -> (0, 0); (n - 1, n - 1) -> (n - 1, 0); (0, n - 1) -> (n - 1, n - 1); (0, 0) -> (0, n - 1); while ((cnt + 1 ) * 2 <= n){ for ( int j = cnt; j < n - cnt - 1 ; j++){ //注意这里,不是n - cnt, 一行的最后一个不用处理 int tmp = matrix[cnt][j]; matrix[cnt][j] = matrix[n - 1 - j][cnt]; matrix[n - 1 - j][cnt] = matrix[n - 1 - cnt][n - 1 - j]; matrix[n - 1 - cnt][n - 1 - j] = matrix[j][n - 1 - cnt]; matrix[j][n - 1 - cnt] = tmp; } cnt+...

AM/FB 134. LRU Cache

least recent usage cache  缘起: hash可以实现get和set的O(1)操作,但是如何实现least recent, 也就是需要移动元素,把每次get或set的元素移动到一个地方,也就是 按就近使用排列,最早使用的要在超过cap后去掉。 实现移动的O(1)操作就是要借助doubly linked list。 步骤: 1,建立doubly linked list,里面存放key和value信息。用于实现移动。 2,hash map, key 到 linked list node的映射。用于实现查找。 3,每次get,把key 对应的node移到开头,返回key对应node中的value值。 4,每次set,是否key在hash中?是,更新value,把key对应node移到开头;                                                      否,在hash和doubly list中添加新元素,                                                      如超过cap,去掉tail处,同时对应去掉hash中key。 注意和出错点: 1,这种hash和其他数据结构一起使用时,一定要在添加和删除元素时,同时对 hash和其他数据结构一起操作。 2,linked list node里没有存key信息,导致去掉tail处节点时,无法得到hash中 应该删除的key值。 Code ( Language :C++) ( Judger :ip-...

AM 1534. Convert Binary Search Tree to Sorted Doubly Linked List

迭代和递归 两种本质一样的 iteration class Solution { public : /** * @param root: root of a tree * @return: head node of a doubly linked list */ TreeNode * treeToDoublyList (TreeNode * root) { // Write your code here. std :: stack <TreeNode *> stk; TreeNode *p = root; TreeNode *pre = NULL , *head; while (!stk.empty() || p != NULL ){ while (p != NULL ){ stk.push(p); p = p->left; } TreeNode *cur = stk.top(); stk.pop(); if (pre != NULL ){ pre->right = cur; cur->left = pre; } else { head = cur; } pre = cur; p = cur->right; } head->left = pre; pre->right = head; return head; } }; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...

AM 647. Find All Anagrams in a String

Code ( Language :C++) ( Judger :cloudjudge-cluster-7) Edit class Solution { public : /** * @param s: a string * @param p: a string * @return: a list of index */ vector < int > findAnagrams( string &s, string &p) { // write your code here vector < int > res; vector < int > as( 26 , 0 ); vector < int > ap( 26 , 0 ); const int sizeS = s.size(); const int sizeP = p.size(); if (sizeS < sizeP){ return res; } for ( int i = 0 ; i < sizeP; i++){ as[s[i] - 'a' ]++; ap[p[i] - 'a' ]++; } if (as == ap){ res.push_back( 0 ); } for ( int i = sizeP; i < sizeS; i++){ as[s[i] - 'a' ]++; as[s[i - sizeP] - 'a' ]--; if (as == ap){ res.push_back(i - sizeP + 1 ); } ...

AM 804. Number of Distinct Islands II

Code ( Language :C++) Edit class Solution { public : /** * @param grid: the 2D grid * @return: the number of distinct islands */ int n, m; const vector < int > dx = { -1 , 1 , 0 , 0 }; const vector < int > dy = { 0 , 0 , -1 , 1 }; const int dir = 4 ; typedef pair< int , int > NODE; NODE findRoate ( int x, int y, int type) { // key 5 switch (type){ case 0 : return {y, m - 1 - x}; // 90 case 1 : return {n - 1 - y, x}; // 270; case 2 : return {m - 1 - x, n - 1 - y}; // 180 case 3 : return {x, n - 1 - y}; //left right case 4 : return {m - 1 - x, y}; // up down case 5 : return {x, y}; // orginal } } void dfs ( vector < vector < int >> &grid, int x, int y, vector <NODE> &island) { if (x < 0 || x >= m || y < 0 || y >= n || ...