f
合并两个有序数组
这个思路很重要,代码也不算复杂,利用双指针进行更新
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 vector<int > mearge (vector<int > va, vector<int > vb) { vector <int > res; int sa = va.size (); int sb = vb.size (); int i=0 ,j=0 ; for (;i!=sa && j!=sb;) { if (va[i]<vb[j]){ res.push_back (va[i++]); }else { res.push_back (vb[j++]); } } while (i!=sa) res.push_back (va[i++]); while (j!=sb) res.push_back (vb[j++]); return res; }
三元组
寻找一组三元组 使其互相绝对值差最小,数学推导关系,得出这组数最大的差值的二倍即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include <iostream> using namespace std;typedef long long LL;const int N = 100010 ;int a[N], b[N], c[N];int main () { int l, m, n; cin >> l >> m >> n; for (int i = 0 ; i < l; i ++) cin >> a[i]; for (int i = 0 ; i < m; i ++) cin >> b[i]; for (int i = 0 ; i < n; i ++) cin >> c[i]; LL res = 1e18 ; int i = 0 , j = 0 , k = 0 ; while (i < l && j < m && k < n) { int x = a[i], y = b[j], z = c[k]; res = min (res, (LL)max (max (x, y), z) - min (min (x, y), z)); if (x <= y && x <= z) i ++; else if (y <= x && y <= z) j ++; else k ++; } cout << res * 2 << endl; return 0 ; }
顺序表
需要注意P15页中,静态分配和动态分配的区别,动态分配是在程序执行过程中通过动态存储分配语句分配的
1 2 L.data = new int [InitSize]; L.data = (int * )malloc (sizeof (int *) InitSize);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 #include <bits/stdc++.h> using namespace std;struct List { int *arr; int length; };List creastList () { List list; list.length = 0 ; list.arr = new int [100 ]; return list; }bool insert (List &list, int index,int value) { if (list.length==100 ) return false ; for (int i = list.length-1 ;i>=index;i--) { list.arr[i + 1 ] = list.arr[i]; } list.arr[index] = value; list.length ++ ; return true ; }int print (List list) { for (int i = 0 ;i<list.length;i++) { cout << list.arr[i] << " " ; } cout <<"\n" ; }int main () { List list = creastList (); insert (list,0 ,2 ); insert (list,1 ,5 ); insert (list,5 ,18 ); insert (list,2 ,9 ); print (list); return 0 ; }
链表
链表除了几个操作外 需要注意的是还有头插法,尾插法,以及带虚拟头节点和不带的,需要熟练里面的逻辑和操作差异
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 #include <bits/stdc++.h> using namespace std;struct Node { int val; Node *next; Node (int x): val (x),next (NULL ){} };Node* CreateListByHead (int arr[], int n) { Node *Lhead = NULL ; for (int i = 0 ;i<n;i++) { Node *node = new Node (arr[i]); if (Lhead == NULL ) { Lhead = node; }else { node->next=Lhead; Lhead=node; } } return Lhead; }Node *CreateListByHeadWithHead (int arr[],int n) { Node *Lhead = new Node (-1 ); Lhead -> next = NULL ; for (int i = 0 ;i<n;i++) { Node *node = new Node (arr[i]); node->next = Lhead->next; Lhead->next = node; } return Lhead; }Node *CreateListByTail (int arr[],int n) { Node *Lhead = NULL ; Node *Ltail = NULL ; for (int i = 0 ;i<n;i++) { Node *node = new Node (arr[i]); if (Lhead==NULL ) { Lhead = node; Ltail = node; }else { Ltail->next = node; Ltail = node; } } return Lhead; }Node *CreateListByTailWithHand (int arr[],int n) { Node *Lhead = new Node (-1 ); Node *Ltail = Lhead; for (int i = 0 ;i<n;i++) { Node *node = new Node (arr[i]); Ltail->next = node; Ltail = node; } return Lhead; }void print (Node *Lhead) { Node* p = Lhead; cout << p->val<<" " ; while (p->next != NULL ) { p = p->next; cout << p->val<<" " ; } }int main () { int arr[] = {1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 }; Node *Lhead = CreateListByTailWithHand (arr,9 ); print (Lhead); return 0 ; }