diff --git a/c-cpp/06_linkedlist/singlelist_gc/singleList.c b/c-cpp/06_linkedlist/singlelist_gc/singleList.c new file mode 100644 index 00000000..e7c00a93 --- /dev/null +++ b/c-cpp/06_linkedlist/singlelist_gc/singleList.c @@ -0,0 +1,281 @@ +#include "singleList.h" + +#include + +linkedList * listCreate() +{ + linkedList *list = NULL; + list = malloc(sizeof(*list)); + if (NULL == list) + { + return NULL; + } + + list->dup = NULL; + list->free = NULL; + list->match = NULL; + + list->head = NULL; + list->len = 0; + + return list; +} + +// 释放 +void listRelease(linkedList *list) +{ + if (NULL == list) + { + return; + } + + listEmpty(list); + + free(list); + list = NULL; +} + +void listEmpty(linkedList *list) +{ + if (NULL == list) + { + return; + } + + while (NULL != list->head) + { + listNode *pNode = list->head; + list->head = pNode->next; + if (NULL != list->free) + { + list->free(pNode->value); + } + else + { + free(pNode->value); + } + + pNode->next = NULL; + free(pNode); + pNode = NULL; + } +} + +linkedList * listAddNodeHead(linkedList *list, void * value) +{ + if (NULL == list || NULL == value) + { + return list; + } + + listNode *node = NULL; + node = malloc(sizeof(*node)); + if (NULL == node) + { + return list; + } + + node->value = value; + node->next = list->head; + list->head = node; + + ++list->len; + return list; +} + +linkedList * listAddNodeTail(linkedList *list, void *value) +{ + if (NULL == list || NULL == value) + { + return list; + } + + listNode *node = NULL; + node = malloc(sizeof(*node)); + if (NULL == node) + { + return list; + } + + node->value = value; + node->next = NULL; + + if (NULL == list->head + && list->len == 0) + { + list->head = node; + } + else + { + listNode *tail = list->head; + listNode *pre = list->head; + while (NULL != tail) + { + pre = tail; + tail = tail->next; + } + + pre->next = node; + } + + ++list->len; + return list; +} + +linkedList * listInsertNode(linkedList *list, listNode *old_node, void *value, bool after) +{ + if (NULL == list || NULL == old_node) + { + return list; + } + + listNode *pNode = NULL; + pNode = malloc(sizeof(*pNode)); + if (NULL == pNode) + { + return list; + } + + pNode->value = value; + if (after) + { + pNode->next = old_node->next; + old_node->next = pNode; + } + else + { + listNode *pre = list->head; + while (pre->next != old_node) + { + pre = pre->next; + } + + if (NULL != pre) + { + pre->next = pNode; + pNode->next = old_node; + } + } + + ++list->len; + return list; +} + +// 没设置释放函数时不做释放处理 +void listDelNode(linkedList *list, listNode *node) +{ + if (NULL == list || NULL == node) + { + return; + } + + listNode *pre = list->head; + listNode *cur = list->head; + while (NULL != cur && cur != node) + { + pre = cur; + cur = cur->next; + } + + // 不在该链表中 + if (NULL == pre) + { + return; + } + + pre->next = node->next; + node->next = NULL; + --list->len; + + if (NULL != list->free) + { + list->free(node->value); + free(node); + node = NULL; + } +} + +listNode * listSearchKey(linkedList *list, void *key) +{ + if (NULL == list) + { + return NULL; + } + + listNode *node = list->head; + while (NULL != node) + { + if (NULL != list->match) + { + if (list->match(key, node->value) == 0) + { + return node; + } + } + else + { + if (key == node->value) + { + return node; + } + } + + node = node->next; + } + + return NULL; +} + +listNode * listIndex(linkedList *list, long index) +{ + if (NULL == list) + { + return NULL; + } + + if (index <= 0 + || index > list->len) + { + return NULL; + } + + listNode *pNode = list->head; + for (long i = 0; i < index; ++i) + { + pNode = pNode->next; + } + + return pNode; +} + +linkedList* listRewind(linkedList *list) +{ + if (NULL == list) + { + return NULL; + } + + listNode *head = list->head; + listNode *pre = NULL; + listNode *next = NULL; + while (NULL != head) + { + next = head->next; + head->next = pre; + pre = head; + head = next; + } + + list->head = pre; + return list; +} + +size_t listLength(linkedList *list) +{ + if (NULL == list) + { + return 0; + } + + return list->len; +} \ No newline at end of file diff --git a/c-cpp/06_linkedlist/singlelist_gc/singleList.h b/c-cpp/06_linkedlist/singlelist_gc/singleList.h new file mode 100644 index 00000000..6617dbf7 --- /dev/null +++ b/c-cpp/06_linkedlist/singlelist_gc/singleList.h @@ -0,0 +1,46 @@ +#ifndef __SINGLELIST_H__ +#define __SINGLELIST_H__ + +#include +#include + +typedef struct listNode +{ + struct listNode *next; + void *value; +}listNode; + +typedef struct linkedList +{ + listNode *head; + size_t len; + size_t typesize; + + void(*dup)(void*, void*); + int(*match)(void*, void*); + void(*free)(void*); +}linkedList; + +#define listSetDupMethod(l,m) ((l)->dup = (m)) +#define listSetFreeMethod(l,m) ((l)->free = (m)) +#define listSetMatchMethod(l,m) ((l)->match = (m)) + +#define listGetDupMethod(l) ((l)->dup) +#define listGetFree(l) ((l)->free) +#define listGetMatchMethod(l) ((l)->match) + +linkedList *listCreate(); +void listRelease(linkedList *list); +void listEmpty(linkedList *list); +linkedList *listAddNodeHead(linkedList *list, void *value); +linkedList *listAddNodeTail(linkedList *list, void *value); +linkedList *listInsertNode(linkedList *list, listNode *old_node, void *value, bool after); +void listDelNode(linkedList *list, listNode *node); + +listNode *listSearchKey(linkedList *list, void *key); +listNode *listIndex(linkedList *list, long index); +linkedList* listRewind(linkedList *list); + +size_t listLength(linkedList *list); + +#endif // !__SINGLELIST_H__ diff --git a/java/11_sorts/Sorts.java b/java/11_sorts/Sorts.java index aeda6f46..e55efd7d 100644 --- a/java/11_sorts/Sorts.java +++ b/java/11_sorts/Sorts.java @@ -49,18 +49,31 @@ public static void insertionSort(int[] a, int n) { // 閫夋嫨鎺掑簭锛宎琛ㄧず鏁扮粍锛宯琛ㄧず鏁扮粍澶у皬 public static void selectionSort(int[] a, int n) { if (n <= 1) return; +<<<<<<< HEAD + for (int i = 0; i < n; ++i) { + // 鏌ユ壘鏈灏忓 + int minIndex = i; + int minValue = a[i]; + for (int j = i; j < n; ++j) { + if (a[j] < minValue) { + minValue = a[j]; +======= for (int i = 0; i < n - 1; ++i) { // 鏌ユ壘鏈灏忓 int minIndex = i; for (int j = i + 1; j < n; ++j) { if (a[j] < a[minIndex]) { +>>>>>>> upstream/master minIndex = j; } } +<<<<<<< HEAD +======= if (minIndex == i) continue; +>>>>>>> upstream/master // 浜ゆ崲 int tmp = a[i]; a[i] = a[minIndex]; diff --git a/notes/11_sorts/readme.md b/notes/11_sorts/readme.md index 576668f3..b23cb963 100644 --- a/notes/11_sorts/readme.md +++ b/notes/11_sorts/readme.md @@ -1,100 +1,100 @@ -# 鎺掑簭锛堜笂锛 - -| 鎺掑簭绠楁硶 | 鏃堕棿澶嶆潅搴 | 鏄惁鍩轰簬姣旇緝 | -|---------|----|----| -| 鍐掓场銆佹彃鍏ャ侀夋嫨 | $O(n^2)$ | [y] | -| 蹇帓銆佸綊骞 | $O(n\log n)$ | [y] | -| 妗躲佸熀鏁般佽鏁 | $O(n) | [x] | - -寮绡囬棶棰橈細鎻掑叆鎺掑簭鍜屽啋娉℃帓搴忕殑鏃堕棿澶嶆潅搴︾浉鍚岋紝閮芥槸 $O(n^2)$锛屽湪瀹為檯杞欢寮鍙戜腑锛屼负浠涔堟垜浠洿鍊惧悜浜庝娇鐢ㄦ彃鍏ユ帓搴忚屼笉鏄啋娉℃帓搴忥紵 - -## 濡備綍鍒嗘瀽銆屾帓搴忕畻娉曘嶏紵 - -### 绠楁硶鎵ц鏁堢巼 - -1. 鏈濂姐佹渶鍧忋佸钩鍧囨儏鍐电殑鏃堕棿澶嶆潅搴 -2. 鏃堕棿澶嶆潅搴︾殑绯绘暟銆佷綆闃躲佸父鏁扳斺斿湪娓愯繘澶嶆潅搴︾浉鍚岀殑鎯呭喌涓嬶紝闇瑕佹瘮杈冪郴鏁般佷綆闃跺拰甯告暟 -3. 姣旇緝鍜屼氦鎹紙绉诲姩锛夌殑娆℃暟鈥斺斿熀浜庢瘮杈冪殑鎺掑簭绠楁硶鐨勪袱绉嶅熀鏈搷浣 - -### 绠楁硶鐨勫唴瀛樻秷鑰 - -鏄惁涓哄師鍦版帓搴忕畻娉曪紙In-place sort algorithm锛夛紝鍗崇畻娉曠殑绌洪棿澶嶆潅搴︽槸鍚︿负 $O(1)$銆 - -### 鎺掑簭鐨勭ǔ瀹氭 - -缁忚繃鎺掑簭绠楁硶澶勭悊鍚庯紝鍊肩浉鍚岀殑鍏冪礌锛屽湪鍘熷簭鍒楀拰鎺掑簭鍚庡簭鍒椾腑鐨勭浉瀵逛綅缃繚鎸佷笉鍙橈紝鍒欑О璇ユ帓搴忕畻娉曟槸绋冲畾鐨勩 - -> 寰呮帓搴忕殑 `item` 骞朵笉鏄畝鍗曠殑鍊硷紝鑰屾槸涓涓熀浜庡璞′腑鐨勬煇涓 `key` 杩涜鎺掑簭鏃讹紝鎺掑簭鐨勭ǔ瀹氭у氨鏈夋剰涔変簡銆 - -## 鍐掓场鎺掑簭 - -* 姣忔寰幆閮戒粠搴忓垪璧峰浣嶇疆寮濮 -* 寰幆涓殑姣忎釜鍔ㄤ綔锛岄兘瀵规瘮鐩搁偦涓や釜鍏冪礌鐨勫ぇ灏忔槸鍚︽弧瓒冲亸搴忚姹傦紝鑻ヤ笉婊¤冻锛屽垯浜ゆ崲椤哄簭 - -![鍐掓场鎺掑簭渚嬪浘](https://static001.geekbang.org/resource/image/88/34/8890cbf63ea80455ce82490a23361134.jpg) - -鍒嗘瀽锛 - -* 鍘熷湴鎺掑簭 -* 绋冲畾鎺掑簭锛堝亸搴忓叧绯绘槸涓ユ牸鐨勫亸搴忓叧绯伙紝濡 `<` 鎴 `>`锛 -* 鏃堕棿澶嶆潅搴 - * 鏈濂 $O(n)$ - * 鏈鍧 $O(n^2)$ - * 骞冲潎 $O(n^2)$ - -### 鍐掓场鎺掑簭鐨勫钩鍧囨椂闂村鏉傚害闈炰弗鏍煎垎鏋 - -* 鏈夊簭搴︼細搴忓垪涓弧瓒冲亸搴忓叧绯荤殑涓や袱缁勫悎鐨勫厓绱犲鐨勪釜鏁 -* 婊℃湁搴忓害锛氭帓搴忓畬鎴愮殑搴忓垪鐨勬湁搴忓害锛屽畠绛変簬 $n(n - 1) / 2$ -* 閫嗗簭搴︼細搴忓垪涓笉婊¤冻鍋忓簭鍏崇郴鐨勪寒浜粍鍚堢殑鍏冪礌瀵圭殑涓暟 - -鏄剧劧锛$\text{閫嗗簭搴 = \text{婊℃湁搴忓害} - \text{鏈夊簭搴$銆 - -鍦ㄥ啋娉℃帓搴忎腑锛屾瘡浜х敓涓娆°屼氦鎹€嶆搷浣滐紝$\text{閫嗗簭搴--$銆備簬鏄紝骞冲潎鎯呭喌涓嬶紝闇瑕 $n(n - 1)/4$ 娆′氦鎹㈡搷浣滐紝瀹冨凡缁忔槸 $O(n^2)$ 浜嗐傚洜姝わ紝灏界姣旇緝鎿嶄綔鐨勬暟閲忎細澶т簬浜ゆ崲鎿嶄綔鐨勬暟閲忥紝浣嗘垜浠緷鐒惰兘璇达紝鍐掓场鎺掑簭鐨勫钩鍧囨椂闂村鏉傚害鏄 $O(n^2)$銆 - -> 鍒嗘瀽杩囩▼涓嶄弗鏍硷紝浣嗚冻澶熻鏄庨棶棰樸 - -## 鎻掑叆鎺掑簭 - -1. 灏嗗緟鎺掑簭鏁板垪鍒嗕负宸叉帓搴忓尯闂村拰鏈帓搴忓尯闂 -2. 鍙栨湭鎺掑簭鍖洪棿鐨勭涓涓厓绱 -3. 閬嶅巻宸叉帓搴忓尯闂达紝鎸夌収鍋忓簭鍏崇郴锛屽鎵惧悎閫傜殑浣嶇疆锛屾彃鍏ユ湭鎺掑簭鍖洪棿鐨勭涓涓厓绱 -4. 閲嶅 2 -- 3 鐩磋嚦鏈帓搴忓尯闂撮暱搴︿负闆 - -![鎻掑叆鎺掑簭渚嬪浘](https://static001.geekbang.org/resource/image/fd/01/fd6582d5e5927173ee35d7cc74d9c401.jpg) - -鍒嗘瀽锛 - -* 鍘熷湴鎺掑簭 -* 绋冲畾鎺掑簭锛堝肩浉鍚岀殑鍏冪礌锛屽線鍚庢彃锛 -* 鏃堕棿澶嶆潅搴 - * 鏈濂 $O(n)$ - * 鏈鍧 $O(n^2)$ - * 骞冲潎 $O(n^2)$锛堜箻娉曟硶鍒欙級 - -## 閫夋嫨鎺掑簭 - -1. 灏嗗緟鎺掑簭鏁板垪鍒嗕负宸叉帓搴忓尯闂村拰鏈帓搴忓尯闂 -2. 閬嶅巻鏈帓搴忓尯闂达紝鍙栨湭鎺掑簭鍖洪棿鐨勬渶灏忓厓绱 -3. 浜ゆ崲涓婅堪鏈灏忓厓绱犱笌鏈帓搴忓尯闂翠腑鐨勭涓涓厓绱犵殑浣嶇疆 -4. 閲嶅 2 -- 3 鐩磋嚦鏈帓搴忓尯闂撮暱搴︿负闆 - -![閫夋嫨鎺掑簭渚嬪浘](https://static001.geekbang.org/resource/image/32/1d/32371475a0b08f0db9861d102474181d.jpg) - -鍒嗘瀽锛 - -* 闈炲師鍦版帓搴 -* 闈炵ǔ瀹氭帓搴 -* 鏃堕棿澶嶆潅搴 - * 鏈濂 $O(n^2)$ - * 鏈鍧 $O(n^2)$ - * 骞冲潎 $O(n^2)$锛堜箻娉曟硶鍒欙級 - -## 寮绡囬棶棰 - -* 瀵瑰悓涓浠芥湭鎺掑簭搴忓垪鏁版嵁锛屽啋娉℃帓搴忓拰鎻掑叆鎺掑簭鎵闇鐨勪氦鎹紙绉诲姩锛夋鏁版槸涓瀹氱殑锛屼笖鏄浉绛夌殑 -* 鍗曟鏁版嵁浜ゆ崲锛屽啋娉℃帓搴忔墍闇鐨勬椂闂存洿闀匡紙涓夋璧嬪兼搷浣滐紝鎻掓帓鍙渶瑕佷竴娆★級 - -鍙︽湁鎻掑叆鎺掑簭鐨勪紭鍖栫増鏈琜甯屽皵鎺掑簭](https://zh.wikipedia.org/wiki/%E5%B8%8C%E5%B0%94%E6%8E%92%E5%BA%8F)銆 - -![灏忕粨](https://static001.geekbang.org/resource/image/34/50/348604caaf0a1b1d7fee0512822f0e50.jpg) +# 鎺掑簭锛堜笂锛 + +| 鎺掑簭绠楁硶 | 鏃堕棿澶嶆潅搴 | 鏄惁鍩轰簬姣旇緝 | +|---------|----|----| +| 鍐掓场銆佹彃鍏ャ侀夋嫨 | $O(n^2)$ | [y] | +| 蹇帓銆佸綊骞 | $O(n\log n)$ | [y] | +| 妗躲佸熀鏁般佽鏁 | $O(n) | [x] | + +寮绡囬棶棰橈細鎻掑叆鎺掑簭鍜屽啋娉℃帓搴忕殑鏃堕棿澶嶆潅搴︾浉鍚岋紝閮芥槸 $O(n^2)$锛屽湪瀹為檯杞欢寮鍙戜腑锛屼负浠涔堟垜浠洿鍊惧悜浜庝娇鐢ㄦ彃鍏ユ帓搴忚屼笉鏄啋娉℃帓搴忥紵 + +## 濡備綍鍒嗘瀽銆屾帓搴忕畻娉曘嶏紵 + +### 绠楁硶鎵ц鏁堢巼 + +1. 鏈濂姐佹渶鍧忋佸钩鍧囨儏鍐电殑鏃堕棿澶嶆潅搴 +2. 鏃堕棿澶嶆潅搴︾殑绯绘暟銆佷綆闃躲佸父鏁扳斺斿湪娓愯繘澶嶆潅搴︾浉鍚岀殑鎯呭喌涓嬶紝闇瑕佹瘮杈冪郴鏁般佷綆闃跺拰甯告暟 +3. 姣旇緝鍜屼氦鎹紙绉诲姩锛夌殑娆℃暟鈥斺斿熀浜庢瘮杈冪殑鎺掑簭绠楁硶鐨勪袱绉嶅熀鏈搷浣 + +### 绠楁硶鐨勫唴瀛樻秷鑰 + +鏄惁涓哄師鍦版帓搴忕畻娉曪紙In-place sort algorithm锛夛紝鍗崇畻娉曠殑绌洪棿澶嶆潅搴︽槸鍚︿负 $O(1)$銆 + +### 鎺掑簭鐨勭ǔ瀹氭 + +缁忚繃鎺掑簭绠楁硶澶勭悊鍚庯紝鍊肩浉鍚岀殑鍏冪礌锛屽湪鍘熷簭鍒楀拰鎺掑簭鍚庡簭鍒椾腑鐨勭浉瀵逛綅缃繚鎸佷笉鍙橈紝鍒欑О璇ユ帓搴忕畻娉曟槸绋冲畾鐨勩 + +> 寰呮帓搴忕殑 `item` 骞朵笉鏄畝鍗曠殑鍊硷紝鑰屾槸涓涓熀浜庡璞′腑鐨勬煇涓 `key` 杩涜鎺掑簭鏃讹紝鎺掑簭鐨勭ǔ瀹氭у氨鏈夋剰涔変簡銆 + +## 鍐掓场鎺掑簭 + +* 姣忔寰幆閮戒粠搴忓垪璧峰浣嶇疆寮濮 +* 寰幆涓殑姣忎釜鍔ㄤ綔锛岄兘瀵规瘮鐩搁偦涓や釜鍏冪礌鐨勫ぇ灏忔槸鍚︽弧瓒冲亸搴忚姹傦紝鑻ヤ笉婊¤冻锛屽垯浜ゆ崲椤哄簭 + +![鍐掓场鎺掑簭渚嬪浘](https://static001.geekbang.org/resource/image/88/34/8890cbf63ea80455ce82490a23361134.jpg) + +鍒嗘瀽锛 + +* 鍘熷湴鎺掑簭 +* 绋冲畾鎺掑簭锛堝亸搴忓叧绯绘槸涓ユ牸鐨勫亸搴忓叧绯伙紝濡 `<` 鎴 `>`锛 +* 鏃堕棿澶嶆潅搴 + * 鏈濂 $O(n)$ + * 鏈鍧 $O(n^2)$ + * 骞冲潎 $O(n^2)$ + +### 鍐掓场鎺掑簭鐨勫钩鍧囨椂闂村鏉傚害闈炰弗鏍煎垎鏋 + +* 鏈夊簭搴︼細搴忓垪涓弧瓒冲亸搴忓叧绯荤殑涓や袱缁勫悎鐨勫厓绱犲鐨勪釜鏁 +* 婊℃湁搴忓害锛氭帓搴忓畬鎴愮殑搴忓垪鐨勬湁搴忓害锛屽畠绛変簬 $n(n - 1) / 2$ +* 閫嗗簭搴︼細搴忓垪涓笉婊¤冻鍋忓簭鍏崇郴鐨勪寒浜粍鍚堢殑鍏冪礌瀵圭殑涓暟 + +鏄剧劧锛$\text{閫嗗簭搴 = \text{婊℃湁搴忓害} - \text{鏈夊簭搴$銆 + +鍦ㄥ啋娉℃帓搴忎腑锛屾瘡浜х敓涓娆°屼氦鎹€嶆搷浣滐紝$\text{閫嗗簭搴--$銆備簬鏄紝骞冲潎鎯呭喌涓嬶紝闇瑕 $n(n - 1)/4$ 娆′氦鎹㈡搷浣滐紝瀹冨凡缁忔槸 $O(n^2)$ 浜嗐傚洜姝わ紝灏界姣旇緝鎿嶄綔鐨勬暟閲忎細澶т簬浜ゆ崲鎿嶄綔鐨勬暟閲忥紝浣嗘垜浠緷鐒惰兘璇达紝鍐掓场鎺掑簭鐨勫钩鍧囨椂闂村鏉傚害鏄 $O(n^2)$銆 + +> 鍒嗘瀽杩囩▼涓嶄弗鏍硷紝浣嗚冻澶熻鏄庨棶棰樸 + +## 鎻掑叆鎺掑簭 + +1. 灏嗗緟鎺掑簭鏁板垪鍒嗕负宸叉帓搴忓尯闂村拰鏈帓搴忓尯闂 +2. 鍙栨湭鎺掑簭鍖洪棿鐨勭涓涓厓绱 +3. 閬嶅巻宸叉帓搴忓尯闂达紝鎸夌収鍋忓簭鍏崇郴锛屽鎵惧悎閫傜殑浣嶇疆锛屾彃鍏ユ湭鎺掑簭鍖洪棿鐨勭涓涓厓绱 +4. 閲嶅 2 -- 3 鐩磋嚦鏈帓搴忓尯闂撮暱搴︿负闆 + +![鎻掑叆鎺掑簭渚嬪浘](https://static001.geekbang.org/resource/image/fd/01/fd6582d5e5927173ee35d7cc74d9c401.jpg) + +鍒嗘瀽锛 + +* 鍘熷湴鎺掑簭 +* 绋冲畾鎺掑簭锛堝肩浉鍚岀殑鍏冪礌锛屽線鍚庢彃锛 +* 鏃堕棿澶嶆潅搴 + * 鏈濂 $O(n)$ + * 鏈鍧 $O(n^2)$ + * 骞冲潎 $O(n^2)$锛堜箻娉曟硶鍒欙級 + +## 閫夋嫨鎺掑簭 + +1. 灏嗗緟鎺掑簭鏁板垪鍒嗕负宸叉帓搴忓尯闂村拰鏈帓搴忓尯闂 +2. 閬嶅巻鏈帓搴忓尯闂达紝鍙栨湭鎺掑簭鍖洪棿鐨勬渶灏忓厓绱 +3. 浜ゆ崲涓婅堪鏈灏忓厓绱犱笌鏈帓搴忓尯闂翠腑鐨勭涓涓厓绱犵殑浣嶇疆 +4. 閲嶅 2 -- 3 鐩磋嚦鏈帓搴忓尯闂撮暱搴︿负闆 + +![閫夋嫨鎺掑簭渚嬪浘](https://static001.geekbang.org/resource/image/32/1d/32371475a0b08f0db9861d102474181d.jpg) + +鍒嗘瀽锛 + +* 闈炲師鍦版帓搴 +* 闈炵ǔ瀹氭帓搴 +* 鏃堕棿澶嶆潅搴 + * 鏈濂 $O(n^2)$ + * 鏈鍧 $O(n^2)$ + * 骞冲潎 $O(n^2)$锛堜箻娉曟硶鍒欙級 + +## 寮绡囬棶棰 + +* 瀵瑰悓涓浠芥湭鎺掑簭搴忓垪鏁版嵁锛屽啋娉℃帓搴忓拰鎻掑叆鎺掑簭鎵闇鐨勪氦鎹紙绉诲姩锛夋鏁版槸涓瀹氱殑锛屼笖鏄浉绛夌殑 +* 鍗曟鏁版嵁浜ゆ崲锛屽啋娉℃帓搴忔墍闇鐨勬椂闂存洿闀匡紙涓夋璧嬪兼搷浣滐紝鎻掓帓鍙渶瑕佷竴娆★級 + +鍙︽湁鎻掑叆鎺掑簭鐨勪紭鍖栫増鏈琜甯屽皵鎺掑簭](https://zh.wikipedia.org/wiki/%E5%B8%8C%E5%B0%94%E6%8E%92%E5%BA%8F)銆 + +![灏忕粨](https://static001.geekbang.org/resource/image/34/50/348604caaf0a1b1d7fee0512822f0e50.jpg) diff --git a/php/08_stack/Compute.php b/php/08_stack/Compute.php index d9b8eb42..7e0a3379 100644 --- a/php/08_stack/Compute.php +++ b/php/08_stack/Compute.php @@ -28,6 +28,8 @@ function expression($str) array_push($operStack, $arr[$i]); break; case '*': +<<<<<<< HEAD +======= $arrLen = count($operStack); while ($operStack[$arrLen-1] === '/'){ compute($numStack, $operStack); @@ -36,6 +38,7 @@ function expression($str) array_push($operStack, $arr[$i]); break; +>>>>>>> upstream/master case '/': case '(': array_push($operStack, $arr[$i]); @@ -78,9 +81,13 @@ function compute(&$numStack, &$operStack){ case '-': array_push($numStack, array_pop($numStack) - $num); break; +<<<<<<< HEAD + +======= case '(': throw new \Exception("涓嶅尮閰嶇殑(", 2); break; +>>>>>>> upstream/master } } expression('-1+2-(1+2*3)'); diff --git a/php/README.md b/php/README.md index c702546d..6e3a2773 100644 --- a/php/README.md +++ b/php/README.md @@ -17,7 +17,11 @@ * findMiddleNode 姹傞摼琛ㄧ殑涓棿缁撶偣 #### 08_stack +<<<<<<< HEAD +* 閾炬爤瀹炵幇 +======= * 閾炬爤瀹炵幇 #### 09_stack -* 闃熷垪閾捐〃瀹炵幇 \ No newline at end of file +* 闃熷垪閾捐〃瀹炵幇 +>>>>>>> upstream/master diff --git a/php/Stack/Compute.php b/php/Stack/Compute.php new file mode 100644 index 00000000..3e5624bd --- /dev/null +++ b/php/Stack/Compute.php @@ -0,0 +1,78 @@ += 48 && ord($arr[$i] <= 57)){ + array_push($numStack, $arr[$i]); + continue; + } + switch ($arr[$i]){ + case '+': + case '-': + $arrLen = count($operStack); + while ($operStack[$arrLen-1] === '*' || $operStack[$arrLen-1] === '/' || $operStack[$arrLen-1] === '-'){ + compute($numStack, $operStack); + $arrLen--; + } + array_push($operStack, $arr[$i]); + break; + case '*': + case '/': + case '(': + array_push($operStack, $arr[$i]); + break; + case ')': + $arrLen = count($operStack); + while ($operStack[$arrLen-1] !== '('){ + compute($numStack, $operStack); + $arrLen--; + } + array_pop($operStack); + break; + default: + throw new \Exception("涓嶆敮鎸佺殑杩愮畻绗", 1); + break; + } + } + + $arrLen = count($operStack); + while ($operStack[$arrLen-1] !== NULL){ + compute($numStack, $operStack); + $arrLen--; + } + echo array_pop($numStack); +} + +//鏁板瓧鏍堥暱搴﹀噺涓锛岃繍绠楃鏍堥暱搴﹀噺涓 +function compute(&$numStack, &$operStack){ + $num = array_pop($numStack); + switch (array_pop($operStack)) { + case '*': + array_push($numStack, array_pop($numStack) * $num); + break; + case '/': + array_push($numStack, array_pop($numStack) / $num); + break; + case '+': + array_push($numStack, array_pop($numStack) + $num); + break; + case '-': + array_push($numStack, array_pop($numStack) - $num); + break; + + } +} +expression('-1+2-(1+2*3)'); +echo PHP_EOL; +eval('echo -1+2-(1+2*3);'); \ No newline at end of file diff --git a/php/composer.json b/php/composer.json index eef8f49f..12bea697 100644 --- a/php/composer.json +++ b/php/composer.json @@ -7,8 +7,12 @@ "psr-4": { "Algo_06\\": "06_linkedlist/", "Algo_07\\": "07_linkedlist/", +<<<<<<< HEAD + "Algo_08\\": "08_stack/" +======= "Algo_08\\": "08_stack/", "Algo_09\\": "09_queue/" +>>>>>>> upstream/master } } } diff --git a/python/05_array/myarray.py b/python/05_array/myarray.py index 8eea274d..6ff05629 100644 --- a/python/05_array/myarray.py +++ b/python/05_array/myarray.py @@ -79,6 +79,12 @@ def print_all(self): for i in range(6): a.insert_to_tail(i) +<<<<<<< HEAD + a.delete(2) + print(a) + a.insert_to_tail(7) + print(a) +======= print('origin',a) a.delete(4) print ('delete ',a) @@ -86,3 +92,4 @@ def print_all(self): a.insert(100,10000) print (a) +>>>>>>> upstream/master diff --git a/python/09_queue/array_queue.py b/python/09_queue/array_queue.py index b8ffe4b5..19c6b79a 100644 --- a/python/09_queue/array_queue.py +++ b/python/09_queue/array_queue.py @@ -21,7 +21,11 @@ def enqueue(self, item: str) -> bool: return False else: for i in range(0, self._tail - self._head): +<<<<<<< HEAD + self._data[i] = self._items[i + self._head] +======= self._items[i] = self._items[i + self._head] +>>>>>>> upstream/master self._tail = self._tail - self._head self._head = 0 diff --git a/python/11_sorts/sorts.py b/python/11_sorts/sorts.py index b9e34933..127a6475 100644 --- a/python/11_sorts/sorts.py +++ b/python/11_sorts/sorts.py @@ -11,8 +11,13 @@ def bubble_sort(a: List[int]): if len(a) <= 1: return +<<<<<<< HEAD + made_swap = False + for i in range(len(a)): +======= for i in range(len(a)): made_swap = False +>>>>>>> upstream/master for j in range(len(a) - i - 1): if a[j] > a[j+1]: a[j], a[j+1] = a[j+1], a[j]