From 6182dd9b45c67c710fd2f33a0711869e9e502970 Mon Sep 17 00:00:00 2001 From: zhangzc Date: Fri, 5 Nov 2021 17:55:29 +0800 Subject: [PATCH] add 22 algorithm exercises --- .../config.json" | 12 + .../desc.html" | 1 + .../solution.cpp" | 51 +++ .../solution.json" | 6 + .../solution.md" | 225 +++++++++++ .../config.json" | 12 + .../desc.html" | 1 + .../solution.cpp" | 91 +++++ .../solution.json" | 6 + .../solution.md" | 173 +++++++++ .../77_\345\255\220\351\233\206/config.json" | 12 + .../77_\345\255\220\351\233\206/desc.html" | 1 + .../77_\345\255\220\351\233\206/solution.cpp" | 24 ++ .../solution.json" | 6 + .../77_\345\255\220\351\233\206/solution.md" | 144 +++++++ .../config.json" | 12 + .../desc.html" | 28 ++ .../solution.cpp" | 44 +++ .../solution.json" | 6 + .../solution.md" | 165 ++++++++ .../config.json" | 12 + .../desc.html" | 30 ++ .../solution.cpp" | 57 +++ .../solution.json" | 6 + .../solution.md" | 256 +++++++++++++ .../config.json" | 12 + .../desc.html" | 1 + .../solution.cpp" | 39 ++ .../solution.json" | 6 + .../solution.md" | 149 ++++++++ .../config.json" | 12 + .../desc.html" | 1 + .../solution.cpp" | 63 ++++ .../solution.json" | 6 + .../solution.md" | 149 ++++++++ .../config.json" | 12 + .../desc.html" | 1 + .../solution.cpp" | 34 ++ .../solution.json" | 6 + .../solution.md" | 124 +++++++ .../config.json" | 12 + .../desc.html" | 1 + .../solution.cpp" | 76 ++++ .../solution.json" | 6 + .../solution.md" | 181 +++++++++ .../config.json" | 12 + .../desc.html" | 1 + .../solution.cpp" | 49 +++ .../solution.json" | 6 + .../solution.md" | 271 ++++++++++++++ .../config.json" | 12 + .../desc.html" | 1 + .../solution.cpp" | 93 +++++ .../solution.json" | 6 + .../solution.md" | 225 +++++++++++ .../config.json" | 12 + .../desc.html" | 1 + .../solution.cpp" | 42 +++ .../solution.json" | 6 + .../solution.md" | 166 +++++++++ .../config.json" | 12 + .../desc.html" | 1 + .../solution.cpp" | 56 +++ .../solution.json" | 6 + .../solution.md" | 154 ++++++++ .../config.json" | 12 + .../desc.html" | 1 + .../solution.cpp" | 19 + .../solution.json" | 6 + .../solution.md" | 102 +++++ .../config.json" | 12 + .../desc.html" | 1 + .../solution.cpp" | 36 ++ .../solution.json" | 6 + .../solution.md" | 187 ++++++++++ .../config.json" | 12 + .../desc.html" | 1 + .../solution.cpp" | 73 ++++ .../solution.json" | 6 + .../solution.md" | 225 +++++++++++ .../config.json" | 12 + .../desc.html" | 59 +++ .../solution.cpp" | 63 ++++ .../solution.json" | 6 + .../solution.md" | 241 ++++++++++++ .../config.json" | 12 + .../desc.html" | 63 ++++ .../solution.cpp" | 118 ++++++ .../solution.json" | 6 + .../solution.md" | 351 ++++++++++++++++++ .../config.json" | 12 + .../desc.html" | 1 + .../solution.cpp" | 37 ++ .../solution.json" | 6 + .../solution.md" | 177 +++++++++ .../76_\347\273\204\345\220\210/config.json" | 12 + .../76_\347\273\204\345\220\210/desc.html" | 3 + .../76_\347\273\204\345\220\210/solution.cpp" | 30 ++ .../solution.json" | 6 + .../76_\347\273\204\345\220\210/solution.md" | 171 +++++++++ .../config.json" | 12 + .../desc.html" | 1 + .../solution.cpp" | 68 ++++ .../solution.json" | 6 + .../solution.md" | 203 ++++++++++ .../65_\345\212\240\344\270\200/config.json" | 12 + .../65_\345\212\240\344\270\200/desc.html" | 1 + .../65_\345\212\240\344\270\200/solution.cpp" | 23 ++ .../solution.json" | 6 + .../65_\345\212\240\344\270\200/solution.md" | 140 +++++++ .../config.json" | 12 + .../desc.html" | 1 + .../solution.cpp" | 101 +++++ .../solution.json" | 6 + .../solution.md" | 181 +++++++++ .../config.json" | 12 + .../desc.html" | 7 + .../solution.cpp" | 35 ++ .../solution.json" | 6 + .../solution.md" | 110 ++++++ .../solution.md" | 125 ++++++- .../solution.md" | 244 +++++++++++- .../65_\345\212\240\344\270\200/solution.md" | 115 +++++- .../solution.md" | 160 +++++++- .../solution.md" | 264 ++++++++++++- .../solution.md" | 79 +++- .../solution.md" | 77 +++- .../solution.md" | 198 +++++++++- .../solution.md" | 158 +++++++- .../solution.md" | 198 +++++++++- .../solution.md" | 148 +++++++- .../solution.md" | 124 ++++++- .../solution.md" | 150 +++++++- .../76_\347\273\204\345\220\210/solution.md" | 142 ++++++- .../77_\345\255\220\351\233\206/solution.md" | 117 +++++- .../solution.md" | 174 ++++++++- .../solution.md" | 113 +++++- .../solution.md" | 204 +++++++++- .../solution.md" | 122 +++++- .../solution.md" | 97 ++++- .../solution.md" | 143 ++++++- .../solution.md" | 196 +++++++++- .../solution.md" | 154 +++++++- .../solution.md" | 162 +++++++- 144 files changed, 10011 insertions(+), 85 deletions(-) create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/72_\347\237\251\351\230\265\347\275\256\351\233\266/config.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/72_\347\237\251\351\230\265\347\275\256\351\233\266/desc.html" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/72_\347\237\251\351\230\265\347\275\256\351\233\266/solution.cpp" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/72_\347\237\251\351\230\265\347\275\256\351\233\266/solution.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/72_\347\237\251\351\230\265\347\275\256\351\233\266/solution.md" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/config.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/desc.html" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/solution.cpp" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/solution.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/solution.md" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/77_\345\255\220\351\233\206/config.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/77_\345\255\220\351\233\206/desc.html" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/77_\345\255\220\351\233\206/solution.cpp" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/77_\345\255\220\351\233\206/solution.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/77_\345\255\220\351\233\206/solution.md" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/config.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/desc.html" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/solution.cpp" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/solution.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/solution.md" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/config.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/desc.html" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/solution.cpp" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/solution.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/solution.md" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode\345\217\214\346\214\207\351\222\210/74_\351\242\234\350\211\262\345\210\206\347\261\273/config.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode\345\217\214\346\214\207\351\222\210/74_\351\242\234\350\211\262\345\210\206\347\261\273/desc.html" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode\345\217\214\346\214\207\351\222\210/74_\351\242\234\350\211\262\345\210\206\347\261\273/solution.cpp" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode\345\217\214\346\214\207\351\222\210/74_\351\242\234\350\211\262\345\210\206\347\261\273/solution.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode\345\217\214\346\214\207\351\222\210/74_\351\242\234\350\211\262\345\210\206\347\261\273/solution.md" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/config.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/desc.html" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/solution.cpp" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/solution.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/solution.md" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/config.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/desc.html" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/solution.cpp" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/solution.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/solution.md" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/85_\345\210\206\351\232\224\351\223\276\350\241\250/config.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/85_\345\210\206\351\232\224\351\223\276\350\241\250/desc.html" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/85_\345\210\206\351\232\224\351\223\276\350\241\250/solution.cpp" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/85_\345\210\206\351\232\224\351\223\276\350\241\250/solution.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/85_\345\210\206\351\232\224\351\223\276\350\241\250/solution.md" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/3.leetcode\345\255\227\347\254\246\344\270\262/64_\346\234\211\346\225\210\346\225\260\345\255\227/config.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/3.leetcode\345\255\227\347\254\246\344\270\262/64_\346\234\211\346\225\210\346\225\260\345\255\227/desc.html" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/3.leetcode\345\255\227\347\254\246\344\270\262/64_\346\234\211\346\225\210\346\225\260\345\255\227/solution.cpp" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/3.leetcode\345\255\227\347\254\246\344\270\262/64_\346\234\211\346\225\210\346\225\260\345\255\227/solution.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/3.leetcode\345\255\227\347\254\246\344\270\262/64_\346\234\211\346\225\210\346\225\260\345\255\227/solution.md" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/70_\347\256\200\345\214\226\350\267\257\345\276\204/config.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/70_\347\256\200\345\214\226\350\267\257\345\276\204/desc.html" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/70_\347\256\200\345\214\226\350\267\257\345\276\204/solution.cpp" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/70_\347\256\200\345\214\226\350\267\257\345\276\204/solution.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/70_\347\256\200\345\214\226\350\267\257\345\276\204/solution.md" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/config.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/desc.html" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/solution.cpp" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/solution.json" create mode 100644 "data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/solution.md" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/config.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/desc.html" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/solution.cpp" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/solution.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/solution.md" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/69_\347\210\254\346\245\274\346\242\257/config.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/69_\347\210\254\346\245\274\346\242\257/desc.html" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/69_\347\210\254\346\245\274\346\242\257/solution.cpp" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/69_\347\210\254\346\245\274\346\242\257/solution.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/69_\347\210\254\346\245\274\346\242\257/solution.md" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/71_\347\274\226\350\276\221\350\267\235\347\246\273/config.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/71_\347\274\226\350\276\221\350\267\235\347\246\273/desc.html" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/71_\347\274\226\350\276\221\350\267\235\347\246\273/solution.cpp" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/71_\347\274\226\350\276\221\350\267\235\347\246\273/solution.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/71_\347\274\226\350\276\221\350\267\235\347\246\273/solution.md" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/84_\346\234\200\345\244\247\347\237\251\345\275\242/config.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/84_\346\234\200\345\244\247\347\237\251\345\275\242/desc.html" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/84_\346\234\200\345\244\247\347\237\251\345\275\242/solution.cpp" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/84_\346\234\200\345\244\247\347\237\251\345\275\242/solution.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/84_\346\234\200\345\244\247\347\237\251\345\275\242/solution.md" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/config.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/desc.html" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/solution.cpp" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/solution.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/solution.md" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/config.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/desc.html" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/solution.cpp" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/solution.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/solution.md" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/6.leetcode\345\223\210\345\270\214\350\241\250/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/config.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/6.leetcode\345\223\210\345\270\214\350\241\250/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/desc.html" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/6.leetcode\345\223\210\345\270\214\350\241\250/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/solution.cpp" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/6.leetcode\345\223\210\345\270\214\350\241\250/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/solution.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/6.leetcode\345\223\210\345\270\214\350\241\250/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/solution.md" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/76_\347\273\204\345\220\210/config.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/76_\347\273\204\345\220\210/desc.html" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/76_\347\273\204\345\220\210/solution.cpp" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/76_\347\273\204\345\220\210/solution.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/76_\347\273\204\345\220\210/solution.md" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/78_\345\215\225\350\257\215\346\220\234\347\264\242/config.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/78_\345\215\225\350\257\215\346\220\234\347\264\242/desc.html" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/78_\345\215\225\350\257\215\346\220\234\347\264\242/solution.cpp" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/78_\345\215\225\350\257\215\346\220\234\347\264\242/solution.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/78_\345\215\225\350\257\215\346\220\234\347\264\242/solution.md" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/65_\345\212\240\344\270\200/config.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/65_\345\212\240\344\270\200/desc.html" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/65_\345\212\240\344\270\200/solution.cpp" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/65_\345\212\240\344\270\200/solution.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/65_\345\212\240\344\270\200/solution.md" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/config.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/desc.html" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/solution.cpp" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/solution.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/solution.md" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/68_x \347\232\204\345\271\263\346\226\271\346\240\271/config.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/68_x \347\232\204\345\271\263\346\226\271\346\240\271/desc.html" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/68_x \347\232\204\345\271\263\346\226\271\346\240\271/solution.cpp" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/68_x \347\232\204\345\271\263\346\226\271\346\240\271/solution.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/68_x \347\232\204\345\271\263\346\226\271\346\240\271/solution.md" diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/72_\347\237\251\351\230\265\347\275\256\351\233\266/config.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/72_\347\237\251\351\230\265\347\275\256\351\233\266/config.json" new file mode 100644 index 000000000..a7b65ffe9 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/72_\347\237\251\351\230\265\347\275\256\351\233\266/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "矩阵置零" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "矩阵置零" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/72_\347\237\251\351\230\265\347\275\256\351\233\266/desc.html" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/72_\347\237\251\351\230\265\347\275\256\351\233\266/desc.html" new file mode 100644 index 000000000..92e1e2d43 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/72_\347\237\251\351\230\265\347\275\256\351\233\266/desc.html" @@ -0,0 +1 @@ +

给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0 。请使用 原地 算法

进阶:

 

示例 1:

输入:matrix = [[1,1,1],[1,0,1],[1,1,1]]
输出:
[[1,0,1],[0,0,0],[1,0,1]]

示例 2:

输入:matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
输出:
[[0,0,0,0],[0,4,5,0],[0,3,1,0]]

 

提示:

\ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/72_\347\237\251\351\230\265\347\275\256\351\233\266/solution.cpp" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/72_\347\237\251\351\230\265\347\275\256\351\233\266/solution.cpp" new file mode 100644 index 000000000..546f95b40 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/72_\347\237\251\351\230\265\347\275\256\351\233\266/solution.cpp" @@ -0,0 +1,51 @@ +#include +using namespace std; +public: +void setZeroes(vector> &matrix) +{ + bool bRow = false, bCol = false; + for (int row = 0; row < matrix.size(); row++) + { + for (int col = 0; col < matrix[row].size(); col++) + { + if (matrix[row][col] == 0) + { + if (row == 0) + { + bRow = true; + } + if (col == 0) + { + bCol = true; + } + matrix[0][col] = matrix[row][0] = 0; + } + } + } + for (int row = 1; row < matrix.size(); row++) + { + for (int col = 1; col < matrix[row].size(); col++) + { + if (matrix[0][col] == 0 || matrix[row][0] == 0) + { + matrix[row][col] = 0; + } + } + } + if (bRow) + { + for (auto &m : matrix[0]) + { + m = 0; + } + } + if (bCol) + { + for (int row = 0; row < matrix.size(); row++) + { + matrix[row][0] = 0; + } + } +} +} +; \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/72_\347\237\251\351\230\265\347\275\256\351\233\266/solution.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/72_\347\237\251\351\230\265\347\275\256\351\233\266/solution.json" new file mode 100644 index 000000000..c940ebaa2 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/72_\347\237\251\351\230\265\347\275\256\351\233\266/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "809ee8412dbd47ba967017d142f698e0" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/72_\347\237\251\351\230\265\347\275\256\351\233\266/solution.md" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/72_\347\237\251\351\230\265\347\275\256\351\233\266/solution.md" new file mode 100644 index 000000000..c0206fcd7 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/72_\347\237\251\351\230\265\347\275\256\351\233\266/solution.md" @@ -0,0 +1,225 @@ +# 矩阵置零 +

给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0 。请使用 原地 算法

进阶:

  • 一个直观的解决方案是使用  O(mn) 的额外空间,但这并不是一个好的解决方案。
  • 一个简单的改进方案是使用 O(m + n) 的额外空间,但这仍然不是最好的解决方案。
  • 你能想出一个仅使用常量空间的解决方案吗?

 

示例 1:

输入:matrix = [[1,1,1],[1,0,1],[1,1,1]]
输出:
[[1,0,1],[0,0,0],[1,0,1]]

示例 2:

输入:matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
输出:
[[0,0,0,0],[0,4,5,0],[0,3,1,0]]

 

提示:

  • m == matrix.length
  • n == matrix[0].length
  • 1 <= m, n <= 200
  • -231 <= matrix[i][j] <= 231 - 1
+

以下错误的选项是?

+## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + int a = 3, b = 3; + + vector> matrix = vector>(a, vector(b)) = {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}; + sol.setZeroes(matrix); + for (auto i : matrix) + { + for (auto j : i) + cout << j << " "; + cout << endl; + } + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +public: + void setZeroes(vector> &matrix) + { + int row = matrix.size(); + int col = matrix[0].size(); + vector pos; + + for (int i = 0; i < row; i++) + { + for (int j = 0; j < col; j++) + { + if (matrix[i][j] == 0) + { + pos.push_back(i); + pos.push_back(j); + } + } + } + + for (int i = 1; i < pos.size(); i += 2) + { + int x = pos[i]; + int y = pos[i + 1]; + + for (int j = 0; j < row; j++) + { + matrix[j][y] = 0; + } + for (int k = 0; k < col; k++) + { + matrix[x][k] = 0; + } + } + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + void setZeroes(vector> &matrix) + { + if (matrix.size() <= 0) + return; + + int row = -1, col = -1; + + int i = 0; + + for (i = 0; i < matrix.size(); i++) + { + for (int j = 0; j < matrix[0].size(); j++) + { + if (matrix[i][j] == 0) + { + row = i; + col = j; + break; + } + } + if (row != -1) + break; + } + + if (i == matrix.size()) + return; + + for (int k = i + 1; k < matrix.size(); k++) + { + for (int j = 0; j < matrix[0].size(); j++) + { + if (matrix[k][j] == 0) + { + matrix[row][j] = 0; + matrix[k][col] = 0; + } + } + } + + for (i = 0; i < matrix[0].size(); i++) + { + if (i == col) + continue; + if (matrix[row][i] == 0) + for (int j = 0; j < matrix.size(); j++) + matrix[j][i] = 0; + } + + for (i = 0; i < matrix.size(); i++) + { + if (i == row) + continue; + if (matrix[i][col] == 0) + for (int j = 0; j < matrix[0].size(); j++) + matrix[i][j] = 0; + } + + for (i = 0; i < matrix.size(); i++) + matrix[i][col] = 0; + for (i = 0; i < matrix[0].size(); i++) + matrix[row][i] = 0; + } +}; +``` + +### B +```cpp +class Solution +{ +public: + void setZeroes(vector> &matrix) + { + bool row = false, col = false; + int m = matrix.size(), n = matrix[0].size(); + for (int i = 0; i < m; ++i) + { + if (matrix[i][0] == 0) + col = true; + } + for (int i = 0; i < n; ++i) + { + if (matrix[0][i] == 0) + row = true; + } + for (int i = 1; i < m; ++i) + for (int j = 1; j < n; ++j) + { + if (matrix[i][j] == 0) + matrix[i][0] = 0, matrix[0][j] = 0; + } + for (int i = 1; i < m; ++i) + for (int j = 1; j < n; ++j) + { + if (matrix[i][0] == 0 || matrix[0][j] == 0) + matrix[i][j] = 0; + } + if (col) + { + for (int i = 0; i < m; ++i) + matrix[i][0] = 0; + } + if (row) + { + for (int i = 0; i < n; ++i) + matrix[0][i] = 0; + } + } +}; +``` + +### C +```cpp +class Solution +{ +public: + void setZeroes(vector> &matrix) + { + int m = matrix.size(), n = matrix[0].size(); + vector row(m, false), column(n, false); + int i, j; + for (i = 0; i < m; i++) + { + for (j = 0; j < n; j++) + { + if (matrix[i][j] == 0) + { + row[i] = true; + column[j] = true; + } + } + } + for (i = 0; i < m; i++) + { + if (row[i]) + { + for (j = 0; j < n; j++) + matrix[i][j] = 0; + } + } + for (i = 0; i < n; i++) + { + if (column[i]) + { + for (j = 0; j < m; j++) + matrix[j][i] = 0; + } + } + } +}; +``` diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/config.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/config.json" new file mode 100644 index 000000000..fe6aa99c0 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "搜索二维矩阵" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "搜索二维矩阵" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/desc.html" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/desc.html" new file mode 100644 index 000000000..78246975a --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/desc.html" @@ -0,0 +1 @@ +

编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:

  • 每行中的整数从左到右按升序排列。
  • 每行的第一个整数大于前一行的最后一个整数。

 

示例 1:

输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
输出:
true

示例 2:

输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
输出:
false

 

提示:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 100
  • -104 <= matrix[i][j], target <= 104
\ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/solution.cpp" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/solution.cpp" new file mode 100644 index 000000000..171fe5568 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/solution.cpp" @@ -0,0 +1,91 @@ +#include +#include +#include +static int binary_search(int *nums, int len, int target) +{ + int low = -1; + int high = len; + while (low + 1 < high) + { + int mid = low + (high - low) / 2; + if (target > nums[mid]) + { + low = mid; + } + else + { + high = mid; + } + } + if (high == len || nums[high] != target) + { + return -high - 1; + } + else + { + return high; + } +} +static bool searchMatrix(int **matrix, int matrixRowSize, int matrixColSize, int target) +{ + if (matrixRowSize == 0 || matrixColSize == 0) + { + return false; + } + if (target < matrix[0][0] || target > matrix[matrixRowSize - 1][matrixColSize - 1]) + { + return false; + } + int row = 0; + int *nums = NULL; + if (matrixRowSize > 0) + { + nums = malloc(matrixRowSize * sizeof(int)); + for (row = 0; row < matrixRowSize; row++) + { + nums[row] = matrix[row][0]; + } + row = binary_search(nums, matrixRowSize, target); + if (row >= 0) + { + return true; + } + else + { + row = -row - 1; + if (row == 0) + { + return false; + } + else + { + row--; + } + } + } + int col = binary_search(matrix[row], matrixColSize, target); + return col >= 0; +} +int main(int argc, char **argv) +{ + int row = 3; + int col = 4; + int **mat = malloc(row * sizeof(int *)); + mat[0] = malloc(col * sizeof(int)); + mat[0][0] = 1; + mat[0][1] = 3; + mat[0][2] = 5; + mat[0][3] = 7; + mat[1] = malloc(col * sizeof(int)); + mat[1][0] = 10; + mat[1][1] = 11; + mat[1][2] = 16; + mat[1][3] = 20; + mat[2] = malloc(col * sizeof(int)); + mat[2][0] = 23; + mat[2][1] = 30; + mat[2][2] = 34; + mat[2][3] = 50; + printf("%s\n", searchMatrix(mat, row, col, atoi(argv[1])) ? "true" : "false"); + return 0; +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/solution.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/solution.json" new file mode 100644 index 000000000..2dcb373ce --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "059c2f45d5374f4584e7cfa2846fa7d8" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/solution.md" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/solution.md" new file mode 100644 index 000000000..07c9f77e1 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/solution.md" @@ -0,0 +1,173 @@ +# 搜索二维矩阵 +

编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:

  • 每行中的整数从左到右按升序排列。
  • 每行的第一个整数大于前一行的最后一个整数。

 

示例 1:

输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
输出:
true

示例 2:

输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
输出:
false

 

提示:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 100
  • -104 <= matrix[i][j], target <= 104
+

以下错误的选项是?

+## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + int a = 3, b = 4; + + vector> matrix = vector>(a, vector(b)) = {{1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 60}}; + int target = 3; + + bool res; + res = sol.searchMatrix(matrix, target); + + cout << res; + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +public: + bool searchMatrix(vector> &matrix, int target) + { + if (matrix.size() == 0 || matrix[0].size() == 0 || target < matrix[0][0] || target > matrix[matrix.size() - 1][matrix[0].size() - 1]) + return false; + int up = 0, down = matrix.size() - 1, left = 0, right = matrix[0].size() - 1; + while (up < down) + { + int mid = up + (down - up + 1) / 2; + if (matrix[mid][0] == target) + return true; + else if (matrix[mid][0] > target) + down = mid - 1; + else + up = mid + 1; + } + while (left < right) + { + int mid = left + (right - left + 1) / 2; + if (matrix[up][mid] == target) + return true; + else if (matrix[up][mid] > target) + right = mid - 1; + else + left = mid + 1; + } + if (matrix[up][left] == target) + return true; + return false; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + bool searchMatrix(vector> &matrix, int target) + { + if (matrix.size() == 0 || matrix[0].size() == 0) + return false; + decltype(matrix.size()) row_front = 0, c_front = 0, row_back = matrix.size(), c_back = matrix[0].size(); + while (row_front < row_back) + { + auto k = row_front + (row_back - row_front) / 2; + if (matrix[k][0] == target) + { + return true; + } + else if (matrix[k][0] < target) + { + row_front = k + 1; + } + else if (matrix[k][0] > target) + { + row_back = k; + } + } + if (row_front == 0) + { + return false; + } + decltype(matrix.size()) target_line = row_front - 1; + while (c_front < c_back) + { + auto j = c_front + (c_back - c_front) / 2; + if (matrix[target_line][j] == target) + { + return true; + } + else if (matrix[target_line][j] < target) + { + c_front = j + 1; + } + else if (matrix[target_line][j] > target) + { + c_back = j; + } + } + return false; + } +}; +``` + +### B +```cpp +class Solution +{ +public: + bool searchMatrix(vector> &matrix, int target) + { + if (matrix.empty()) + return false; + + int m = matrix.size(); + int n = matrix[0].size(); + + int left = 0; + int right = m * n - 1; + while (left <= right) + { + int middle = (left + right) / 2; + int middle_element = matrix[middle / n][middle % n]; + if (middle_element == target) + return true; + else if (middle_element < target) + left = middle + 1; + else + right = middle - 1; + } + return false; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + bool searchMatrix(vector> &matrix, int target) + { + if (matrix.empty()) + return false; + int m = matrix.size(); + int n = matrix[0].size(); + int row = 0, col = n - 1; + while (row < m && col >= 0) + { + if (matrix[row][col] < target && col == n - 1) + row++; + else if (matrix[row][col] == target) + return true; + else + col--; + } + return false; + } +}; +``` diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/77_\345\255\220\351\233\206/config.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/77_\345\255\220\351\233\206/config.json" new file mode 100644 index 000000000..09780bce2 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/77_\345\255\220\351\233\206/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "子集" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "子集" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/77_\345\255\220\351\233\206/desc.html" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/77_\345\255\220\351\233\206/desc.html" new file mode 100644 index 000000000..b599426d6 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/77_\345\255\220\351\233\206/desc.html" @@ -0,0 +1 @@ +

给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。

解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。

 

示例 1:

输入:nums = [1,2,3]
输出:
[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

示例 2:

输入:nums = [0]
输出:
[[],[0]]

 

提示:

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10
  • nums 中的所有元素 互不相同
\ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/77_\345\255\220\351\233\206/solution.cpp" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/77_\345\255\220\351\233\206/solution.cpp" new file mode 100644 index 000000000..7cf9fd190 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/77_\345\255\220\351\233\206/solution.cpp" @@ -0,0 +1,24 @@ +#include +using namespace std; +class Solution +{ +public: + vector> subsets(vector &nums) + { + vector> res; + dfs(nums, 0, res); + return res; + } +private: + vector stack; + void dfs(vector &nums, int start, vector> &res) + { + res.push_back(stack); + for (int i = start; i < nums.size(); i++) + { + stack.push_back(nums[i]); + dfs(nums, i + 1, res); + stack.pop_back(); + } + } +}; \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/77_\345\255\220\351\233\206/solution.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/77_\345\255\220\351\233\206/solution.json" new file mode 100644 index 000000000..0db1aaeac --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/77_\345\255\220\351\233\206/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "10a696dc3f174eccab18fed2ea531866" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/77_\345\255\220\351\233\206/solution.md" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/77_\345\255\220\351\233\206/solution.md" new file mode 100644 index 000000000..d2ab9a9dc --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/77_\345\255\220\351\233\206/solution.md" @@ -0,0 +1,144 @@ +# 子集 +

给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。

解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。

 

示例 1:

输入:nums = [1,2,3]
输出:
[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

示例 2:

输入:nums = [0]
输出:
[[],[0]]

 

提示:

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10
  • nums 中的所有元素 互不相同
+

以下错误的选项是?

+## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + vector nums = {1, 2, 3}; + vector> res; + res = sol.subsets(nums); + for (auto i : res) + { + for (auto j : i) + cout << j << " "; + cout << endl; + } + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +private: + void subsetss(int numssize, int i, vector &nums, vector> &output, vector &newtemp) + { + if (i >= numssize) + { + output.push_back(newtemp); + return; + } + newtemp.push_back(nums[i]); + subsetss(numssize, i + 1, nums, output, newtemp); + newtemp.pop_back(); + } + +public: + vector> subsets(vector &nums) + { + int numssize = nums.size(); + int i = 0; + vector> output; + vector newtemp; + subsetss(numssize, i, nums, output, newtemp); + return output; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + vector> subsets(vector &nums) + { + int numssize = nums.size(); + int numscount = 1 << numssize; + vector> output; + int i = 0; + while (i < numscount) + { + vector newtemp; + for (int x = 0; x < numssize; x++) + { + if ((1 << x) & i) + { + newtemp.push_back(nums[x]); + } + } + i++; + output.push_back(newtemp); + } + return output; + } +}; +``` + +### B +```cpp +class Solution +{ +public: + vector> subsets(vector &nums) + { + vector> output; + vector subset; + int length = nums.size(); + output.push_back(subset); + int current = 0; + int output_size; + while (current < length) + { + output_size = output.size(); + for (int i = 0; i < output_size; i++) + { + vector newinsert = output[i]; + newinsert.insert(newinsert.end(), nums[current]); + output.push_back(newinsert); + } + current++; + } + + return output; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + vector> subsets(vector &nums) + { + vector> res; + if (nums.empty()) + return res; + res.push_back({}); + int n = nums.size(); + for (int i = 0; i < n; i++) + { + + int nRes = res.size(); + for (int j = 0; j < nRes; j++) + { + vector temp = res[j]; + temp.push_back(nums[i]); + res.push_back(temp); + } + } + return res; + } +}; +``` diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/config.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/config.json" new file mode 100644 index 000000000..8e8e1ef0f --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "删除有序数组中的重复项 II" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "删除有序数组中的重复项 II" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/desc.html" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/desc.html" new file mode 100644 index 000000000..af2e6a569 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/desc.html" @@ -0,0 +1,28 @@ +

给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现两次 ,返回删除后数组的新长度。

+

不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。

+

 

+

说明:

+

为什么返回数值是整数,但输出的答案是数组呢?

+

请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。

+

你可以想象内部操作如下:

+
+    // nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
+    int len = removeDuplicates(nums);// 在函数里修改输入数组对于调用者是可见的。
+    // 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
+    for (int i = 0; i < len; i++) {
+            print(nums[i]);
+    }
+

 

+

示例 1:

+
输入:nums = [1,1,1,2,2,3]
输出:
5, nums = [1,1,2,2,3]
解释:
函数应返回新长度 length = 5, 并且原数组的前五个元素被修改为 1, 1, 2, 2, 3 。 不需要考虑数组中超出新长度后面的元素。
+

示例 2:

+
输入:nums = [0,0,1,1,1,1,2,3,3]
输出:
7, nums = [0,0,1,1,2,3,3]
解释:
函数应返回新长度 length = 7, 并且原数组的前五个元素被修改为 0, 0, 1, 1, 2, 3, 3 。 不需要考虑数组中超出新长度后面的元素。
+

 

+

提示:

+
    +
  • 1 <= nums.length <= 3 * 104
  • +
  • -104 <= nums[i] <= 104
  • +
  • nums 已按升序排列
  • +
\ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/solution.cpp" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/solution.cpp" new file mode 100644 index 000000000..033af79f9 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/solution.cpp" @@ -0,0 +1,44 @@ +#include +#include +static int removeDuplicates(int *nums, int numsSize) +{ + if (numsSize == 0) + { + return 0; + } + int i; + int len = 0; + int count = 1; + for (i = 1; i < numsSize; i++) + { + if (nums[len] == nums[i]) + { + if (count < 2) + { + count++; + nums[++len] = nums[i]; + } + } + else + { + count = 1; + nums[++len] = nums[i]; + } + } + return len + 1; +} +int main(int argc, char **argv) +{ + int i, count = argc - 1; + int *nums = malloc(count * sizeof(int)); + for (i = 0; i < count; i++) + { + nums[i] = atoi(argv[i + 1]); + } + count = removeDuplicates(nums, count); + for (i = 0; i < count; i++) + { + printf("%d ", nums[i]); + } + printf("\n"); +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/solution.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/solution.json" new file mode 100644 index 000000000..dce3cd9e3 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "56068be5e5d24eb9ba7d357dccdb5315" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/solution.md" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/solution.md" new file mode 100644 index 000000000..40586b0b2 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/solution.md" @@ -0,0 +1,165 @@ +# 删除有序数组中的重复项 II +

给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现两次 ,返回删除后数组的新长度。

+

不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。

+

 

+

说明:

+

为什么返回数值是整数,但输出的答案是数组呢?

+

请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。

+

你可以想象内部操作如下:

+
+    // nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
+    int len = removeDuplicates(nums);// 在函数里修改输入数组对于调用者是可见的。
+    // 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
+    for (int i = 0; i < len; i++) {
+            print(nums[i]);
+    }
+

 

+

示例 1:

+
输入:nums = [1,1,1,2,2,3]
输出:
5, nums = [1,1,2,2,3]
解释:
函数应返回新长度 length = 5, 并且原数组的前五个元素被修改为 1, 1, 2, 2, 3 。 不需要考虑数组中超出新长度后面的元素。
+

示例 2:

+
输入:nums = [0,0,1,1,1,1,2,3,3]
输出:
7, nums = [0,0,1,1,2,3,3]
解释:
函数应返回新长度 length = 7, 并且原数组的前五个元素被修改为 0, 0, 1, 1, 2, 3, 3 。 不需要考虑数组中超出新长度后面的元素。
+

 

+

提示:

+
    +
  • 1 <= nums.length <= 3 * 104
  • +
  • -104 <= nums[i] <= 104
  • +
  • nums 已按升序排列
  • +
+

以下错误的选项是?

+## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + vector nums = {1, 1, 1, 2, 2, 3}; + + int res; + res = sol.removeDuplicates(nums); + cout << res << endl; + for (auto i : nums) + cout << i << " "; + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +public: + int removeDuplicates(vector &nums) + { + int n = nums.size(); + if (n <= 2) + { + return n; + } + int sp = 1; + for (int fp = 2; fp < n; fp++) + { + if (nums[fp] != nums[sp - 1]) + { + nums[++sp] = nums[fp]; + } + } + return sp; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + int removeDuplicates(vector &nums) + { + unordered_map maps; + int n = 0; + for (int i = 0; i < nums.size(); ++i) + { + if (maps[nums[i]] < 2) + { + maps[nums[i]]++; + n++; + } + else + { + nums.erase(nums.begin() + i); + --i; + } + } + return n; + } +}; +``` + +### B +```cpp +class Solution +{ +public: + int removeDuplicates(vector &nums) + { + if (nums.empty()) + return 0; + int len; + len = 1; + for (int i = 1; i < nums.size(); i++) + { + if (i == 1 || nums[i] != nums[len - 2]) + { + nums[len] = nums[i]; + ++len; + } + } + return len; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + int removeDuplicates(vector &nums) + { + + int ret = nums.size(), quantity = nums[0], num = 0; + + for (vector::iterator iter = nums.begin(); iter != nums.end(); iter++) + { + + if (*iter == quantity) + { + num++; + + if (num > 2) + { + iter = nums.erase(iter); + iter--; + ret--; + num = 2; + } + } + + else + { + quantity = *iter; + num = 1; + } + } + return ret; + } +}; +``` diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/config.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/config.json" new file mode 100644 index 000000000..ade4ba9af --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "搜索旋转排序数组 II" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "搜索旋转排序数组 II" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/desc.html" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/desc.html" new file mode 100644 index 000000000..0e811baae --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/desc.html" @@ -0,0 +1,30 @@ +

已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同。

+

在传递给函数之前,nums 在预先未知的某个下标 k0 <= k < nums.length)上进行了 旋转 + ,使数组变为 [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]](下标 从 0 + 开始 计数)。例如, [0,1,2,4,4,4,5,6,6,7] 在下标 5 处经旋转后可能变为 + [4,5,6,6,7,0,1,2,4,4] 。 +

+

给你 旋转后 的数组 nums 和一个整数 target ,请你编写一个函数来判断给定的目标值是否存在于数组中。如果 + nums 中存在这个目标值 target ,则返回 true ,否则返回 false 。 +

+

 

+

示例 1:

+
输入:nums = [2,5,6,0,0,1,2], target = 0
输出:
true
+

示例 2:

+
输入:nums = [2,5,6,0,0,1,2], target = 3
输出:
false
+

 

+

提示:

+
    +
  • 1 <= nums.length <= 5000
  • +
  • -104 <= nums[i] <= 104
  • +
  • 题目数据保证 nums 在预先未知的某个下标上进行了旋转
  • +
  • -104 <= target <= 104
  • +
+

 

+

进阶:

+
    +
  • 这是 搜索旋转排序数组 的延伸题目,本题中的 nums  + 可能包含重复元素。
  • +
  • 这会影响到程序的时间复杂度吗?会有怎样的影响,为什么?
  • +
\ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/solution.cpp" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/solution.cpp" new file mode 100644 index 000000000..989beac50 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/solution.cpp" @@ -0,0 +1,57 @@ +#include +#include +#include +static bool search(int *nums, int numsSize, int target) +{ + int lo = 0; + int hi = numsSize - 1; + while (lo <= hi) + { + int mid = lo + (hi - lo) / 2; + if (nums[mid] == target) + { + return true; + } + if (nums[lo] == nums[mid] && nums[mid] == nums[hi]) + { + lo++; + hi--; + } + else if (nums[lo] <= nums[mid]) + { + if (nums[lo] <= target && target < nums[mid]) + { + hi = mid - 1; + } + else + { + lo = mid + 1; + } + } + else + { + if (nums[mid] < target && target <= nums[hi]) + { + lo = mid + 1; + } + else + { + hi = mid - 1; + } + } + } + return false; +} +int main(int argc, char **argv) +{ + int i; + int target = atoi(argv[1]); + int size = argc - 2; + int *nums = malloc(size * sizeof(int)); + for (i = 0; i < argc - 2; i++) + { + nums[i] = atoi(argv[i + 2]); + } + printf("%d\n", search(nums, size, target)); + return 0; +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/solution.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/solution.json" new file mode 100644 index 000000000..712c5af16 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "d97465cbc9394c6d917bfa83b756cf42" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/solution.md" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/solution.md" new file mode 100644 index 000000000..7b5e412bd --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/1.leetcode\346\225\260\347\273\204/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/solution.md" @@ -0,0 +1,256 @@ +# 搜索旋转排序数组 II +

已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同。

+

在传递给函数之前,nums 在预先未知的某个下标 k0 <= k < nums.length)上进行了 旋转 + ,使数组变为 [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]](下标 从 0 + 开始 计数)。例如, [0,1,2,4,4,4,5,6,6,7] 在下标 5 处经旋转后可能变为 + [4,5,6,6,7,0,1,2,4,4] 。 +

+

给你 旋转后 的数组 nums 和一个整数 target ,请你编写一个函数来判断给定的目标值是否存在于数组中。如果 + nums 中存在这个目标值 target ,则返回 true ,否则返回 false 。 +

+

 

+

示例 1:

+
输入:nums = [2,5,6,0,0,1,2], target = 0
输出:
true
+

示例 2:

+
输入:nums = [2,5,6,0,0,1,2], target = 3
输出:
false
+

 

+

提示:

+
    +
  • 1 <= nums.length <= 5000
  • +
  • -104 <= nums[i] <= 104
  • +
  • 题目数据保证 nums 在预先未知的某个下标上进行了旋转
  • +
  • -104 <= target <= 104
  • +
+

 

+

进阶:

+
    +
  • 这是 搜索旋转排序数组 的延伸题目,本题中的 nums  + 可能包含重复元素。
  • +
  • 这会影响到程序的时间复杂度吗?会有怎样的影响,为什么?
  • +
+

以下错误的选项是?

+## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + vector nums = {2, 5, 6, 0, 0, 1, 2}; + int target = 0; + int res; + res = sol.search(nums, target); + cout << res << endl; + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +public: + bool search(vector &nums, int target) + { + if (nums.empty() || nums.size() == 0) + { + return false; + } + int start = 0; + int end = nums.size() - 1; + int mid; + while (start <= end) + { + mid = start + (end - start) / 2; + if (nums[mid] == target) + { + return true; + } + if (nums[start] < nums[mid]) + { + + if (nums[mid] > target && nums[start] <= target) + { + end = mid - 1; + } + else + { + start = mid + 1; + } + } + else + { + + if (nums[mid] < target && nums[end] >= target) + { + start = mid + 1; + } + else + { + end = mid - 1; + } + } + } + + return false; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + bool search(const vector &nums, int target) + { + int first = 0, last = nums.size(); + while (first != last) + { + const int mid = first + (last - first) / 2; + if (nums[mid == target]) + { + return true; + } + if (nums[first] < nums[mid]) + { + if (nums[first] <= target && target < nums[mid]) + { + last = mid; + } + else + { + first = mid + 1; + } + } + else if (nums[first] > nums[mid]) + { + if (nums[mid] < target && target <= nums[last - 1]) + { + first = mid + 1; + } + else + { + last = mid; + } + } + else + { + first++; + } + } + return false; + } +}; +``` + +### B +```cpp +class Solution +{ +public: + bool search(vector &nums, int target) + { + if (nums.empty()) + return false; + int i = 0, j = nums.size() - 1, mid; + mid = (i + j) / 2; + if (nums[mid] == target) + return true; + else if (mid != i && nums[i] == nums[mid]) + { + i = mid + 1; + while (i <= j && nums[i] == nums[mid]) + i++; + if (i == nums.size()) + { + i = 0; + j = mid - 1; + while (i <= j && nums[j] == nums[mid]) + j--; + } + } + while (i <= j) + { + mid = (i + j) / 2; + if (nums[mid] == target) + return true; + else if (nums[mid] >= nums[i]) + { + if (target >= nums[i] && target < nums[mid]) + { + j = mid - 1; + while (i <= j && nums[j] == nums[mid]) + j--; + } + else + { + i = mid + 1; + while (i <= j && nums[i] == nums[mid]) + i++; + } + } + else + { + if (target > nums[mid] && target <= nums[j]) + { + i = mid + 1; + while (i <= j && nums[i] == nums[mid]) + i++; + } + else + { + j = mid - 1; + while (i <= j && nums[j] == nums[mid]) + j--; + } + } + } + return false; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + bool search(vector &nums, int &target) + { + int n = nums.size(); + if (n == 0) + return false; + int left = 0, right = n - 1; + while (left <= right) + { + int mid = (left + right) / 2; + if (nums[mid] == target) + return true; + else if (nums[mid] < nums[right]) + { + if (nums[mid] < target && nums[right] >= target) + left = mid + 1; + else + right = mid - 1; + } + else if (nums[mid] > nums[right]) + { + if (nums[left] <= target && nums[mid] > target) + right = mid - 1; + else + left = mid + 1; + } + else + --right; + } + return false; + } +}; +``` diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode\345\217\214\346\214\207\351\222\210/74_\351\242\234\350\211\262\345\210\206\347\261\273/config.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode\345\217\214\346\214\207\351\222\210/74_\351\242\234\350\211\262\345\210\206\347\261\273/config.json" new file mode 100644 index 000000000..112b90720 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode\345\217\214\346\214\207\351\222\210/74_\351\242\234\350\211\262\345\210\206\347\261\273/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "颜色分类" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "颜色分类" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode\345\217\214\346\214\207\351\222\210/74_\351\242\234\350\211\262\345\210\206\347\261\273/desc.html" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode\345\217\214\346\214\207\351\222\210/74_\351\242\234\350\211\262\345\210\206\347\261\273/desc.html" new file mode 100644 index 000000000..a79029d74 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode\345\217\214\346\214\207\351\222\210/74_\351\242\234\350\211\262\345\210\206\347\261\273/desc.html" @@ -0,0 +1 @@ +

给定一个包含红色、白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。

此题中,我们使用整数 0、 12 分别表示红色、白色和蓝色。

     

    示例 1:

    输入:nums = [2,0,2,1,1,0]
    输出:
    [0,0,1,1,2,2]

    示例 2:

    输入:nums = [2,0,1]
    输出:
    [0,1,2]

    示例 3:

    输入:nums = [0]
    输出:
    [0]

    示例 4:

    输入:nums = [1]
    输出:
    [1]

     

    提示:

    • n == nums.length
    • 1 <= n <= 300
    • nums[i]012

     

    进阶:

    • 你可以不使用代码库中的排序函数来解决这道题吗?
    • 你能想出一个仅使用常数空间的一趟扫描算法吗?
    \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode\345\217\214\346\214\207\351\222\210/74_\351\242\234\350\211\262\345\210\206\347\261\273/solution.cpp" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode\345\217\214\346\214\207\351\222\210/74_\351\242\234\350\211\262\345\210\206\347\261\273/solution.cpp" new file mode 100644 index 000000000..fc1d9739d --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode\345\217\214\346\214\207\351\222\210/74_\351\242\234\350\211\262\345\210\206\347\261\273/solution.cpp" @@ -0,0 +1,39 @@ +#include +using namespace std; +class Solution +{ +public: + void sortColors(vector &nums) + { + int i = 0, j = nums.size() - 1; + while (i < j) + { + if (nums[i] == 0) + { + i++; + continue; + } + if (nums[j] != 0) + { + j--; + continue; + } + swap(nums[i], nums[j]); + } + j = nums.size() - 1; + while (i < j) + { + if (nums[i] == 1) + { + i++; + continue; + } + if (nums[j] != 1) + { + j--; + continue; + } + swap(nums[i], nums[j]); + } + } +}; \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode\345\217\214\346\214\207\351\222\210/74_\351\242\234\350\211\262\345\210\206\347\261\273/solution.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode\345\217\214\346\214\207\351\222\210/74_\351\242\234\350\211\262\345\210\206\347\261\273/solution.json" new file mode 100644 index 000000000..e101758be --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode\345\217\214\346\214\207\351\222\210/74_\351\242\234\350\211\262\345\210\206\347\261\273/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "3597e6b773e84f019cfa733c2a9d3ad2" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode\345\217\214\346\214\207\351\222\210/74_\351\242\234\350\211\262\345\210\206\347\261\273/solution.md" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode\345\217\214\346\214\207\351\222\210/74_\351\242\234\350\211\262\345\210\206\347\261\273/solution.md" new file mode 100644 index 000000000..1be741536 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode\345\217\214\346\214\207\351\222\210/74_\351\242\234\350\211\262\345\210\206\347\261\273/solution.md" @@ -0,0 +1,149 @@ +# 颜色分类 +

    给定一个包含红色、白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。

    此题中,我们使用整数 0、 12 分别表示红色、白色和蓝色。

       

      示例 1:

      输入:nums = [2,0,2,1,1,0]
      输出:
      [0,0,1,1,2,2]

      示例 2:

      输入:nums = [2,0,1]
      输出:
      [0,1,2]

      示例 3:

      输入:nums = [0]
      输出:
      [0]

      示例 4:

      输入:nums = [1]
      输出:
      [1]

       

      提示:

      • n == nums.length
      • 1 <= n <= 300
      • nums[i]012

       

      进阶:

      • 你可以不使用代码库中的排序函数来解决这道题吗?
      • 你能想出一个仅使用常数空间的一趟扫描算法吗?
      +

      以下错误的选项是?

      +## aop +### before +```cpp +#include +using namespace std; + +``` +### after +```cpp +int main() +{ + Solution sol; + int a = 3, b = 4; + + vector nums = {2, 0, 2, 1, 1, 0}; + sol.sortColors(nums); + for (auto i : nums) + cout << i << " "; + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +public: + void sortColors(vector &nums) + { + int zero = 0, two = nums.size() - 1; + int i = 0; + while (i <= two) + { + if (nums[i] <= 1) + { + int temp = nums[i]; + nums[i] = nums[zero]; + nums[zero] = temp; + zero++; + i++; + } + else + { + int temp = nums[i]; + nums[i] = nums[two]; + nums[two] = temp; + two--; + } + } + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + void sortColors(vector &nums) + { + int p0 = 0; + int p1 = 0; + int p2 = nums.size() - 1; + while (p1 <= p2) + { + if (nums[p1] == 0) + swap(nums[p1++], nums[p0++]); + else if (nums[p1] == 2) + swap(nums[p1], nums[p2--]); + else + p1++; + } + } +}; +``` + +### B +```cpp +class Solution +{ +public: + void sortColors(vector &nums) + { + int a[3] = {0}; + int b[3] = {0, 1, 2}; + int num = 0; + for (int i = 0; i < nums.size(); i++) + { + if (nums[i] == 0) + a[0]++; + if (nums[i] == 1) + a[1]++; + if (nums[i] == 2) + a[2]++; + } + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < a[i]; j++) + { + nums[num] = b[i]; + num++; + } + } + } +}; +``` + +### C +```cpp +class Solution +{ +public: + void sortColors(vector &nums) + { + int start = 0; + int end = nums.size() - 1; + int temp; + for (int i = 0; i < nums.size(); i++) + { + if (i > end) + break; + if (nums[i] == 2) + { + if (i == end) + continue; + temp = nums[i]; + nums[i] = nums[end]; + nums[end] = temp; + end--; + i--; + } + else if (nums[i] == 0) + { + if (i == start) + continue; + temp = nums[i]; + nums[i] = nums[start]; + nums[start] = temp; + start++; + i--; + } + } + } +}; +``` diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/config.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/config.json" new file mode 100644 index 000000000..537c8bb38 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "删除排序链表中的重复元素 II" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "删除排序链表中的重复元素 II" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/desc.html" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/desc.html" new file mode 100644 index 000000000..96eb96169 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/desc.html" @@ -0,0 +1 @@ +

      存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 没有重复出现 的数字。

      返回同样按升序排列的结果链表。

       

      示例 1:

      输入:head = [1,2,3,3,4,4,5]
      输出:
      [1,2,5]

      示例 2:

      输入:head = [1,1,1,2,3]
      输出:
      [2,3]

       

      提示:

      • 链表中节点数目在范围 [0, 300]
      • -100 <= Node.val <= 100
      • 题目数据保证链表已经按升序排列
      \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/solution.cpp" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/solution.cpp" new file mode 100644 index 000000000..c2fef4977 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/solution.cpp" @@ -0,0 +1,63 @@ +#include +#include +struct ListNode +{ + int val; + struct ListNode *next; +}; +struct ListNode *deleteDuplicates(struct ListNode *head) +{ + struct ListNode dummy; + struct ListNode *p, *q, *prev; + prev = &dummy; + dummy.next = head; + p = q = head; + while (p != NULL) + { + while (q != NULL && q->val == p->val) + { + q = q->next; + } + if (p->next == q) + { + prev = p; + } + else + { + prev->next = q; + } + p = q; + } + return dummy.next; +} +int main(int argc, char **argv) +{ + int i; + struct ListNode *head = NULL; + struct ListNode *prev = NULL; + struct ListNode *p; + for (i = 0; i < argc - 1; i++) + { + p = malloc(sizeof(*p)); + p->val = atoi(argv[i + 1]); + p->next = NULL; + if (head == NULL) + { + head = p; + prev = head; + } + else + { + prev->next = p; + prev = p; + } + } + p = deleteDuplicates(head); + while (p != NULL) + { + printf("%d ", p->val); + p = p->next; + } + printf("\n"); + return 0; +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/solution.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/solution.json" new file mode 100644 index 000000000..9d5fb08b5 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "c0bd527c63d04abb93a667c1ad319f59" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/solution.md" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/solution.md" new file mode 100644 index 000000000..c2255cd01 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/solution.md" @@ -0,0 +1,149 @@ +# 删除排序链表中的重复元素 II +

      存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 没有重复出现 的数字。

      返回同样按升序排列的结果链表。

       

      示例 1:

      输入:head = [1,2,3,3,4,4,5]
      输出:
      [1,2,5]

      示例 2:

      输入:head = [1,1,1,2,3]
      输出:
      [2,3]

       

      提示:

      • 链表中节点数目在范围 [0, 300]
      • -100 <= Node.val <= 100
      • 题目数据保证链表已经按升序排列
      +

      以下错误的选项是?

      +## aop +### before +```cpp +#include +using namespace std; + +struct ListNode +{ + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; +``` +### after +```cpp + +``` + +## 答案 +```cpp +class Solution +{ +public: + ListNode *deleteDuplicates(ListNode *head) + { + if (!head || !head->next) + return head; + auto dummy = new ListNode(-1); + dummy->next = head; + auto pre = dummy, cur = head; + while (cur && cur->next) + { + if (cur->val != cur->next->val) + { + pre = cur; + cur = cur->next; + } + else + { + while (cur->next && cur->val == cur->next->val) + { + cur = cur->next; + } + } + } + return dummy->next; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + ListNode *deleteDuplicates(ListNode *head) + { + if (!head || !head->next) + return head; + if (head->val == head->next->val) + { + while (head->next && head->val == head->next->val) + { + head = head->next; + } + return deleteDuplicates(head->next); + } + else + head->next = deleteDuplicates(head->next); + return head; + } +}; +``` + +### B +```cpp +class Solution +{ +public: + ListNode *deleteDuplicates(ListNode *head) + { + ListNode *new_head = new ListNode(0); + ListNode *cur = new_head; + ListNode *temp1 = head; + while (temp1) + { + ListNode *temp2 = temp1->next; + if (!temp2) + { + cur->next = temp1; + temp1 = temp1->next; + cur = cur->next; + } + else if (temp2 && temp1->val != temp2->val) + { + cur->next = temp1; + temp1 = temp1->next; + cur = cur->next; + } + else + { + while (temp2 && temp1->val == temp2->val) + { + temp2 = temp2->next; + } + temp1 = temp2; + } + } + cur->next = NULL; + return new_head->next; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + ListNode *deleteDuplicates(ListNode *head) + { + if (head == NULL || head->next == NULL) + return head; + ListNode *L = new ListNode(0); + L->next = head; + + ListNode *slow = L; + while (slow->next) + { + ListNode *fast = slow->next; + while (fast->next && fast->val == fast->next->val) + { + fast = fast->next; + } + if (slow->next == fast) + slow = slow->next; + else + { + slow->next = fast->next; + } + } + return L->next; + } +}; +``` diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/config.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/config.json" new file mode 100644 index 000000000..b95434cf9 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "删除排序链表中的重复元素" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "删除排序链表中的重复元素" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/desc.html" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/desc.html" new file mode 100644 index 000000000..380d1bf48 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/desc.html" @@ -0,0 +1 @@ +

      存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除所有重复的元素,使每个元素 只出现一次

      返回同样按升序排列的结果链表。

       

      示例 1:

      输入:head = [1,1,2]
      输出:
      [1,2]

      示例 2:

      输入:head = [1,1,2,3,3]
      输出:
      [1,2,3]

       

      提示:

      • 链表中节点数目在范围 [0, 300]
      • -100 <= Node.val <= 100
      • 题目数据保证链表已经按升序排列
      \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/solution.cpp" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/solution.cpp" new file mode 100644 index 000000000..fcd68c74c --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/solution.cpp" @@ -0,0 +1,34 @@ +#include +using namespace std; +struct ListNode +{ + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} +}; +class Solution +{ +public: + ListNode *deleteDuplicates(ListNode *head) + { + if (head == nullptr) + { + return nullptr; + } + ListNode *prev = head; + ListNode *p = prev->next; + while (p != nullptr) + { + if (p->val != prev->val) + { + prev->next = p; + prev = p; + } + p = p->next; + } + prev->next = p; + return head; + } +}; \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/solution.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/solution.json" new file mode 100644 index 000000000..e32d15c61 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "125ae9e70c624ccb8f125134e2f31606" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/solution.md" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/solution.md" new file mode 100644 index 000000000..023444117 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/solution.md" @@ -0,0 +1,124 @@ +# 删除排序链表中的重复元素 +

      存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除所有重复的元素,使每个元素 只出现一次

      返回同样按升序排列的结果链表。

       

      示例 1:

      输入:head = [1,1,2]
      输出:
      [1,2]

      示例 2:

      输入:head = [1,1,2,3,3]
      输出:
      [1,2,3]

       

      提示:

      • 链表中节点数目在范围 [0, 300]
      • -100 <= Node.val <= 100
      • 题目数据保证链表已经按升序排列
      +

      以下错误的选项是?

      +## aop +### before +```cpp +#include +using namespace std; + +struct ListNode +{ + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; +``` +### after +```cpp + +``` + +## 答案 +```cpp +class Solution +{ +public: + ListNode *deleteDuplicates(ListNode *head) + { + if (!head || !head->next) + return head; + head->next = deleteDuplicates(head->next); + if (head->val != head->next->val) + head = head->next; + return head; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + ListNode *deleteDuplicates(ListNode *head) + { + if (head == NULL || head->next == NULL) + return head; + ListNode *p = head; + ListNode *q = head->next; + while (p->next != NULL) + { + if (p->val == q->val) + { + if (q->next == NULL) + p->next = NULL; + else + { + p->next = q->next; + q = q->next; + } + } + else + { + p = p->next; + q = q->next; + } + } + return head; + } +}; +``` + +### B +```cpp +class Solution +{ +public: + ListNode *deleteDuplicates(ListNode *head) + { + ListNode *cur = head; + while (cur != NULL) + { + ListNode *rear = cur->next; + if (rear == NULL) + return head; + if (cur->val == rear->val) + cur->next = rear->next; + else + cur = cur->next; + } + return head; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + ListNode *deleteDuplicates(ListNode *head) + { + if (head == NULL) + return head; + ListNode *ptr{head}; + while (ptr->next != NULL) + { + if (ptr->val == ptr->next->val) + { + ListNode *p = ptr->next; + ptr->next = p->next; + delete p; + } + else + { + ptr = ptr->next; + } + } + + return head; + } +}; +``` diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/85_\345\210\206\351\232\224\351\223\276\350\241\250/config.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/85_\345\210\206\351\232\224\351\223\276\350\241\250/config.json" new file mode 100644 index 000000000..c47003cd1 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/85_\345\210\206\351\232\224\351\223\276\350\241\250/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "分隔链表" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "分隔链表" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/85_\345\210\206\351\232\224\351\223\276\350\241\250/desc.html" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/85_\345\210\206\351\232\224\351\223\276\350\241\250/desc.html" new file mode 100644 index 000000000..2962177ef --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/85_\345\210\206\351\232\224\351\223\276\350\241\250/desc.html" @@ -0,0 +1 @@ +

      给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前。

      你应当 保留 两个分区中每个节点的初始相对位置。

       

      示例 1:

      输入:head = [1,4,3,2,5,2], x = 3
      输出
      :[1,2,2,4,3,5]

      示例 2:

      输入:head = [2,1], x = 2
      输出
      :[1,2]

       

      提示:

      • 链表中节点的数目在范围 [0, 200]
      • -100 <= Node.val <= 100
      • -200 <= x <= 200
      \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/85_\345\210\206\351\232\224\351\223\276\350\241\250/solution.cpp" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/85_\345\210\206\351\232\224\351\223\276\350\241\250/solution.cpp" new file mode 100644 index 000000000..3cabea1af --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/85_\345\210\206\351\232\224\351\223\276\350\241\250/solution.cpp" @@ -0,0 +1,76 @@ +#include +#include +struct ListNode +{ + int val; + struct ListNode *next; +}; +struct ListNode *partition(struct ListNode *head, int x) +{ + struct ListNode dummy; + struct ListNode *prev1 = &dummy, *pivot; + dummy.next = head; + for (pivot = head; pivot != NULL; pivot = pivot->next) + { + if (pivot->val >= x) + { + break; + } + prev1 = pivot; + } + struct ListNode *p = pivot->next; + struct ListNode *prev2 = pivot; + while (p != NULL) + { + if (p->val < x) + { + prev2->next = p->next; + p->next = prev1->next; + prev1->next = p; + prev1 = p; + p = prev2->next; + } + else + { + prev2 = p; + p = p->next; + } + } + return dummy.next; +} +int main(int argc, char **argv) +{ + if (argc < 2) + { + fprintf(stderr, "Usage: ./test target n1 n2 n3...\n"); + exit(-1); + } + int i, target = atoi(argv[1]); + struct ListNode *head = NULL; + struct ListNode *prev = NULL; + struct ListNode *p; + for (i = 0; i < argc - 2; i++) + { + p = malloc(sizeof(*p)); + p->val = atoi(argv[i + 2]); + p->next = NULL; + if (head == NULL) + { + head = p; + prev = head; + } + else + { + prev->next = p; + prev = p; + } + } + p = partition(head, target); + while (p != NULL) + { + printf("%d ", p->val); + p = p->next; + } + printf("\n"); + return 0; +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/85_\345\210\206\351\232\224\351\223\276\350\241\250/solution.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/85_\345\210\206\351\232\224\351\223\276\350\241\250/solution.json" new file mode 100644 index 000000000..adb91ef85 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/85_\345\210\206\351\232\224\351\223\276\350\241\250/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "92a0f69d294d4ce1adf43c603deb67e3" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/85_\345\210\206\351\232\224\351\223\276\350\241\250/solution.md" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/85_\345\210\206\351\232\224\351\223\276\350\241\250/solution.md" new file mode 100644 index 000000000..8612e2016 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/2.leetcode\351\223\276\350\241\250/85_\345\210\206\351\232\224\351\223\276\350\241\250/solution.md" @@ -0,0 +1,181 @@ +# 分隔链表 +

      给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前。

      你应当 保留 两个分区中每个节点的初始相对位置。

       

      示例 1:

      输入:head = [1,4,3,2,5,2], x = 3
      输出
      :[1,2,2,4,3,5]

      示例 2:

      输入:head = [2,1], x = 2
      输出
      :[1,2]

       

      提示:

      • 链表中节点的数目在范围 [0, 200]
      • -100 <= Node.val <= 100
      • -200 <= x <= 200
      +

      以下错误的选项是?

      +## aop +### before +```cpp +#include +using namespace std; + +struct ListNode +{ + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; +``` +### after +```cpp + +``` + +## 答案 +```cpp +class Solution +{ +public: + ListNode *partition(ListNode *head, int x) + { + if (!head || !head->next) + return head; + ListNode *p = head; + ListNode *q = head; + ListNode *qq = q; + int flag = 0; + while (q && q->val < x) + { + flag = 1; + p = q; + q = q->next; + } + while (q) + { + if (flag == 0 && q->val < x) + { + qq->next = q->next; + q->next = p; + p = q; + head = p; + q = qq->next; + flag = 1; + } + else if (flag == 1 && q->val < x) + { + qq->next = q->next; + q->next = p->next; + p->next = q; + p = p->next; + q = qq->next; + } + } + return head; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + ListNode *partition(ListNode *head, int x) + { + ListNode *dummy = new ListNode(-1); + dummy->next = head; + ListNode *pre = dummy, *cur; + while (pre->next && pre->next->val < x) + pre = pre->next; + cur = pre; + while (cur->next) + { + if (cur->next->val < x) + { + ListNode *tmp = cur->next; + cur->next = tmp->next; + tmp->next = pre->next; + pre->next = tmp; + pre = pre->next; + } + else + { + cur = cur->next; + } + } + return dummy->next; + } +}; + +``` + +### B +```cpp +class Solution +{ +public: + ListNode *partition(ListNode *head, int x) + { + ListNode *lessNode = new ListNode(-1); + ListNode *moreNode = new ListNode(-1); + ListNode *l = lessNode; + ListNode *m = moreNode; + while (head) + { + if (head->val >= x) + { + m->next = head; + m = m->next; + } + else + { + l->next = head; + l = l->next; + } + head = head->next; + } + l->next = moreNode->next; + m->next = NULL; + return lessNode->next; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + ListNode *partition(ListNode *head, int x) + { + if (head == NULL || head->next == NULL) + return head; + ListNode *ahead, *p, *after, *p1; + p = head; + while (p && p->val < x) + { + ahead = p; + p = p->next; + } + if (p == head) + ahead = p; + if (p) + after = p->next; + p1 = p; + while (after) + { + if (after->val < x) + { + if (p == head) + { + head = after; + ahead = head; + } + else + { + ahead->next = after; + ahead = after; + } + p1->next = after->next; + } + else + { + p1 = after; + } + after = after->next; + } + if (ahead != p) + ahead->next = p; + return head; + } +}; +``` diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/3.leetcode\345\255\227\347\254\246\344\270\262/64_\346\234\211\346\225\210\346\225\260\345\255\227/config.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/3.leetcode\345\255\227\347\254\246\344\270\262/64_\346\234\211\346\225\210\346\225\260\345\255\227/config.json" new file mode 100644 index 000000000..e38b57aad --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/3.leetcode\345\255\227\347\254\246\344\270\262/64_\346\234\211\346\225\210\346\225\260\345\255\227/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "有效数字" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "有效数字" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/3.leetcode\345\255\227\347\254\246\344\270\262/64_\346\234\211\346\225\210\346\225\260\345\255\227/desc.html" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/3.leetcode\345\255\227\347\254\246\344\270\262/64_\346\234\211\346\225\210\346\225\260\345\255\227/desc.html" new file mode 100644 index 000000000..ca39ca6c6 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/3.leetcode\345\255\227\347\254\246\344\270\262/64_\346\234\211\346\225\210\346\225\260\345\255\227/desc.html" @@ -0,0 +1 @@ +

      有效数字(按顺序)可以分成以下几个部分:

      1. 一个 小数 或者 整数
      2. (可选)一个 'e''E' ,后面跟着一个 整数

      小数(按顺序)可以分成以下几个部分:

      1. (可选)一个符号字符('+''-'
      2. 下述格式之一:
        1. 至少一位数字,后面跟着一个点 '.'
        2. 至少一位数字,后面跟着一个点 '.' ,后面再跟着至少一位数字
        3. 一个点 '.' ,后面跟着至少一位数字

      整数(按顺序)可以分成以下几个部分:

      1. (可选)一个符号字符('+''-'
      2. 至少一位数字

      部分有效数字列举如下:

      • ["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"]

      部分无效数字列举如下:

      • ["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"]

      给你一个字符串 s ,如果 s 是一个 有效数字 ,请返回 true

       

      示例 1:

      输入:s = "0"
      输出:
      true

      示例 2:

      输入:s = "e"
      输出:
      false

      示例 3:

      输入:s = "."
      输出:
      false

      示例 4:

      输入:s = ".1"
      输出:
      true

       

      提示:

      • 1 <= s.length <= 20
      • s 仅含英文字母(大写和小写),数字(0-9),加号 '+' ,减号 '-' ,或者点 '.'
      \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/3.leetcode\345\255\227\347\254\246\344\270\262/64_\346\234\211\346\225\210\346\225\260\345\255\227/solution.cpp" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/3.leetcode\345\255\227\347\254\246\344\270\262/64_\346\234\211\346\225\210\346\225\260\345\255\227/solution.cpp" new file mode 100644 index 000000000..9ca289758 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/3.leetcode\345\255\227\347\254\246\344\270\262/64_\346\234\211\346\225\210\346\225\260\345\255\227/solution.cpp" @@ -0,0 +1,49 @@ +#include +#include +#include +#include +static bool isNumber(const char *s) +{ + while (*s == ' ') + ++s; + bool if_find_num = false; + if (*s == '-' || *s == '+') + ++s; + while (isdigit(*s)) + { + if_find_num = true; + ++s; + } + if (*s == '.') + ++s; + while (isdigit(*s)) + { + if_find_num = true; + ++s; + } + if (if_find_num == true && *s == 'e') + { + ++s; + if (*s == '+' || *s == '-') + ++s; + if_find_num = false; + while (isdigit(*s)) + { + if_find_num = true; + ++s; + } + } + while (*s == ' ') + ++s; + return *s == '\0' && if_find_num == true; +} +int main(int argc, char **argv) +{ + if (argc != 2) + { + fprintf(stderr, "Usage: ./test number\n"); + exit(-1); + } + printf("%s\n", isNumber(argv[1]) ? "true" : "false"); + return 0; +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/3.leetcode\345\255\227\347\254\246\344\270\262/64_\346\234\211\346\225\210\346\225\260\345\255\227/solution.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/3.leetcode\345\255\227\347\254\246\344\270\262/64_\346\234\211\346\225\210\346\225\260\345\255\227/solution.json" new file mode 100644 index 000000000..e5ff0b7a5 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/3.leetcode\345\255\227\347\254\246\344\270\262/64_\346\234\211\346\225\210\346\225\260\345\255\227/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "55a54ace6aa54e449b7be515edab62a7" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/3.leetcode\345\255\227\347\254\246\344\270\262/64_\346\234\211\346\225\210\346\225\260\345\255\227/solution.md" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/3.leetcode\345\255\227\347\254\246\344\270\262/64_\346\234\211\346\225\210\346\225\260\345\255\227/solution.md" new file mode 100644 index 000000000..af93a713c --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/3.leetcode\345\255\227\347\254\246\344\270\262/64_\346\234\211\346\225\210\346\225\260\345\255\227/solution.md" @@ -0,0 +1,271 @@ +# 有效数字 +

      有效数字(按顺序)可以分成以下几个部分:

      1. 一个 小数 或者 整数
      2. (可选)一个 'e''E' ,后面跟着一个 整数

      小数(按顺序)可以分成以下几个部分:

      1. (可选)一个符号字符('+''-'
      2. 下述格式之一:
        1. 至少一位数字,后面跟着一个点 '.'
        2. 至少一位数字,后面跟着一个点 '.' ,后面再跟着至少一位数字
        3. 一个点 '.' ,后面跟着至少一位数字

      整数(按顺序)可以分成以下几个部分:

      1. (可选)一个符号字符('+''-'
      2. 至少一位数字

      部分有效数字列举如下:

      • ["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"]

      部分无效数字列举如下:

      • ["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"]

      给你一个字符串 s ,如果 s 是一个 有效数字 ,请返回 true

       

      示例 1:

      输入:s = "0"
      输出:
      true

      示例 2:

      输入:s = "e"
      输出:
      false

      示例 3:

      输入:s = "."
      输出:
      false

      示例 4:

      输入:s = ".1"
      输出:
      true

       

      提示:

      • 1 <= s.length <= 20
      • s 仅含英文字母(大写和小写),数字(0-9),加号 '+' ,减号 '-' ,或者点 '.'
      +

      以下错误的选项是?

      +## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + string s = ".1"; + bool res; + res = sol.isNumber(s); + cout << res; + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +public: + bool isNumber(string s) + { + int len = s.size(); + int left = 0, right = len - 1; + bool eExisted = false; + bool dotExisted = false; + bool digitExisited = false; + + while (s[left] == ' ') + ++left; + while (s[right] == ' ') + --right; + + if (left >= right && (s[left] < '0' || s[left] > '9')) + return false; + + if (s[left] == '.') + dotExisted = true; + else if (s[left] >= '0' && s[left] <= '9') + digitExisited = true; + else if (s[left] != '+' && s[left] != '-') + return false; + + for (int i = left + 1; i <= right - 1; ++i) + { + if (s[i] >= '0' && s[i] <= '9') + digitExisited = true; + else if (s[i] == 'e' || s[i] == 'E') + { + if (!eExisted && s[i - 1] != '+' && s[i - 1] != '-' && digitExisited) + eExisted = true; + else + return false; + } + else if (s[i] == '+' || s[i] == '-') + { + if (s[i - 1] != 'e' && s[i - 1] != 'E') + return false; + } + else if (s[i] == '.') + { + if (!dotExisted && !eExisted) + dotExisted = true; + else + return false; + } + else + return false; + } + + if (s[right] >= '0' && s[right] <= '9') + return true; + else + return false; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + bool isInt(string &s, int &index) + { + if (index < s.size() && (s[index] == '-' || s[index] == '+')) + index++; + return isUnsigned(s, index); + } + + bool isUnsigned(string &s, int &index) + { + int pre = index; + while (index < s.size()) + { + if (s[index] >= '0' && s[index] <= '9') + index++; + else + break; + } + return index > pre; + } + + bool isNumber(string s) + { + if (s.empty()) + { + return false; + } + + int index = 0; + while (index < s.size() && s[index] == ' ') + { + index++; + } + + bool ans = isInt(s, index); + if (index < s.size() && s[index] == '.') + { + index++; + ans = isUnsigned(s, index) || ans; + } + + if (index < s.size() && s[index] == 'e') + { + index++; + ans = isInt(s, index) && ans; + } + + while (index < s.size() && s[index] == ' ') + { + index++; + } + + return ans && index == s.size(); + } +}; +``` + +### B +```cpp +class Solution +{ +public: + bool isNumber(string s) + { + int tail = s.size() - 1; + int head = 0; + while (tail >= 0) + { + if (s[tail] == ' ') + --tail; + else + break; + } + while (head <= tail) + { + if (s[head] == ' ') + ++head; + else + break; + } + if (head > tail) + return false; + s = s.substr(head, tail - head + 1); + int index = s.find('e'); + if (index == string::npos) + return judgea(s); + else + return judgea(s.substr(0, index)) && judgeb(s.substr(index + 1)); + } + bool judgea(string s) + { + bool have_num = false; + bool have_pointed = false; + for (int i = 0; i < s.size(); i++) + { + if (s[i] >= '0' && s[i] <= '9') + have_num = true; + else if (s[i] == '+' || s[i] == '-') + { + if (i != 0) + return false; + } + else if (s[i] == '.') + { + if (have_pointed) + return false; + have_pointed = true; + } + else + return false; + } + return have_num; + } + + bool judgeb(string s) + { + bool have_num = false; + for (int i = 0; i < s.size(); i++) + { + if (s[i] >= '0' && s[i] <= '9') + have_num = true; + else if (s[i] == '-' || s[i] == '+') + { + if (i != 0) + return false; + } + else + return false; + } + return have_num; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + bool isNumber(string s) + { + bool num = false, numAfterE = true, dot = false, sign = false, exp = false; + int n = s.size(); + for (int i = 0; i < n; ++i) + { + if (s[i] == ' ') + { + if (i < n - 1 && s[i + 1] != ' ' && (num || dot || sign || exp)) + return false; + } + else if (s[i] == '+' || s[i] == '-') + { + if (i > 0 && s[i - 1] != 'e' && s[i - 1] != ' ') + return false; + sign = true; + } + else if (s[i] >= '0' && s[i] <= '9') + { + num = true; + numAfterE = true; + } + else if (s[i] == '.') + { + if (dot || exp) + return false; + dot = true; + } + else if (s[i] == 'e') + { + if (exp || !num) + return false; + exp = true; + numAfterE = false; + } + else + return false; + } + return num && numAfterE; + } +}; +``` diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/70_\347\256\200\345\214\226\350\267\257\345\276\204/config.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/70_\347\256\200\345\214\226\350\267\257\345\276\204/config.json" new file mode 100644 index 000000000..463559ea2 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/70_\347\256\200\345\214\226\350\267\257\345\276\204/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "简化路径" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "简化路径" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/70_\347\256\200\345\214\226\350\267\257\345\276\204/desc.html" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/70_\347\256\200\345\214\226\350\267\257\345\276\204/desc.html" new file mode 100644 index 000000000..f730116ee --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/70_\347\256\200\345\214\226\350\267\257\345\276\204/desc.html" @@ -0,0 +1 @@ +

      给你一个字符串 path ,表示指向某一文件或目录的 Unix 风格 绝对路径 (以 '/' 开头),请你将其转化为更加简洁的规范路径。

      在 Unix 风格的文件系统中,一个点(.)表示当前目录本身;此外,两个点 (..) 表示将目录切换到上一级(指向父目录);两者都可以是复杂相对路径的组成部分。任意多个连续的斜杠(即,'//')都被视为单个斜杠 '/' 。 对于此问题,任何其他格式的点(例如,'...')均被视为文件/目录名称。

      请注意,返回的 规范路径 必须遵循下述格式:

      • 始终以斜杠 '/' 开头。
      • 两个目录名之间必须只有一个斜杠 '/'
      • 最后一个目录名(如果存在)不能 '/' 结尾。
      • 此外,路径仅包含从根目录到目标文件或目录的路径上的目录(即,不含 '.''..')。

      返回简化后得到的 规范路径

       

      示例 1:

      输入:path = "/home/"
      输出:
      "/home"
      解释:
      注意,最后一个目录名后面没有斜杠。

      示例 2:

      输入:path = "/../"
      输出:
      "/"
      解释:
      从根目录向上一级是不可行的,因为根目录是你可以到达的最高级。

      示例 3:

      输入:path = "/home//foo/"
      输出:
      "/home/foo"
      解释:
      在规范路径中,多个连续斜杠需要用一个斜杠替换。

      示例 4:

      输入:path = "/a/./b/../../c/"
      输出:
      "/c"

       

      提示:

      • 1 <= path.length <= 3000
      • path 由英文字母,数字,'.''/''_' 组成。
      • path 是一个有效的 Unix 风格绝对路径。
      \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/70_\347\256\200\345\214\226\350\267\257\345\276\204/solution.cpp" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/70_\347\256\200\345\214\226\350\267\257\345\276\204/solution.cpp" new file mode 100644 index 000000000..eeb87488b --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/70_\347\256\200\345\214\226\350\267\257\345\276\204/solution.cpp" @@ -0,0 +1,93 @@ +#include +#include +#include +static char *simplifyPath(char *path) +{ + int len = strlen(path); + if (len == 0) + { + return path; + } + char *p = path; + int *indexes = malloc(len * sizeof(int)); + int depth = 0; + int name_start = 1; + while (*p != '\0') + { + if (*p == '/') + { + if (p > path && *(p - 1) != '/' && *(p - 1) != '.') + { + name_start = 1; + } + } + else if (*p == '.') + { + if (*(p + 1) == '\0' || *(p + 1) == '/') + { + p += 1; + } + else if (*(p + 1) == '.' && (*(p + 2) == '\0' || *(p + 2) == '/')) + { + if (depth > 0) + { + depth--; + name_start = 1; + } + p += 2; + } + else + { + indexes[depth++] = p - path; + while (*p != '/' && *p != '\0') + { + p++; + } + } + if (*p == '\0') + { + break; + } + } + else + { + if (name_start && depth >= 0) + { + indexes[depth++] = p - path; + name_start = 0; + } + } + p++; + } + int i; + char *result = malloc(len + 1); + char *q = result; + if (depth <= 0) + { + *q++ = '/'; + } + else + { + for (i = 0; i < depth; i++) + { + p = path + indexes[i]; + *q++ = '/'; + while (*p != '/') + { + *q++ = *p++; + } + } + } + *q = '\0'; + return result; +} +int main(int argc, char **argv) +{ + if (argc != 2) + { + fprintf(stderr, "Usage: ./test path\n"); + exit(-1); + } + printf("%s\n", simplifyPath(argv[1])); + return 0; +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/70_\347\256\200\345\214\226\350\267\257\345\276\204/solution.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/70_\347\256\200\345\214\226\350\267\257\345\276\204/solution.json" new file mode 100644 index 000000000..7321a63cd --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/70_\347\256\200\345\214\226\350\267\257\345\276\204/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "3afec91807524524a677f4194175dafb" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/70_\347\256\200\345\214\226\350\267\257\345\276\204/solution.md" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/70_\347\256\200\345\214\226\350\267\257\345\276\204/solution.md" new file mode 100644 index 000000000..d711a0159 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/70_\347\256\200\345\214\226\350\267\257\345\276\204/solution.md" @@ -0,0 +1,225 @@ +# 简化路径 +

      给你一个字符串 path ,表示指向某一文件或目录的 Unix 风格 绝对路径 (以 '/' 开头),请你将其转化为更加简洁的规范路径。

      在 Unix 风格的文件系统中,一个点(.)表示当前目录本身;此外,两个点 (..) 表示将目录切换到上一级(指向父目录);两者都可以是复杂相对路径的组成部分。任意多个连续的斜杠(即,'//')都被视为单个斜杠 '/' 。 对于此问题,任何其他格式的点(例如,'...')均被视为文件/目录名称。

      请注意,返回的 规范路径 必须遵循下述格式:

      • 始终以斜杠 '/' 开头。
      • 两个目录名之间必须只有一个斜杠 '/'
      • 最后一个目录名(如果存在)不能 '/' 结尾。
      • 此外,路径仅包含从根目录到目标文件或目录的路径上的目录(即,不含 '.''..')。

      返回简化后得到的 规范路径

       

      示例 1:

      输入:path = "/home/"
      输出:
      "/home"
      解释:
      注意,最后一个目录名后面没有斜杠。

      示例 2:

      输入:path = "/../"
      输出:
      "/"
      解释:
      从根目录向上一级是不可行的,因为根目录是你可以到达的最高级。

      示例 3:

      输入:path = "/home//foo/"
      输出:
      "/home/foo"
      解释:
      在规范路径中,多个连续斜杠需要用一个斜杠替换。

      示例 4:

      输入:path = "/a/./b/../../c/"
      输出:
      "/c"

       

      提示:

      • 1 <= path.length <= 3000
      • path 由英文字母,数字,'.''/''_' 组成。
      • path 是一个有效的 Unix 风格绝对路径。
      +

      以下错误的选项是?

      +## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + + string path = "/a/./b/../../c/"; + string res; + res = sol.simplifyPath(path); + cout << res; + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +public: + string names[1500]; + int temp = 0, endflag = 0; + void solve(string s, int start, int end, int n) + { + for (int i = start; i < n; i++) + { + if (s[i] == '/' && s[i + 1] != '/') + { + start = i + 1; + } + if (s[i] != '/' && s[i + 1] == '/') + { + end = i; + endflag = 1; + } + else if (s[i] != '/' && i == n - 1) + { + end = i; + endflag = 1; + } + if (endflag == 1) + { + string name = s.substr(start, end - start + 1); + if (name == "..") + { + if (temp > 0) + temp--; + } + else + { + temp = temp + 1; + names[temp] = name; + } + start = end + 1; + endflag = 0; + } + } + } + string simplifyPath(string path) + { + int n = path.length(); + names[temp] = "*"; + for (int i = n - 1; i >= 0; i++) + { + if (path[i] == '/') + continue; + if (path[i] != '/') + { + path = path.substr(0, i - 0 + 1); + break; + } + } + solve(path, 0, n - 1, n); + string str = ""; + if (temp == 0) + { + str = "/"; + } + else + { + for (int i = 0; i <= temp - 1; i++) + { + if (i == 0) + { + str += "/"; + } + else + { + str = str + names[i] + "/"; + } + } + str = str + names[temp]; + } + return str; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + string simplifyPath(string path) + { + if (path == "/") + return "/"; + + for (int i = 0; i < path.size(); i++) + { + if (path[i] == '/') + path[i] = ' '; + } + vector stack; + istringstream str(path); + string buf; + while (str >> buf) + { + if (buf == ".." && !stack.empty()) + stack.pop_back(); + else if (buf != "." && buf != "..") + stack.push_back(buf); + } + if (stack.empty()) + return "/"; + string res; + for (int i = 0; i < stack.size(); i++) + { + res = res + "/" + stack[i]; + } + return res; + } +}; +``` + +### B +```cpp +class Solution +{ +public: + string simplifyPath(string path) + { + stringstream ss(path); + vector strs; + strs.reserve(20); + string curr; + while (getline(ss, curr, '/')) + { + if (curr != "." && curr != "") + { + if (curr != "..") + { + strs.push_back(curr); + } + else if (!strs.empty()) + { + strs.pop_back(); + } + } + } + + if (!strs.empty()) + { + string res = ""; + for (string str : strs) + { + res.append("/"); + res.append(str); + } + return res; + } + else + { + + return "/"; + } + } +}; +``` + +### C +```cpp +class Solution +{ +public: + string simplifyPath(string path) + { + vector tmp; + int i = 0; + while (i < path.size()) + { + while (path[i] == '/') + ++i; + if (i == path.size()) + break; + int start = i; + while (path[i] != '/' && i < path.size()) + ++i; + string s = path.substr(start, i - start); + if (s == "..") + { + if (tmp.size() != 0) + tmp.pop_back(); + } + else if (s != ".") + tmp.push_back(s); + } + if (tmp.empty()) + return "/"; + string ans; + for (int i = 0; i < tmp.size(); ++i) + ans += '/' + tmp[i]; + return ans; + } +}; +``` diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/config.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/config.json" new file mode 100644 index 000000000..d14779381 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "柱状图中最大的矩形" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "柱状图中最大的矩形" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/desc.html" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/desc.html" new file mode 100644 index 000000000..2bcf9513b --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/desc.html" @@ -0,0 +1 @@ +

      给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。

      求在该柱状图中,能够勾勒出来的矩形的最大面积。

       

      以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为 [2,1,5,6,2,3]

       

      图中阴影部分为所能勾勒出的最大矩形面积,其面积为 10 个单位。

       

      示例:

      输入: [2,1,5,6,2,3]
      输出:
      10
      \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/solution.cpp" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/solution.cpp" new file mode 100644 index 000000000..0e7758843 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/solution.cpp" @@ -0,0 +1,42 @@ +#include +#include +static int largestRectangleArea(int *heights, int heightsSize) +{ + int *indexes = malloc(heightsSize * sizeof(int)); + int *left = malloc(heightsSize * sizeof(int)); + int *right = malloc(heightsSize * sizeof(int)); + int i, pos = 0; + for (i = 0; i < heightsSize; i++) + { + while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) + { + pos--; + } + left[i] = pos == 0 ? -1 : indexes[pos - 1]; + indexes[pos++] = i; + } + pos = 0; + for (i = heightsSize - 1; i >= 0; i--) + { + while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) + { + pos--; + } + right[i] = pos == 0 ? heightsSize : indexes[pos - 1]; + indexes[pos++] = i; + } + int max_area = 0; + for (i = 0; i < heightsSize; i++) + { + int area = heights[i] * (right[i] - left[i] - 1); + max_area = area > max_area ? area : max_area; + } + return max_area; +} +int main(void) +{ + int nums[] = {2, 1, 5, 6, 2, 3}; + int count = sizeof(nums) / sizeof(*nums); + printf("%d\n", largestRectangleArea(nums, count)); + return 0; +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/solution.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/solution.json" new file mode 100644 index 000000000..a00ba3745 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "869c530dafc545fcbc074e8eb379b2cc" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/solution.md" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/solution.md" new file mode 100644 index 000000000..978359db3 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/4.leetcode\346\240\210\344\270\216\351\230\237\345\210\227/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/solution.md" @@ -0,0 +1,166 @@ +# 柱状图中最大的矩形 +

      给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。

      求在该柱状图中,能够勾勒出来的矩形的最大面积。

       

      以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为 [2,1,5,6,2,3]

       

      图中阴影部分为所能勾勒出的最大矩形面积,其面积为 10 个单位。

       

      示例:

      输入: [2,1,5,6,2,3]
      输出:
      10
      +

      以下错误的选项是?

      +## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + vector heights = {2, 1, 5, 6, 2, 3}; + int res; + res = sol.largestRectangleArea(heights); + cout << res << endl; + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +public: + int largestRectangleArea(vector &heights) + { + if (heights.empty()) + return 0; + stack st; + heights.push_back(0); + int res = 0; + for (int i = 0; i < heights.size(); i++) + { + while (!st.empty() && heights[i] < heights[st.top()]) + { + int curHeight = heights[st.top()]; + st.pop(); + int width = st.empty() ? i : i - st.top(); + if (width * curHeight > res) + res = width * curHeight; + } + st.push(i); + } + return res; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + int largestRectangleArea(vector &heights) + { + int sz = heights.size(); + int ma = 0; + for (int i = 0; i < sz; i++) + { + int len = 1; + int hei = heights[i]; + int sta = i - 1, en = i + 1; + while (sta >= 0 && heights[sta] >= hei) + { + len++; + sta--; + } + while (en < sz && heights[en] >= hei) + { + len++; + en++; + } + ma = max(ma, len * hei); + } + return ma; + } +}; +``` + +### B +```cpp +class Solution +{ +public: + int largestRectangleArea(vector &heights) + { + int n = heights.size(); + + vector left(n), right(n, n); + + stack mono_stack; + + for (int i = 0; i < n; ++i) + { + + while (!mono_stack.empty() && heights[mono_stack.top()] >= heights[i]) + { + right[mono_stack.top()] = i; + mono_stack.pop(); + } + + left[i] = (mono_stack.empty() ? -1 : mono_stack.top()); + + mono_stack.push(i); + } + + int ans = 0; + for (int i = 0; i < n; ++i) + { + + ans = max(ans, (right[i] - left[i] - 1) * heights[i]); + } + return ans; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + int largestRectangleArea(vector &heights) + { + int n = heights.size(); + stack index; + int area = 0; + for (int i = 0; i < heights.size(); i++) + { + if (index.empty() || heights[index.top()] < heights[i]) + index.push(i); + else + { + while (!index.empty() && heights[index.top()] >= heights[i]) + { + int tmp = index.top(); + index.pop(); + int length = 0; + if (index.empty()) + length = i; + else + length = i - index.top() - 1; + area = max(area, length * heights[tmp]); + } + index.push(i); + } + } + while (!index.empty()) + { + int tmp = index.top(); + index.pop(); + int length = 0; + if (index.empty()) + length = n; + else + length = n - index.top() - 1; + area = max(area, length * heights[tmp]); + } + return area; + } +}; +``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/config.json" new file mode 100644 index 000000000..3fe77316b --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "最小路径和" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "最小路径和" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/desc.html" new file mode 100644 index 000000000..94ffa23c2 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/desc.html" @@ -0,0 +1 @@ +

      给定一个包含非负整数的 m x n 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。

      说明:每次只能向下或者向右移动一步。

       

      示例 1:

      输入:grid = [[1,3,1],[1,5,1],[4,2,1]]
      输出:
      7
      解释:
      因为路径 1→3→1→1→1 的总和最小。

      示例 2:

      输入:grid = [[1,2,3],[4,5,6]]
      输出:
      12

       

      提示:

      • m == grid.length
      • n == grid[i].length
      • 1 <= m, n <= 200
      • 0 <= grid[i][j] <= 100
      \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/solution.cpp" new file mode 100644 index 000000000..816e8b610 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/solution.cpp" @@ -0,0 +1,56 @@ +#include +#include +#include +static inline int min(int a, int b) +{ + return a < b ? a : b; +} +int minPathSum(int **grid, int gridRowSize, int gridColSize) +{ + int i, j; + int **dp = malloc(gridRowSize * sizeof(int *)); + for (i = 0; i < gridRowSize; i++) + { + dp[i] = malloc(gridColSize * sizeof(int)); + } + dp[0][0] = grid[0][0]; + int sum = dp[0][0]; + for (i = 1; i < gridRowSize; i++) + { + sum += grid[i][0]; + dp[i][0] = sum; + } + sum = dp[0][0]; + for (i = 1; i < gridColSize; i++) + { + sum += grid[0][i]; + dp[0][i] = sum; + } + for (i = 1; i < gridRowSize; i++) + { + for (j = 1; j < gridColSize; j++) + { + dp[i][j] = grid[i][j] + min(dp[i - 1][j], dp[i][j - 1]); + } + } + return dp[gridRowSize - 1][gridColSize - 1]; +} +int main(int argc, char **argv) +{ + int i, j; + int row = argc - 1; + int col = strlen(argv[1]); + int **grid = malloc(row * sizeof(int *)); + for (i = 0; i < row; i++) + { + grid[i] = malloc(col * sizeof(int)); + for (j = 0; j < col; j++) + { + grid[i][j] = argv[i + 1][j] - '0'; + printf("%d ", grid[i][j]); + } + printf("\n"); + } + printf("%d\n", minPathSum(grid, row, col)); + return 0; +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/solution.json" new file mode 100644 index 000000000..b1394af80 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "a9c26e3672b44267a074bf551bc8b987" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/solution.md" new file mode 100644 index 000000000..6ef90b51e --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/solution.md" @@ -0,0 +1,154 @@ +# 最小路径和 +

      给定一个包含非负整数的 m x n 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。

      说明:每次只能向下或者向右移动一步。

       

      示例 1:

      输入:grid = [[1,3,1],[1,5,1],[4,2,1]]
      输出:
      7
      解释:
      因为路径 1→3→1→1→1 的总和最小。

      示例 2:

      输入:grid = [[1,2,3],[4,5,6]]
      输出:
      12

       

      提示:

      • m == grid.length
      • n == grid[i].length
      • 1 <= m, n <= 200
      • 0 <= grid[i][j] <= 100
      +

      以下错误的选项是?

      +## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + + int a = 3, b = 3; + vector> grid = vector>(a, vector(b)) = {{1, 3, 1}, {1, 5, 1}, {4, 2, 1}}; + int res; + res = sol.minPathSum(grid); + cout << res; + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +private: + int m, n; + int memo[100][100]; + +public: + int minPathSum(vector> &grid) + { + m = grid.size(), n = grid[0].size(); + + for (int i = 0; i < m; i++) + { + memset(memo[i], -1, sizeof(int) * n); + } + return dfs(grid, 0, 0); + } + + int dfs(vector> &grid, int r, int c) + { + + if (r < 0 || r >= m || c < 0 || c >= n) + return 1000000; + + if (memo[r][c] != -1) + return memo[r][c]; + + if (r == m - 1 && c == n - 1) + { + memo[r][c] = grid[m][n - 1]; + return memo[r][c]; + } + + int right = dfs(grid, r, c + 1); + int down = dfs(grid, r + 1, c); + memo[r][c] = min(right, down) + grid[r][c]; + return memo[r][c]; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + int minPathSum(vector> &grid) + { + int row = grid.size(); + int col = grid[0].size(); + vector f(col, 0); + + for (int i = 0; i < row; ++i) + { + f[0] = f[0] + grid[i][0]; + for (int j = 1; j < col; ++j) + { + if (i == 0) + f[j] = f[j - 1] + grid[i][j]; + else + f[j] = min(f[j - 1], f[j]) + grid[i][j]; + } + } + return f[col - 1]; + } +}; +``` + +### B +```cpp +class Solution +{ +public: + int minPathSum(vector> &grid) + { + int row = grid.size(); + int column = grid[0].size(); + for (int i = 1; i < column; ++i) + { + grid[0][i] = grid[0][i - 1] + grid[0][i]; + } + for (int i = 1; i < row; ++i) + { + grid[i][0] = grid[i - 1][0] + grid[i][0]; + } + for (int i = 1; i < row; ++i) + { + for (int j = 1; j < column; ++j) + { + int temp = grid[i - 1][j] > grid[i][j - 1] ? grid[i][j - 1] : grid[i - 1][j]; + grid[i][j] = grid[i][j] + temp; + } + } + return grid[row - 1][column - 1]; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + int minPathSum(vector> &grid) + { + if (grid.size() == 0) + return 0; + int m = grid.size(); + int n = grid[0].size(); + vector> m_memo = vector>(m + 1, vector(n + 1, 0)); + + for (int i = n - 1; i >= 0; --i) + m_memo[m - 1][i] = grid[m - 1][i] + m_memo[m - 1][i + 1]; + for (int j = m - 1; j >= 0; --j) + m_memo[j][n - 1] = grid[j][n - 1] + m_memo[j + 1][n - 1]; + + for (int i = m - 2; i >= 0; --i) + { + for (int j = n - 2; j >= 0; --j) + { + m_memo[i][j] = grid[i][j] + min(m_memo[i][j + 1], m_memo[i + 1][j]); + } + } + return m_memo[0][0]; + } +}; +``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/69_\347\210\254\346\245\274\346\242\257/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/69_\347\210\254\346\245\274\346\242\257/config.json" new file mode 100644 index 000000000..6279fcabd --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/69_\347\210\254\346\245\274\346\242\257/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "爬楼梯" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "爬楼梯" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/69_\347\210\254\346\245\274\346\242\257/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/69_\347\210\254\346\245\274\346\242\257/desc.html" new file mode 100644 index 000000000..5a5fba907 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/69_\347\210\254\346\245\274\346\242\257/desc.html" @@ -0,0 +1 @@ +

      假设你正在爬楼梯。需要 n 阶你才能到达楼顶。

      每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?

      注意:给定 n 是一个正整数。

      示例 1:

      输入: 2
      输出:
      2
      解释:
      有两种方法可以爬到楼顶。1. 1 阶 + 1 阶2. 2 阶

      示例 2:

      输入: 3
      输出:
      3
      解释:
      有三种方法可以爬到楼顶。1. 1 阶 + 1 阶 + 1 阶2. 1 阶 + 2 阶3. 2 阶 + 1 阶
      \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/69_\347\210\254\346\245\274\346\242\257/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/69_\347\210\254\346\245\274\346\242\257/solution.cpp" new file mode 100644 index 000000000..b6bf8ef90 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/69_\347\210\254\346\245\274\346\242\257/solution.cpp" @@ -0,0 +1,19 @@ +#include +using namespace std; +class Solution +{ +public: + int climbStairs(int n) + { + int a = 1; + int b = 2; + int c = 0; + for (int i = 3; i <= n; i++) + { + c = a + b; + a = b; + b = c; + } + return n == 1 ? a : (n == 2 ? b : c); + } +}; \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/69_\347\210\254\346\245\274\346\242\257/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/69_\347\210\254\346\245\274\346\242\257/solution.json" new file mode 100644 index 000000000..7993de011 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/69_\347\210\254\346\245\274\346\242\257/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "e208713d1240406dade9a52e0d45fda2" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/69_\347\210\254\346\245\274\346\242\257/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/69_\347\210\254\346\245\274\346\242\257/solution.md" new file mode 100644 index 000000000..1ad125255 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/69_\347\210\254\346\245\274\346\242\257/solution.md" @@ -0,0 +1,102 @@ +# 爬楼梯 +

      假设你正在爬楼梯。需要 n 阶你才能到达楼顶。

      每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?

      注意:给定 n 是一个正整数。

      示例 1:

      输入: 2
      输出:
      2
      解释:
      有两种方法可以爬到楼顶。1. 1 阶 + 1 阶2. 2 阶

      示例 2:

      输入: 3
      输出:
      3
      解释:
      有三种方法可以爬到楼顶。1. 1 阶 + 1 阶 + 1 阶2. 1 阶 + 2 阶3. 2 阶 + 1 阶
      +

      以下错误的选项是?

      +## aop +### before +```cpp +#include +using namespace std; + +``` +### after +```cpp +int main() +{ + Solution sol; + + int x = 3; + int res; + res = sol.climbStairs(x); + cout << res; + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +public: + int climbStairs(int n) + { + double sqrt5 = sqrt(5); + double fibn = pow((1 + sqrt5) / 2, n) - pow((1 - sqrt5) / 2, n + 1); + return (int)(fibn / sqrt5); + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + int climbStairs(int n) + { + vector s; + s.push_back(1); + s.push_back(2); + if (n == 1) + return 1; + if (n == 2) + return 2; + for (int i = 2; i < n; i++) + { + s.push_back(s[i - 1] + s[i - 2]); + } + return s[n - 1]; + } +}; +``` + +### B +```cpp +class Solution +{ +public: + int climbStairs(int n) + { + vector res(n + 1, 0); + res[1] = 1; + res[2] = 2; + for (int i = 3; i <= n; i++) + res[i] = res[i - 1] + res[i - 2]; + return res[n]; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + int climbStairs(int n) + { + if (n == 1) + { + return 1; + } + int first = 1; + int second = 2; + for (int i = 3; i <= n; i++) + { + int third = first + second; + first = second; + second = third; + } + return second; + } +}; +``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/71_\347\274\226\350\276\221\350\267\235\347\246\273/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/71_\347\274\226\350\276\221\350\267\235\347\246\273/config.json" new file mode 100644 index 000000000..9c899c33e --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/71_\347\274\226\350\276\221\350\267\235\347\246\273/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "编辑距离" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "编辑距离" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/71_\347\274\226\350\276\221\350\267\235\347\246\273/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/71_\347\274\226\350\276\221\350\267\235\347\246\273/desc.html" new file mode 100644 index 000000000..d4f47ed6a --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/71_\347\274\226\350\276\221\350\267\235\347\246\273/desc.html" @@ -0,0 +1 @@ +

      给你两个单词 word1 和 word2,请你计算出将 word1 转换成 word2 所使用的最少操作数 。

      你可以对一个单词进行如下三种操作:

      • 插入一个字符
      • 删除一个字符
      • 替换一个字符

       

      示例 1:

      输入:word1 = "horse", word2 = "ros"
      输出:
      3
      解释:
      horse -> rorse (将 'h' 替换为 'r')rorse -> rose (删除 'r')rose -> ros (删除 'e')

      示例 2:

      输入:word1 = "intention", word2 = "execution"
      输出:
      5
      解释:
      intention -> inention (删除 't')inention -> enention (将 'i' 替换为 'e')enention -> exention (将 'n' 替换为 'x')exention -> exection (将 'n' 替换为 'c')exection -> execution (插入 'u')

       

      提示:

      • 0 <= word1.length, word2.length <= 500
      • word1word2 由小写英文字母组成
      \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/71_\347\274\226\350\276\221\350\267\235\347\246\273/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/71_\347\274\226\350\276\221\350\267\235\347\246\273/solution.cpp" new file mode 100644 index 000000000..e9e0e674c --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/71_\347\274\226\350\276\221\350\267\235\347\246\273/solution.cpp" @@ -0,0 +1,36 @@ +#include +using namespace std; +class Solution +{ +public: + int minDistance(string word1, string word2) + { + int l1 = word1.length(); + int l2 = word2.length(); + vector dp(l2 + 1); + for (int i = 0; i <= l2; i++) + { + dp[i] = i; + } + int up = 0; + for (int i = 1; i <= l1; i++) + { + int left_up = dp[0]; + dp[0] = i; + for (int j = 1; j <= l2; j++) + { + up = dp[j]; + if (word1[i - 1] == word2[j - 1]) + { + dp[j] = left_up; + } + else + { + dp[j] = 1 + min(left_up, min(up, dp[j - 1])); + } + left_up = up; + } + } + return dp[l2]; + } +}; \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/71_\347\274\226\350\276\221\350\267\235\347\246\273/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/71_\347\274\226\350\276\221\350\267\235\347\246\273/solution.json" new file mode 100644 index 000000000..815332d6d --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/71_\347\274\226\350\276\221\350\267\235\347\246\273/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "38e847ac1ca048a8b39a99e90b9c788e" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/71_\347\274\226\350\276\221\350\267\235\347\246\273/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/71_\347\274\226\350\276\221\350\267\235\347\246\273/solution.md" new file mode 100644 index 000000000..b60d4b81f --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/71_\347\274\226\350\276\221\350\267\235\347\246\273/solution.md" @@ -0,0 +1,187 @@ +# 编辑距离 +

      给你两个单词 word1 和 word2,请你计算出将 word1 转换成 word2 所使用的最少操作数 。

      你可以对一个单词进行如下三种操作:

      • 插入一个字符
      • 删除一个字符
      • 替换一个字符

       

      示例 1:

      输入:word1 = "horse", word2 = "ros"
      输出:
      3
      解释:
      horse -> rorse (将 'h' 替换为 'r')rorse -> rose (删除 'r')rose -> ros (删除 'e')

      示例 2:

      输入:word1 = "intention", word2 = "execution"
      输出:
      5
      解释:
      intention -> inention (删除 't')inention -> enention (将 'i' 替换为 'e')enention -> exention (将 'n' 替换为 'x')exention -> exection (将 'n' 替换为 'c')exection -> execution (插入 'u')

       

      提示:

      • 0 <= word1.length, word2.length <= 500
      • word1word2 由小写英文字母组成
      +

      以下错误的选项是?

      +## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + + string word1 = "horse"; + string word2 = "ros"; + + int res; + res = sol.minDistance(word1, word2); + cout << res; + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +public: + int minDistance(string word1, string word2) + { + int len1 = word1.size(); + int len2 = word2.size(); + int **dp = new int *[len1 + 1]; + for (int i = 0; i < len1 + 1; i++) + dp[i] = new int[len2 + 1]; + for (int i = 0; i < len1 + 1; i++) + dp[i - 1][0] = i; + for (int i = 1; i < len2 + 1; i++) + dp[0][i - 1] = i; + for (int i = 1; i < len1 + 1; i++) + { + for (int j = 1; j < len2 + 1; j++) + { + if (word1[i - 1] == word2[j - 1]) + dp[i][j] = dp[i - 1][j - 1]; + else + dp[i][j] = (min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1); + } + } + return dp[len1][len2]; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + int minDistance(string word1, string word2) + { + int m = word1.size(), n = word2.size(); + + if (m == 0) + return n; + if (n == 0) + return m; + + int dp[m][n]; + bool w1 = false, w2 = false; + if (word1[0] == word2[0]) + { + w1 = true; + w2 = true; + dp[0][0] = 0; + } + else + dp[0][0] = 1; + for (int i = 1; i < m; i++) + { + if (!w1 && word1[i] == word2[0]) + { + w1 = true; + dp[i][0] = dp[i - 1][0]; + } + else + dp[i][0] = dp[i - 1][0] + 1; + } + for (int j = 1; j < n; j++) + { + if (!w2 && word1[0] == word2[j]) + { + w2 = true; + dp[0][j] = dp[0][j - 1]; + } + else + dp[0][j] = dp[0][j - 1] + 1; + } + + for (int i = 1; i < m; i++) + for (int j = 1; j < n; j++) + if (word1[i] == word2[j]) + dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]) + 1, dp[i - 1][j - 1]); + else + dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1; + return dp[m - 1][n - 1]; + } +}; +``` + +### B +```cpp +class Solution +{ +public: + int minDistance(string word1, string word2) + { + int n = word1.size(); + int m = word2.size(); + + if (n * m == 0) + { + return n + m; + } + + int d[n + 1][m + 1]; + for (int i = 0; i < n + 1; ++i) + { + d[i][0] = i; + } + for (int i = 0; i < m + 1; ++i) + { + d[0][i] = i; + } + + for (int i = 1; i < n + 1; ++i) + { + for (int j = 1; j < m + 1; ++j) + { + int left = d[i - 1][j] + 1; + int down = d[i][j - 1] + 1; + int left_down = d[i - 1][j - 1]; + if (word1[i - 1] != word2[j - 1]) + { + left_down += 1; + } + d[i][j] = min(left, min(down, left_down)); + } + } + return d[n][m]; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + int minDistance(string word1, string word2) + { + int m = word1.size(), n = word2.size(); + + vector> dp(m + 1, vector(n + 1, 0)); + + for (int i = 1; i <= n; ++i) + dp[0][i] = dp[0][i - 1] + 1; + for (int i = 1; i <= m; ++i) + dp[i][0] = dp[i - 1][0] + 1; + + for (int i = 1; i <= m; ++i) + { + for (int j = 1; j <= n; ++j) + { + if (word1[i - 1] == word2[j - 1]) + dp[i][j] = dp[i - 1][j - 1]; + else + dp[i][j] = min(min(dp[i - 1][j - 1], dp[i - 1][j]), dp[i][j - 1]) + 1; + } + } + return dp[m][n]; + } +}; +``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/84_\346\234\200\345\244\247\347\237\251\345\275\242/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/84_\346\234\200\345\244\247\347\237\251\345\275\242/config.json" new file mode 100644 index 000000000..85517481a --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/84_\346\234\200\345\244\247\347\237\251\345\275\242/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "最大矩形" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "最大矩形" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/84_\346\234\200\345\244\247\347\237\251\345\275\242/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/84_\346\234\200\345\244\247\347\237\251\345\275\242/desc.html" new file mode 100644 index 000000000..6d9bdc4aa --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/84_\346\234\200\345\244\247\347\237\251\345\275\242/desc.html" @@ -0,0 +1 @@ +

      给定一个仅包含 01 、大小为 rows x cols 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。

       

      示例 1:

      输入:matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
      输出:
      6
      解释:
      最大矩形如上图所示。

      示例 2:

      输入:matrix = []
      输出:
      0

      示例 3:

      输入:matrix = [["0"]]
      输出:
      0

      示例 4:

      输入:matrix = [["1"]]
      输出:
      1

      示例 5:

      输入:matrix = [["0","0"]]
      输出:
      0

       

      提示:

      • rows == matrix.length
      • cols == matrix[0].length
      • 0 <= row, cols <= 200
      • matrix[i][j]'0''1'
      \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/84_\346\234\200\345\244\247\347\237\251\345\275\242/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/84_\346\234\200\345\244\247\347\237\251\345\275\242/solution.cpp" new file mode 100644 index 000000000..51b472439 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/84_\346\234\200\345\244\247\347\237\251\345\275\242/solution.cpp" @@ -0,0 +1,73 @@ +#include +#include +#include +#include +static inline int max(int a, int b) +{ + return a > b ? a : b; +} +static int area_calc(int *heights, int size) +{ + int *indexes = malloc(size * sizeof(int)); + int *lhist = malloc(size * sizeof(int)); + int *rhist = malloc(size * sizeof(int)); + int i, pos = 0; + for (i = 0; i < size; i++) + { + while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) + { + pos--; + } + lhist[i] = pos == 0 ? -1 : indexes[pos - 1]; + indexes[pos++] = i; + } + pos = 0; + for (i = size - 1; i >= 0; i--) + { + while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) + { + pos--; + } + rhist[i] = pos == 0 ? size : indexes[pos - 1]; + indexes[pos++] = i; + } + int max_area = 0; + for (i = 0; i < size; i++) + { + int area = heights[i] * (rhist[i] - lhist[i] - 1); + max_area = max(area, max_area); + } + return max_area; +} +static int maximalRectangle(char **matrix, int matrixRowSize, int matrixColSize) +{ + int i, j, max_area = 0; + int *heights = malloc(matrixColSize * sizeof(int)); + memset(heights, 0, matrixColSize * sizeof(int)); + for (i = 0; i < matrixRowSize; i++) + { + for (j = 0; j < matrixColSize; j++) + { + heights[j] = matrix[i][j] == '1' ? heights[j] + 1 : 0; + } + max_area = max(max_area, area_calc(heights, matrixColSize)); + } + return max_area; +} +int main(int argc, char **argv) +{ + if (argc < 2) + { + fprintf(stderr, "Usage: ./test row1 row2...\n"); + exit(-1); + } + int i, j; + int row_size = argc - 1; + int col_size = strlen(argv[1]); + for (i = 0; i < row_size; i++) + { + printf("%s\n", argv[i + 1]); + } + printf("%d\n", maximalRectangle(argv + 1, argc - 1, strlen(argv[1]))); + return 0; +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/84_\346\234\200\345\244\247\347\237\251\345\275\242/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/84_\346\234\200\345\244\247\347\237\251\345\275\242/solution.json" new file mode 100644 index 000000000..f173985a1 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/84_\346\234\200\345\244\247\347\237\251\345\275\242/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "b2a08644fd54460fb4d8abcb56c527f1" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/84_\346\234\200\345\244\247\347\237\251\345\275\242/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/84_\346\234\200\345\244\247\347\237\251\345\275\242/solution.md" new file mode 100644 index 000000000..c1613e6b5 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/84_\346\234\200\345\244\247\347\237\251\345\275\242/solution.md" @@ -0,0 +1,225 @@ +# 最大矩形 +

      给定一个仅包含 01 、大小为 rows x cols 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。

       

      示例 1:

      输入:matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
      输出:
      6
      解释:
      最大矩形如上图所示。

      示例 2:

      输入:matrix = []
      输出:
      0

      示例 3:

      输入:matrix = [["0"]]
      输出:
      0

      示例 4:

      输入:matrix = [["1"]]
      输出:
      1

      示例 5:

      输入:matrix = [["0","0"]]
      输出:
      0

       

      提示:

      • rows == matrix.length
      • cols == matrix[0].length
      • 0 <= row, cols <= 200
      • matrix[i][j]'0''1'
      +

      以下错误的选项是?

      +## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + vector> matrix = {{'1', '0', '1', '0', '0'}, + {'1', '0', '1', '1', '1'}, + {'1', '1', '1', '1', '1'}, + {'1', '0', '0', '1', '0'}}; + int res; + res = sol.maximalRectangle(matrix); + cout << res << endl; + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +public: + int largestRectangleArea(vector &heights) + { + stack h; + heights.push_back(0); + int ans = 0, hsize = heights.size(); + for (int i = 0; i < hsize; i++) + { + while (!h.empty() && heights[h.top()] > heights[i]) + { + int top = h.top(); + h.pop(); + ans = max(ans, heights[top] * (h.empty() ? i : (i - h.top()))); + } + h.push(i); + } + return ans; + } + + int maximalRectangle(vector> &matrix) + { + if (matrix.empty()) + return 0; + int n = matrix.size(), m = matrix[0].size(), ans = 0; + vector> num(n, vector(m, 0)); + for (int j = 0; j < m; j++) + { + num[0][j] = (matrix[0][j] == '0') ? 0 : 1; + for (int i = 1; i < n; i++) + num[i][j] = (matrix[i][j] == '0') ? 0 : num[i - 1][j] + 1; + } + for (int i = 0; i < n; i++) + { + int area = largestRectangleArea(num[i]); + ans = max(ans, area); + } + return ans; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + int maximalRectangle(vector> &matrix) + { + int res = 0; + vector height; + for (int i = 0; i < matrix.size(); ++i) + { + height.resize(matrix[i].size()); + for (int j = 0; j < matrix[i].size(); ++j) + { + height[j] = matrix[i][j] == '0' ? 0 : (1 + height[j]); + } + res = max(res, largestRectangleArea(height)); + } + return res; + } + + int largestRectangleArea(vector &heights) + { + if (heights.empty()) + return 0; + stack st; + heights.push_back(0); + int res0 = 0; + for (int i = 0; i < heights.size(); i++) + { + while (!st.empty() && heights[i] < heights[st.top()]) + { + int curHeight = heights[st.top()]; + st.pop(); + int width = st.empty() ? i : i - st.top() - 1; + if (width * curHeight > res0) + res0 = width * curHeight; + } + st.push(i); + } + return res0; + } +}; +``` + +### B +```cpp +class Solution +{ +public: + int maximalRectangle(vector> &matrix) + { + if (matrix.size() == 0) + { + return 0; + } + int m = matrix.size(); + int n = matrix[0].size(); + int left[n]; + int right[n]; + int height[n]; + + memset(right, n, sizeof(right)); + memset(left, n, sizeof(left)); + memset(height, n, sizeof(height)); + + int maxarea = 0; + for (int i = 0; i < m; ++i) + { + int cur_left = 0, cur_right = n; + + for (int j = 0; j < n; ++j) + { + if (matrix[i][j] == '1') + { + height[j]++; + } + else + { + height[j] = 0; + } + } + + for (int j = 0; j < n; ++j) + { + if (matrix[i][j] == '1') + { + left[j] = max(left[j], cur_left); + } + else + { + left[j] = 0, cur_left = j + 1; + } + } + + for (int j = n - 1; j >= 0; --j) + { + if (matrix[i][j] == '1') + { + right[j] = min(right[j], cur_right); + } + else + { + right[j] = n; + cur_right = j; + } + } + + for (int j = 0; j < n; ++j) + { + maxarea = max(maxarea, (right[j] - left[j]) * height[j]); + } + } + return maxarea; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + int maximalRectangle(vector> &matrix) + { + if (matrix.size() == 0) + { + return 0; + } + int maxarea = 0; + int dp[matrix.size()][matrix[0].size()]; + memset(dp, 0, sizeof(dp)); + + for (int i = 0; i < matrix.size(); ++i) + { + for (int j = 0; j < matrix[0].size(); ++j) + { + if (matrix[i][j] == '1') + { + dp[i][j] = j == 0 ? 1 : dp[i][j - 1] + 1; + int width = dp[i][j]; + + for (int k = i; k >= 0; k--) + { + width = min(width, dp[k][j]); + maxarea = max(maxarea, width * (i - k + 1)); + } + } + } + } + return maxarea; + } +}; +``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/config.json" new file mode 100644 index 000000000..564ae08fe --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "扰乱字符串" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "扰乱字符串" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/desc.html" new file mode 100644 index 000000000..dcca706ce --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/desc.html" @@ -0,0 +1,59 @@ +
      使用下面描述的算法可以扰乱字符串 s 得到字符串 t : +
        +
      1. 如果字符串的长度为 1 ,算法停止
      2. +
      3. 如果字符串的长度 > 1 ,执行下述步骤: +
          +
        • 在一个随机下标处将字符串分割成两个非空的子字符串。即,如果已知字符串 s ,则可以将其分成两个子字符串 xy + ,且满足 s = x + y
        • +
        • 随机 决定是要「交换两个子字符串」还是要「保持这两个子字符串的顺序不变」。即,在执行这一步骤之后,s 可能是 + s = x + y 或者 s = y + x 。 +
        • +
        • xy 这两个子字符串上继续从步骤 1 开始递归执行此算法。
        • +
        +
      4. +
      + +

      给你两个 长度相等 的字符串 s1 + 和 s2,判断 s2 是否是 s1 的扰乱字符串。如果是,返回 + true ;否则,返回 false 。 +

      + +

       

      + +

      示例 1:

      + +
      输入:s1 = "great", s2 = "rgeat"
      +
      输出:
      true +
      解释:
      s1 上可能发生的一种情形是: +"great" --> "gr/eat" // 在一个随机下标处分割得到两个子字符串 +"gr/eat" --> "gr/eat" // 随机决定:「保持这两个子字符串的顺序不变」 +"gr/eat" --> "g/r / e/at" // 在子字符串上递归执行此算法。两个子字符串分别在随机下标处进行一轮分割 +"g/r / e/at" --> "r/g / e/at" // 随机决定:第一组「交换两个子字符串」,第二组「保持这两个子字符串的顺序不变」 +"r/g / e/at" --> "r/g / e/ a/t" // 继续递归执行此算法,将 "at" 分割得到 "a/t" +"r/g / e/ a/t" --> "r/g / e/ a/t" // 随机决定:「保持这两个子字符串的顺序不变」 +算法终止,结果字符串和 s2 相同,都是 "rgeat" +这是一种能够扰乱 s1 得到 s2 的情形,可以认为 s2 是 s1 的扰乱字符串,返回 true +
      + +

      示例 2:

      + +
      输入:s1 = "abcde", s2 = "caebd"
      +
      输出:
      false +
      + +

      示例 3:

      + +
      输入:s1 = "a", s2 = "a"
      +
      输出:
      true +
      + +

       

      + +

      提示:

      + +
        +
      • s1.length == s2.length
      • +
      • 1 <= s1.length <= 30
      • +
      • s1s2 由小写英文字母组成
      • +
      +
      \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/solution.cpp" new file mode 100644 index 000000000..39b717ec8 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/solution.cpp" @@ -0,0 +1,63 @@ +#include +#include +#include +#include +static bool scramble(char *s1, int low1, int high1, char *s2, int low2, int high2) +{ + if (high1 - low1 != high2 - low2) + { + return false; + } + else if (!memcmp(s1 + low1, s2 + low2, high1 - low1 + 1)) + { + return true; + } + else + { + int i, c1[128] = {0}, c2[128] = {0}; + for (i = low1; i <= high1; i++) + { + c1[s1[i]]++; + } + for (i = low2; i <= high2; i++) + { + c2[s2[i]]++; + } + if (memcmp(c1, c2, 128 * sizeof(int))) + { + return false; + } + else + { + int len = high1 - low1 + 1; + for (i = 1; i < len; i++) + { + if (scramble(s1, low1, low1 + i - 1, s2, low2, low2 + i - 1) && + scramble(s1, low1 + i, high1, s2, low2 + i, high2)) + { + return true; + } + if (scramble(s1, low1, low1 + i - 1, s2, high2 - i + 1, high2) && + scramble(s1, low1 + i, high1, s2, low2, high2 - i)) + { + return true; + } + } + return false; + } + } +} +static bool isScramble(char *s1, char *s2) +{ + return scramble(s1, 0, strlen(s1) - 1, s2, 0, strlen(s2) - 1); +} +int main(int argc, char **argv) +{ + if (argc != 3) + { + fprintf(stderr, "Usage: ./test s1 s2\n"); + exit(-1); + } + printf("%s\n", isScramble(argv[1], argv[2]) ? "true" : "false"); + return 0; +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/solution.json" new file mode 100644 index 000000000..2355ee82c --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "ad842cfadb884aedbab6b6b8e8e60a11" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/solution.md" new file mode 100644 index 000000000..72efaedf1 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode\345\212\250\346\200\201\350\247\204\345\210\222/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/solution.md" @@ -0,0 +1,241 @@ +# 扰乱字符串 +
      使用下面描述的算法可以扰乱字符串 s 得到字符串 t : +
        +
      1. 如果字符串的长度为 1 ,算法停止
      2. +
      3. 如果字符串的长度 > 1 ,执行下述步骤: +
          +
        • 在一个随机下标处将字符串分割成两个非空的子字符串。即,如果已知字符串 s ,则可以将其分成两个子字符串 xy + ,且满足 s = x + y
        • +
        • 随机 决定是要「交换两个子字符串」还是要「保持这两个子字符串的顺序不变」。即,在执行这一步骤之后,s 可能是 + s = x + y 或者 s = y + x 。 +
        • +
        • xy 这两个子字符串上继续从步骤 1 开始递归执行此算法。
        • +
        +
      4. +
      + +

      给你两个 长度相等 的字符串 s1 + 和 s2,判断 s2 是否是 s1 的扰乱字符串。如果是,返回 + true ;否则,返回 false 。 +

      + +

       

      + +

      示例 1:

      + +
      输入:s1 = "great", s2 = "rgeat"
      +
      输出:
      true +
      解释:
      s1 上可能发生的一种情形是: +"great" --> "gr/eat" // 在一个随机下标处分割得到两个子字符串 +"gr/eat" --> "gr/eat" // 随机决定:「保持这两个子字符串的顺序不变」 +"gr/eat" --> "g/r / e/at" // 在子字符串上递归执行此算法。两个子字符串分别在随机下标处进行一轮分割 +"g/r / e/at" --> "r/g / e/at" // 随机决定:第一组「交换两个子字符串」,第二组「保持这两个子字符串的顺序不变」 +"r/g / e/at" --> "r/g / e/ a/t" // 继续递归执行此算法,将 "at" 分割得到 "a/t" +"r/g / e/ a/t" --> "r/g / e/ a/t" // 随机决定:「保持这两个子字符串的顺序不变」 +算法终止,结果字符串和 s2 相同,都是 "rgeat" +这是一种能够扰乱 s1 得到 s2 的情形,可以认为 s2 是 s1 的扰乱字符串,返回 true +
      + +

      示例 2:

      + +
      输入:s1 = "abcde", s2 = "caebd"
      +
      输出:
      false +
      + +

      示例 3:

      + +
      输入:s1 = "a", s2 = "a"
      +
      输出:
      true +
      + +

       

      + +

      提示:

      + +
        +
      • s1.length == s2.length
      • +
      • 1 <= s1.length <= 30
      • +
      • s1s2 由小写英文字母组成
      • +
      +
      +

      以下错误的选项是?

      +## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + string s1 = "great"; + string s2 = "rgeat"; + bool res; + res = sol.isScramble(s1, s2); + cout << res << endl; + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ + unordered_map memo; + +public: + bool isScramble(string s1, string s2) + { + if (memo.count(s1 + '_' + s2) != 0) + { + return memo[s1 + '_' + s2]; + } + if (s1 == s2) + { + memo[s1 + '_' + s2] = true; + return true; + } + else if (s1.size() == 1) + { + memo[s1 + '_' + s2] = false; + return false; + } + bool do_it = false; + bool not_do = false; + for (int i = 1; i < s1.size(); i++) + { + do_it = isScramble(s1.substr(0, i), s2.substr(s1.size() - 1, s1.size())); + not_do = isScramble(s1.substr(0, i), s2.substr(0, i)); + + if (do_it || not_do) + { + memo[s1 + '_' + s2] = true; + return true; + } + } + memo[s1 + '_' + s2] = false; + return false; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + bool isScramble(string s1, string s2) + { + int n1 = s1.length(), n2 = s2.length(); + if (n1 != n2) + return false; + vector>> dp(n1 + 1, vector>(n1 + 1, vector(n1 + 1, false))); + int i, j, k; + for (i = 1; i <= n1; i++) + { + for (j = 1; j <= n1; j++) + { + dp[i][j][1] = (s1[i - 1] == s2[j - 1]); + } + } + for (int len = 2; len <= n1; len++) + { + for (i = 1; i <= n1 && i + len <= n1 + 1; i++) + { + for (j = 1; j <= n1 && j + len <= n1 + 1; j++) + { + for (k = 1; k < len; k++) + { + if (dp[i][j][k] && dp[i + k][j + k][len - k]) + { + dp[i][j][len] = true; + break; + } + if (dp[i][j + len - k][k] && dp[i + k][j][len - k]) + { + dp[i][j][len] = true; + break; + } + } + } + } + } + + return dp[1][1][n1]; + } +}; +``` + +### B +```cpp +class Solution +{ +public: + bool isScramble(string s1, string s2) + { + + if (s1.size() != s2.size()) + return false; + if (s1 == s2) + return true; + vector hash(26, 0); + + for (int i = 0; i < s1.size(); i++) + hash.at(s1[i] - 'a')++; + + for (int j = 0; j < s2.size(); j++) + hash.at(s2[j] - 'a')--; + + for (int k = 0; k < 26; k++) + { + if (hash.at(k) != 0) + return false; + } + + for (int i = 1; i < s1.size(); i++) + { + if ( + (isScramble(s1.substr(0, i), s2.substr(0, i)) && isScramble(s1.substr(i, s1.size() - i), s2.substr(i, s1.size() - i))) || (isScramble(s1.substr(0, i), s2.substr(s1.size() - i)) && isScramble(s1.substr(i), s2.substr(0, s1.size() - i)))) + return true; + } + return false; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + bool isScramble(string s1, string s2) + { + if (s1.size() != s2.size()) + return false; + if (s1 == s2) + return true; + string str1 = s1, str2 = s2; + sort(str1.begin(), str1.end()); + sort(str2.begin(), str2.end()); + if (str1 != str2) + return false; + for (int i = 1; i < s1.size(); ++i) + { + string s11 = s1.substr(0, i); + string s12 = s1.substr(i); + string s21 = s2.substr(0, i); + string s22 = s2.substr(i); + if (isScramble(s11, s21) && isScramble(s12, s22)) + return true; + s21 = s2.substr(s2.size() - i); + s22 = s2.substr(0, s2.size() - i); + if (isScramble(s11, s21) && isScramble(s12, s22)) + return true; + } + return false; + } +}; +``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/config.json" new file mode 100644 index 000000000..734e8153f --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "文本左右对齐" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "文本左右对齐" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/desc.html" new file mode 100644 index 000000000..7c583861d --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/desc.html" @@ -0,0 +1,63 @@ +
      +

      给定一个单词数组和一个长度 maxWidth,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本。

      + +

      你应该使用“贪心算法”来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格 ' ' 填充,使得每行恰好有 maxWidth 个字符。 +

      + +

      要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。

      + +

      文本的最后一行应为左对齐,且单词之间不插入额外的空格。

      + +

      说明:

      + +
        +
      • 单词是指由非空格字符组成的字符序列。
      • +
      • 每个单词的长度大于 0,小于等于 maxWidth
      • +
      • 输入单词数组 words 至少包含一个单词。
      • +
      + +

      示例:

      + +
      输入:
      +    words = ["This", "is", "an", "example", "of", "text", "justification."]
      +    maxWidth = 16
      +
      输出:
      + [ +    "This    is    an", +    "example  of text", +    "justification.  " + ] +
      + +

      示例 2:

      + +
      输入:
      +    words = ["What","must","be","acknowledgment","shall","be"]
      +    maxWidth = 16
      +
      输出:
      + [ +   "What   must   be", +   "acknowledgment  ", +   "shall be        " + ] +
      解释:
      注意最后一行的格式应为 "shall be " 而不是 "shall be" + 因为最后一行应为左对齐,而不是左右两端对齐,第二行同样为左对齐,这是因为这行只包含一个单词。 +
      + +

      示例 3:

      + +
      输入:
      +    words = ["Science","is","what","we","understand","well","enough","to","explain",
      +             "to","a","computer.","Art","is","everything","else","we","do"]
      +    maxWidth = 20
      +
      输出:
      + [ +   "Science  is  what we", + "understand      well", +   "enough to explain to", +   "a  computer.  Art is", +   "everything  else  we", +   "do                  " + ] +
      +
      \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/solution.cpp" new file mode 100644 index 000000000..0a058a189 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/solution.cpp" @@ -0,0 +1,118 @@ +#include +#include +#include +static void line_fill(char *line, int len, char **words, int *word_lens, int max_size, + int even_spaces, int remain_spaces, int start, int end) +{ + int i, j; + char *p = line; + for (i = start; i < end; i++) + { + memcpy(p, words[i], word_lens[i]); + p += word_lens[i]; + if (i < end - 1) + { + for (j = 0; j < even_spaces; j++) + { + *p++ = ' '; + } + if (remain_spaces > 0) + { + *p++ = ' '; + remain_spaces--; + } + } + } + while (p - line < max_size) + { + *p++ = ' '; + } + *p++ = '\0'; +} +static char **fullJustify(char **words, int wordsSize, int maxWidth, int *returnSize) +{ + int i, j, k, cap = 100, count = 0; + char **lines = malloc(cap * sizeof(char *)); + char *buf = malloc(cap * (maxWidth + 1)); + for (i = 0; i < cap; i++) + { + lines[i] = buf + i * (maxWidth + 1); + } + int *word_lens = malloc(wordsSize * sizeof(int)); + for (i = 0; i < wordsSize; i++) + { + word_lens[i] = strlen(words[i]); + } + int wc = 0; + int len = 0; + int start = 0; + int chars = 0; + for (i = 0, j = 0; i < wordsSize; i++) + { + if (len + word_lens[i] > maxWidth) + { + int even_spaces = wc == 1 ? 0 : (maxWidth - chars) / (wc - 1); + int remain_spaces = wc == 1 ? 0 : (maxWidth - chars) % (wc - 1); + line_fill(lines[count], len, words, word_lens, maxWidth, even_spaces, remain_spaces, start, i); + count++; + wc = 1; + len = word_lens[i] + 1; + chars = word_lens[i]; + start = i; + } + else if (len + word_lens[i] == maxWidth) + { + chars += word_lens[i]; + int even_spaces = wc == 0 ? 0 : (maxWidth - chars) / wc; + int remain_spaces = wc == 0 ? 0 : (maxWidth - chars) % wc; + line_fill(lines[count], len, words, word_lens, maxWidth, even_spaces, remain_spaces, start, i + 1); + count++; + wc = 0; + len = 0; + chars = 0; + start = i + 1; + } + else + { + chars += word_lens[i]; + len += word_lens[i] + 1; + wc++; + } + } + if (wc > 0) + { + char *p = lines[count]; + for (i = start; i < start + wc; i++) + { + memcpy(p, words[i], word_lens[i]); + p += word_lens[i]; + if (i < start + wc - 1) + { + *p++ = ' '; + } + } + while (p - lines[count] < maxWidth) + { + *p++ = ' '; + } + *p++ = '\0'; + count++; + } + *returnSize = count; + return lines; +} +int main(int argc, char **argv) +{ + if (argc <= 2) + { + fprintf(stderr, "Usage: ./test maxsize words...\n"); + exit(-1); + } + int i, count; + char **lines = fullJustify(argv + 2, argc - 2, atoi(argv[1]), &count); + for (i = 0; i < count; i++) + { + printf("%s\n", lines[i]); + } + return 0; +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/solution.json" new file mode 100644 index 000000000..2542d6a80 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "f7276587952f4c079b9dec0fed935c1b" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/solution.md" new file mode 100644 index 000000000..6e0881b59 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/solution.md" @@ -0,0 +1,351 @@ +# 文本左右对齐 +
      +

      给定一个单词数组和一个长度 maxWidth,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本。

      + +

      你应该使用“贪心算法”来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格 ' ' 填充,使得每行恰好有 maxWidth 个字符。 +

      + +

      要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。

      + +

      文本的最后一行应为左对齐,且单词之间不插入额外的空格。

      + +

      说明:

      + +
        +
      • 单词是指由非空格字符组成的字符序列。
      • +
      • 每个单词的长度大于 0,小于等于 maxWidth
      • +
      • 输入单词数组 words 至少包含一个单词。
      • +
      + +

      示例:

      + +
      输入:
      +    words = ["This", "is", "an", "example", "of", "text", "justification."]
      +    maxWidth = 16
      +
      输出:
      + [ +    "This    is    an", +    "example  of text", +    "justification.  " + ] +
      + +

      示例 2:

      + +
      输入:
      +    words = ["What","must","be","acknowledgment","shall","be"]
      +    maxWidth = 16
      +
      输出:
      + [ +   "What   must   be", +   "acknowledgment  ", +   "shall be        " + ] +
      解释:
      注意最后一行的格式应为 "shall be " 而不是 "shall be" + 因为最后一行应为左对齐,而不是左右两端对齐,第二行同样为左对齐,这是因为这行只包含一个单词。 +
      + +

      示例 3:

      + +
      输入:
      +    words = ["Science","is","what","we","understand","well","enough","to","explain",
      +             "to","a","computer.","Art","is","everything","else","we","do"]
      +    maxWidth = 20
      +
      输出:
      + [ +   "Science  is  what we", + "understand      well", +   "enough to explain to", +   "a  computer.  Art is", +   "everything  else  we", +   "do                  " + ] +
      +
      +

      以下错误的选项是?

      +## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + vector words = {"Science", "is", "what", "we", "understand", "well", "enough", "to", "explain", + "to", "a", "computer.", "Art", "is", "everything", "else", "we", "do"}; + int maxWidth = 20; + vector res; + res = sol.fullJustify(words, maxWidth); + for (auto i : res) + cout << i << " " << endl; + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +public: + vector fullJustify(vector &words, int maxWidth) + { + vector result; + int wordsVecSize = words.size(); + for (int beginIndex = 0; beginIndex < wordsVecSize;) + { + string tempRec = words[beginIndex]; + int remainLength = maxWidth - words[beginIndex].size(); + int cnt = 1; + int endIndex = beginIndex + 1; + + while (endIndex < wordsVecSize) + { + int tempVaule = remainLength - words[endIndex].size(); + if (tempVaule >= cnt) + { + remainLength = tempVaule; + cnt += 1; + ++endIndex; + } + else + { + break; + } + } + if (cnt == 1) + { + tempRec = string(remainLength, ' '); + } + else if (endIndex != wordsVecSize) + { + vector spaceNum(cnt, remainLength / (cnt - 1)); + remainLength %= (cnt - 1); + for (int i = 1; i <= remainLength; ++i) + { + spaceNum[i] += 1; + } + + for (int index = beginIndex + 1; index < endIndex; ++index) + { + tempRec += string(spaceNum[index - beginIndex], ' ') + words[index]; + } + } + else + { + remainLength = maxWidth - words[beginIndex].size(); + for (int index = beginIndex + 1; index < endIndex; ++index) + { + tempRec += " " + words[index]; + remainLength = remainLength - 1 - words[index].size(); + } + tempRec += string(remainLength, ' '); + } + result.push_back(tempRec); + beginIndex = endIndex; + } + return result; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + vector fullJustify(vector &words, int maxWidth) + { + vector res; + int i = 0; + while (i < words.size()) + { + int residue_wid = 0; + int begin = i; + while (i < words.size() && residue_wid + words[i].size() <= maxWidth) + { + residue_wid += words[i].size() + 1; + ++i; + } + if (i == words.size()) + { + res.push_back(packagestring(words, begin, i - 1, residue_wid - 1, maxWidth, false)); + break; + } + res.push_back(packagestring(words, begin, i - 1, residue_wid - 1, maxWidth, true)); + } + return res; + } + + string packagestring(vector &words, int starter, int ender, int strlen, int maxWidth, bool tail) + { + string res = ""; + if (tail && starter < ender) + { + int cnt = ender - starter; + int blk = (maxWidth - strlen) / cnt + 1; + string spc = string(blk, ' '); + int ofst = (maxWidth - strlen) % cnt; + for (int i = starter; i < ender; ++i) + { + res += words[i] + spc; + if (i - starter < ofst) + res += ' '; + } + res += words[ender]; + } + else + { + string spc = string(maxWidth - strlen, ' '); + for (int i = starter; i < ender; ++i) + { + res += words[i] + ' '; + } + res += words[ender] + spc; + } + return res; + } +}; +``` + +### B +```cpp +class Solution +{ +public: + vector fullJustify(vector &words, int L) + { + vector res; + int i = 0; + while (i < words.size()) + { + int j = i, len = 0; + while (j < words.size() && len + words[j].size() + j - i <= L) + { + len += words[j++].size(); + } + string out; + int space = L - len; + for (int k = i; k < j; ++k) + { + out += words[k]; + if (space > 0) + { + int tmp; + if (j == words.size()) + { + if (j - k == 1) + tmp = space; + else + tmp = 1; + } + else + { + if (j - k - 1 > 0) + { + if (space % (j - k - 1) == 0) + tmp = space / (j - k - 1); + else + tmp = space / (j - k - 1) + 1; + } + else + tmp = space; + } + out.append(tmp, ' '); + space -= tmp; + } + } + res.push_back(out); + i = j; + } + return res; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + vector fullJustify(vector &words, int maxWidth) + { + int n = words.size(), length, num = 0, i, j, k; + string now; + vector ans; + for (i = 0; i < n;) + { + now.clear(); + num = 0; + length = words[i].length(); + for (j = i; j < n - 1; j++) + { + if (words[j + 1].length() + length + num + 1 > maxWidth) + break; + num++; + length += words[j + 1].length(); + } + if (j == n - 1) + { + for (k = i; k < n; k++) + { + now += words[k]; + if (k != n - 1) + now += " "; + } + for (k = 0; k < maxWidth - length - num; k++) + now += " "; + ans.push_back(now); + } + else + { + int black = maxWidth - length; + if (num == 0) + { + now.append(words[i]); + now.append(black, ' '); + ans.push_back(now); + } + else + { + int x = black % num; + int y = black / num; + if (x == 0) + { + string kongge; + for (k = 0; k < y; k++) + kongge += " "; + for (k = i; k <= j; k++) + { + now += words[k]; + if (k != j) + now += kongge; + } + ans.push_back(now); + } + else + { + string kongge; + for (k = 0; k <= y; k++) + kongge += " "; + for (k = i; k <= j; k++) + { + now += words[k]; + if (k != j) + now += kongge; + if (k - i + 1 == x) + kongge.erase(kongge.begin()); + } + ans.push_back(now); + } + } + } + i = j + 1; + } + return ans; + } +}; +``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/6.leetcode\345\223\210\345\270\214\350\241\250/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/6.leetcode\345\223\210\345\270\214\350\241\250/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/config.json" new file mode 100644 index 000000000..a6031f6e7 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/6.leetcode\345\223\210\345\270\214\350\241\250/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "最小覆盖子串" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "最小覆盖子串" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/6.leetcode\345\223\210\345\270\214\350\241\250/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/6.leetcode\345\223\210\345\270\214\350\241\250/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/desc.html" new file mode 100644 index 000000000..bfa0f67a9 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/6.leetcode\345\223\210\345\270\214\350\241\250/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/desc.html" @@ -0,0 +1 @@ +

      给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 ""

      注意:如果 s 中存在这样的子串,我们保证它是唯一的答案。

       

      示例 1:

      输入:s = "ADOBECODEBANC", t = "ABC"
      输出:
      "BANC"

      示例 2:

      输入:s = "a", t = "a"
      输出:
      "a"

       

      提示:

      • 1 <= s.length, t.length <= 105
      • st 由英文字母组成

       

      进阶:你能设计一个在 o(n) 时间内解决此问题的算法吗? \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/6.leetcode\345\223\210\345\270\214\350\241\250/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/6.leetcode\345\223\210\345\270\214\350\241\250/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/solution.cpp" new file mode 100644 index 000000000..074e28830 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/6.leetcode\345\223\210\345\270\214\350\241\250/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/solution.cpp" @@ -0,0 +1,37 @@ +#include +using namespace std; +class Solution +{ +public: + string minWindow(string s, string t) + { + vector count(128); + for (char c : t) + { + count[c]++; + } + int l = 0, r = 0; + int need_to_meet = t.length(); + int start, min_len = INT_MAX; + while (r < s.length()) + { + if (--count[s[r++]] >= 0) + { + need_to_meet--; + } + while (need_to_meet == 0) + { + if (r - l < min_len) + { + start = l; + min_len = r - l; + } + if (++count[s[l++]] > 0) + { + need_to_meet++; + } + } + } + return min_len == INT_MAX ? "" : s.substr(start, min_len); + } +}; \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/6.leetcode\345\223\210\345\270\214\350\241\250/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/6.leetcode\345\223\210\345\270\214\350\241\250/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/solution.json" new file mode 100644 index 000000000..25b51fe36 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/6.leetcode\345\223\210\345\270\214\350\241\250/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "9cf1c91e49da435aa1d63c2bd5aa7925" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/6.leetcode\345\223\210\345\270\214\350\241\250/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/6.leetcode\345\223\210\345\270\214\350\241\250/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/solution.md" new file mode 100644 index 000000000..9717e9ad5 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/6.leetcode\345\223\210\345\270\214\350\241\250/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/solution.md" @@ -0,0 +1,177 @@ +# 最小覆盖子串 +

      给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 ""

      注意:如果 s 中存在这样的子串,我们保证它是唯一的答案。

       

      示例 1:

      输入:s = "ADOBECODEBANC", t = "ABC"
      输出:
      "BANC"

      示例 2:

      输入:s = "a", t = "a"
      输出:
      "a"

       

      提示:

      • 1 <= s.length, t.length <= 105
      • st 由英文字母组成

       

      进阶:你能设计一个在 o(n) 时间内解决此问题的算法吗? +

      以下错误的选项是?

      +## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + string s = "ADOBECODEBANC"; + string t = "ABC"; + string res; + res = sol.minWindow(s, t); + cout << res; + return 0; +} + +``` + +## 答案 +```cpp +class Solution +{ +public: + string minWindow(string s, string t) + { + unordered_map hs, ht; + for (auto c : t) + ht[c]++; + + string res; + int cnt = 0; + for (int i = 0, j = 0; i < s.size(); i++) + { + hs[s[i]]++; + if (hs[s[i]] <= ht[s[i]]) + cnt++; + + while (hs[s[j]] > ht[s[j]]) + hs[s[j++]]--; + if (cnt == t.size()) + { + if (res.empty()) + res = s.substr(j, i - j); + } + } + + return res; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + string minWindow(string s, string t) + { + vector need(58, 0), window(58, 0); + for (auto c : t) + ++need[c - 'A']; + int left = 0, right = 0; + int count = 0; + int target = 0; + for (auto c : need) + { + if (c) + ++target; + } + int start = 0; + int len = INT_MAX; + while (right < s.size()) + { + auto c = s[right] - 'A'; + ++right; + if (need[c] > 0) + { + ++window[c]; + if (need[c] == window[c]) + ++count; + } + + while (count == target) + { + if (right - left < len) + { + start = left; + len = right - left; + } + auto c = s[left] - 'A'; + ++left; + if (need[c] > 0) + { + if (need[c] == window[c]) + --count; + --window[c]; + } + } + } + return len == INT_MAX ? "" : s.substr(start, len); + } +}; +``` + +### B +```cpp +class Solution +{ +public: + string minWindow(string s, string t) + { + unordered_map findChar; + int n1 = s.length(), n2 = t.length(), i, j; + for (i = 0; i < n2; i++) + findChar[t[i]]++; + int start = 0, cnt = 0, minLength = n1 + 1, now = 0; + for (i = 0; i < n1; i++) + { + if ((--findChar[s[i]]) >= 0) + cnt++; + if (cnt == n2) + { + while (++findChar[s[now]] <= 0) + now++; + cnt--; + if (i - now + 1 < minLength) + { + start = now; + minLength = i - now + 1; + } + now++; + } + } + return minLength > n1 ? "" : s.substr(start, minLength); + } +}; +``` + +### C +```cpp +class Solution +{ +public: + string minWindow(string s, string t) + { + string ans = ""; + map lettercount; + for (char c : t) + ++lettercount[c]; + int count = 0, left = 0, minlen = INT_MAX; + for (int i = 0; i < s.size(); ++i) + { + if (--lettercount[s[i]] >= 0) + ++count; + while (count == t.size()) + { + if (minlen > i - left + 1) + { + minlen = i - left + 1; + ans = s.substr(left, minlen); + } + if (++lettercount[s[left]] > 0) + --count; + ++left; + } + } + return ans; + } +}; +``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/76_\347\273\204\345\220\210/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/76_\347\273\204\345\220\210/config.json" new file mode 100644 index 000000000..e17969c10 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/76_\347\273\204\345\220\210/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "组合" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "组合" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/76_\347\273\204\345\220\210/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/76_\347\273\204\345\220\210/desc.html" new file mode 100644 index 000000000..8ded3769c --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/76_\347\273\204\345\220\210/desc.html" @@ -0,0 +1,3 @@ +

      给定两个整数 nk,返回 1 ... n 中所有可能的 k 个数的组合。

      +

      示例:

      +
      输入: n = 4, k = 2
      输出:
      [[2,4],[3,4],[2,3],[1,2],[1,3],[1,4],]
      \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/76_\347\273\204\345\220\210/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/76_\347\273\204\345\220\210/solution.cpp" new file mode 100644 index 000000000..006e5510c --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/76_\347\273\204\345\220\210/solution.cpp" @@ -0,0 +1,30 @@ +#include +using namespace std; +class Solution +{ +public: + vector> combine(int n, int k) + { + vector> res; + dfs(n, k, 1, res); + return res; + } +private: + vector stack; + void dfs(int n, int k, int start, vector> &res) + { + if (stack.size() == k) + { + res.push_back(stack); + } + else + { + for (int i = start; i <= n; i++) + { + stack.push_back(i); + dfs(n, k, i + 1, res); + stack.pop_back(); + } + } + } +}; \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/76_\347\273\204\345\220\210/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/76_\347\273\204\345\220\210/solution.json" new file mode 100644 index 000000000..9b709c70a --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/76_\347\273\204\345\220\210/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "33c4de1858844039a17bb3bdc2376e3b" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/76_\347\273\204\345\220\210/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/76_\347\273\204\345\220\210/solution.md" new file mode 100644 index 000000000..d39ebb680 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/76_\347\273\204\345\220\210/solution.md" @@ -0,0 +1,171 @@ +# 组合 +

      给定两个整数 nk,返回 1 ... n 中所有可能的 k 个数的组合。

      +

      示例:

      +
      输入: n = 4, k = 2
      输出:
      [[2,4],[3,4],[2,3],[1,2],[1,3],[1,4],]
      +

      以下错误的选项是?

      +## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + int n = 4; + int k = 2; + vector> res; + res = sol.combine(n, k); + for (auto i : res) + { + for (auto j : i) + cout << j << " "; + cout << endl; + } + return 0; +} + +``` + +## 答案 +```cpp +class Solution +{ +public: + vector> combine(int n, int k) + { + vector ones(n), lib(n); + vector> result; + for (int i = 0; i < n; i++) + { + ones[i] = (i < k) ? 1 : 0; + lib[i] = i + 1; + } + while (1) + { + vector solution; + bool flag = false; + for (int i = 0; i < n; i++) + { + if (ones[i]) + solution.push_back(lib[i]); + } + result.push_back(solution); + int count = 0; + for (int i = 0; i < n - 1; i++) + { + if (ones[i] == 1 && ones[i + 1] == 0) + { + flag = true; + ones[i] = 0; + ones[i + 1] = 1; + for (int j = 0; j < i; j++) + { + ones[j] = count > 0 ? 1 : 0; + } + break; + } + else if (ones[i]) + count++; + } + if (!flag) + break; + } + + return result; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + vector> combine(int n, int k) + { + vector> ans; + vector tmp; + combineDFS(n, k, 1, tmp, ans); + return ans; + } + void combineDFS(int n, int k, int level, vector &tmp, vector> &ans) + { + if (tmp.size() == k) + { + ans.push_back(tmp); + return; + } + for (int i = level; i <= n; ++i) + { + tmp.push_back(i); + combineDFS(n, k, i + 1, tmp, ans); + tmp.pop_back(); + } + } +}; +``` + +### B +```cpp +class Solution +{ +public: + vector> combine(int n, int k) + { + vector> result; + int i = 0; + vector p(k, 0); + while (i >= 0) + { + p[i]++; + if (p[i] > n) + --i; + else if (i == k - 1) + result.push_back(p); + else + { + ++i; + p[i] = p[i - 1]; + } + } + return result; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + vector> res; + vector tmp; + int n; + + void backtrack(vector &tmp, int idx, int cnt, int k) + { + if (cnt == k) + { + res.push_back(tmp); + return; + } + for (int i = idx + 1; i <= n; ++i) + { + tmp[cnt] = i; + backtrack(tmp, i, cnt + 1, k); + tmp[cnt] = 0; + } + } + vector> combine(int n, int k) + { + this->n = n; + tmp = vector(k, 0); + backtrack(tmp, 0, 0, k); + return res; + } +}; +``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/78_\345\215\225\350\257\215\346\220\234\347\264\242/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/78_\345\215\225\350\257\215\346\220\234\347\264\242/config.json" new file mode 100644 index 000000000..c1a24b665 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/78_\345\215\225\350\257\215\346\220\234\347\264\242/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "单词搜索" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "单词搜索" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/78_\345\215\225\350\257\215\346\220\234\347\264\242/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/78_\345\215\225\350\257\215\346\220\234\347\264\242/desc.html" new file mode 100644 index 000000000..d0da23d85 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/78_\345\215\225\350\257\215\346\220\234\347\264\242/desc.html" @@ -0,0 +1 @@ +

      给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false

      单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。

       

      示例 1:

      输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
      输出:
      true

      示例 2:

      输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
      输出:
      true

      示例 3:

      输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
      输出:
      false

       

      提示:

      • m == board.length
      • n = board[i].length
      • 1 <= m, n <= 6
      • 1 <= word.length <= 15
      • boardword 仅由大小写英文字母组成

       

      进阶:你可以使用搜索剪枝的技术来优化解决方案,使其在 board 更大的情况下可以更快解决问题?

      \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/78_\345\215\225\350\257\215\346\220\234\347\264\242/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/78_\345\215\225\350\257\215\346\220\234\347\264\242/solution.cpp" new file mode 100644 index 000000000..19ab6b777 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/78_\345\215\225\350\257\215\346\220\234\347\264\242/solution.cpp" @@ -0,0 +1,68 @@ +#include +#include +#include +#include +static bool dfs(char *word, char **board, bool *used, + int row, int col, int row_size, int col_size) +{ + if (board[row][col] != *word) + { + return false; + } + used[row * col_size + col] = true; + if (*(word + 1) == '\0') + { + return true; + } + bool result = false; + if (row > 0 && !used[(row - 1) * col_size + col]) + { + result = dfs(word + 1, board, used, row - 1, col, row_size, col_size); + } + if (!result && row < row_size - 1 && !used[(row + 1) * col_size + col]) + { + result = dfs(word + 1, board, used, row + 1, col, row_size, col_size); + } + if (!result && col > 0 && !used[row * col_size + col - 1]) + { + result = dfs(word + 1, board, used, row, col - 1, row_size, col_size); + } + if (!result && col < col_size - 1 && !used[row * col_size + col + 1]) + { + result = dfs(word + 1, board, used, row, col + 1, row_size, col_size); + } + used[row * col_size + col] = false; + return result; +} +static bool exist(char **board, int boardRowSize, int boardColSize, char *word) +{ + int i, j; + int len = strlen(word); + if (len > boardRowSize * boardColSize) + { + return false; + } + bool *used = malloc(boardRowSize * boardColSize); + for (i = 0; i < boardRowSize; i++) + { + for (j = 0; j < boardColSize; j++) + { + memset(used, false, boardRowSize * boardColSize); + if (dfs(word, board, used, i, j, boardRowSize, boardColSize)) + { + return true; + } + } + } + return false; +} +int main(int argc, char **argv) +{ + if (argc < 3) + { + fprintf(stderr, "Usage: ./test word row1 row2...\n"); + exit(-1); + } + printf("%s\n", exist(argv + 2, argc - 2, strlen(argv[2]), argv[1]) ? "true" : "false"); + return 0; +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/78_\345\215\225\350\257\215\346\220\234\347\264\242/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/78_\345\215\225\350\257\215\346\220\234\347\264\242/solution.json" new file mode 100644 index 000000000..bb03f3774 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/78_\345\215\225\350\257\215\346\220\234\347\264\242/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "3e5323d15c2d438d84c4c55bc1247122" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/78_\345\215\225\350\257\215\346\220\234\347\264\242/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/78_\345\215\225\350\257\215\346\220\234\347\264\242/solution.md" new file mode 100644 index 000000000..ef634f2f7 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/7.leetcode\345\233\276\344\270\216\346\220\234\347\264\242/78_\345\215\225\350\257\215\346\220\234\347\264\242/solution.md" @@ -0,0 +1,203 @@ +# 单词搜索 +

      给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false

      单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。

       

      示例 1:

      输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
      输出:
      true

      示例 2:

      输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
      输出:
      true

      示例 3:

      输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
      输出:
      false

       

      提示:

      • m == board.length
      • n = board[i].length
      • 1 <= m, n <= 6
      • 1 <= word.length <= 15
      • boardword 仅由大小写英文字母组成

       

      进阶:你可以使用搜索剪枝的技术来优化解决方案,使其在 board 更大的情况下可以更快解决问题?

      +

      以下错误的选项是?

      +## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + int a = 3, b = 4; + vector> board = {{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}}; + string word = "ABCCED"; + + bool res; + res = sol.exist(board, word); + cout << res; + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +public: + vector> dirs{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; + bool exist(vector> &board, string word) + { + int n = board.size(), m = board[0].size(); + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < m; ++j) + { + if (dfs(board, word, 0, i, j)) + return true; + } + } + return false; + } + bool dfs(vector> &board, string &word, int idx, int x, int y) + { + if (board[x][y] != word[idx]) + return false; + if (idx == word.size() - 1) + return true; + + char tmp = board[x][y]; + board[x][y] = 0; + for (const auto &dir : dirs) + { + if (x + dir[0] < 0 || x + dir[0] >= board.size() || y + dir[1] < 0 || y + dir[1] >= board[0].size()) + continue; + if (dfs(board, word, idx, x + dir[0], y + dir[1])) + return true; + } + board[x][y] = tmp; + return false; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + bool search(vector> &board, string word, int i, int j, int pos) + { + if (pos == word.size()) + return true; + if (i < 0 || j < 0 || i >= board.size() || j >= board[i].size()) + return false; + if (word[pos] != board[i][j]) + return false; + char temp = board[i][j]; + board[i][j] = '.'; + bool result = search(board, word, i + 1, j, pos + 1) || search(board, word, i - 1, j, pos + 1) || search(board, word, i, j - 1, pos + 1) || search(board, word, i, j + 1, pos + 1); + board[i][j] = temp; + return result; + } + + bool exist(vector> &board, string word) + { + for (int i = 0; i < board.size(); i++) + for (int j = 0; j < board[i].size(); j++) + if (search(board, word, i, j, 0)) + return true; + return false; + } +}; +``` + +### B +```cpp +class Solution +{ +public: + bool exist(vector> &board, string word) + { + rows = board.size(), columns = board[0].size(); + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < columns; j++) + { + if (backtrack(board, word, i, j, 0)) + return true; + } + } + return false; + } + +private: + int rows, columns; + bool backtrack(vector> &board, string &word, int x, int y, int index) + { + + if (x >= rows || x < 0 || y >= columns || y < 0 || board[x][y] != word[index]) + { + return false; + } + + if (index == word.size() - 1) + { + return true; + } + + board[x][y] = ' '; + + if (backtrack(board, word, x - 1, y, index + 1) || backtrack(board, word, x + 1, y, index + 1) || backtrack(board, word, x, y - 1, index + 1) || backtrack(board, word, x, y + 1, index + 1)) + { + return true; + } + + board[x][y] = word[index]; + return false; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + bool DFS(vector> &board, string word, int row, int col, int index, vector> &flag) + { + if (index == word.length()) + { + return true; + } + if (row < 0 || row >= board.size() || col < 0 || col >= board[0].size() || board[row][col] != word[index]) + { + return false; + } + if (0 == flag[row][col]) + { + flag[row][col] = 1; + if (DFS(board, word, row - 1, col, index + 1, flag)) + { + return true; + } + if (DFS(board, word, row + 1, col, index + 1, flag)) + { + return true; + } + if (DFS(board, word, row, col - 1, index + 1, flag)) + { + return true; + } + if (DFS(board, word, row, col + 1, index + 1, flag)) + { + return true; + } + flag[row][col] = 0; + } + return false; + } + bool exist(vector> &board, string word) + { + int m = board.size(); + int n = board[0].size(); + int k = word.length(); + vector> flag(m, vector(n, 0)); + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + if (DFS(board, word, i, j, 0, flag)) + { + return true; + } + } + } + return false; + } +}; +``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/65_\345\212\240\344\270\200/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/65_\345\212\240\344\270\200/config.json" new file mode 100644 index 000000000..658a92a8a --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/65_\345\212\240\344\270\200/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "加一" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "加一" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/65_\345\212\240\344\270\200/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/65_\345\212\240\344\270\200/desc.html" new file mode 100644 index 000000000..10063d744 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/65_\345\212\240\344\270\200/desc.html" @@ -0,0 +1 @@ +

      给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一。

      最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。

      你可以假设除了整数 0 之外,这个整数不会以零开头。

       

      示例 1:

      输入:digits = [1,2,3]
      输出:
      [1,2,4]
      解释:
      输入数组表示数字 123。

      示例 2:

      输入:digits = [4,3,2,1]
      输出:
      [4,3,2,2]
      解释:
      输入数组表示数字 4321。

      示例 3:

      输入:digits = [0]
      输出:
      [1]

       

      提示:

      • 1 <= digits.length <= 100
      • 0 <= digits[i] <= 9
      \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/65_\345\212\240\344\270\200/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/65_\345\212\240\344\270\200/solution.cpp" new file mode 100644 index 000000000..36f8e0ee8 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/65_\345\212\240\344\270\200/solution.cpp" @@ -0,0 +1,23 @@ +#include +using namespace std; +class Solution +{ +public: + vector plusOne(vector &digits) + { + int carry = 1; + vector res; + for (int i = digits.size() - 1; i >= 0; i--) + { + int d = digits[i] + carry; + res.push_back(d % 10); + carry = d / 10; + } + if (carry > 0) + { + res.push_back(carry); + } + reverse(res.begin(), res.end()); + return res; + } +}; \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/65_\345\212\240\344\270\200/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/65_\345\212\240\344\270\200/solution.json" new file mode 100644 index 000000000..780e32477 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/65_\345\212\240\344\270\200/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "abe202eefe824825a23b5de4a2c6c737" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/65_\345\212\240\344\270\200/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/65_\345\212\240\344\270\200/solution.md" new file mode 100644 index 000000000..501e1f677 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/65_\345\212\240\344\270\200/solution.md" @@ -0,0 +1,140 @@ +# 加一 +

      给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一。

      最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。

      你可以假设除了整数 0 之外,这个整数不会以零开头。

       

      示例 1:

      输入:digits = [1,2,3]
      输出:
      [1,2,4]
      解释:
      输入数组表示数字 123。

      示例 2:

      输入:digits = [4,3,2,1]
      输出:
      [4,3,2,2]
      解释:
      输入数组表示数字 4321。

      示例 3:

      输入:digits = [0]
      输出:
      [1]

       

      提示:

      • 1 <= digits.length <= 100
      • 0 <= digits[i] <= 9
      +

      以下错误的选项是?

      +## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + vector digits = {4, 3, 2, 1}; + vector res; + res = sol.plusOne(digits); + for (auto i : res) + cout << i << " "; + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +public: + vector plusOne(vector &digits) + { + int carrybit = 0, n = digits.size(); + vector ans, t(n, 0); + for (int i = n - 1; i >= 0; --i) + { + t[i] = (digits[i] + carrybit) % 10; + carrybit = (digits[i] + carrybit) / 10; + } + if (carrybit) + { + ans.push_back(1); + } + for (int i = 0; i < n; ++i) + ans.push_back(t[i]); + return ans; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + vector plusOne(vector &digits) + { + int len = digits.size() - 1; + for (; len > 0 && digits[len] == 9; --len) + { + digtis[len] = 0; + } + if (len == 0 && digits[0] == 9) + { + digits[0] = 0; + digits.insert(digits.begin(), 1); + } + else + { + ++digits[len]; + } + return digits; + } +}; +``` + +### B +```cpp +class Solution +{ +public: + vector plusOne(vector &digits) + { + int i = 0; + int size = digits.size(); + for (i = size - 1; i >= 0; i--) + { + digits[i]++; + digits[i] = digits[i] % 10; + if (digits[i] != 0) + return digits; + } + if (i == -1) + { + digits.insert(digits.begin(), 1); + + digits[size] = 0; + } + return digits; + } +}; +``` + +### C +```cpp + +class Solution +{ +public: + vector plusOne(vector &digits) + { + int len = digits.size() - 1; + for (int i = len; i >= 0; i--) + { + + if ((digits[i] + 1 == 10 && i == len) || digits[i] >= 10) + { + digits[i] = 0; + if (i == 0) + { + digits.insert(digits.begin(), 1); + } + else + { + digits[i - 1] += 1; + } + } + + else + { + if (i == len) + { + digits[i] += 1; + } + break; + } + } + return digits; + } +}; +``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/config.json" new file mode 100644 index 000000000..4e7a40bab --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "二进制求和" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "二进制求和" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/desc.html" new file mode 100644 index 000000000..3b2f1b0ac --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/desc.html" @@ -0,0 +1 @@ +

      给你两个二进制字符串,返回它们的和(用二进制表示)。

      输入为 非空 字符串且只包含数字 1 和 0

       

      示例 1:

      输入: a = "11", b = "1"
      输出:
      "100"

      示例 2:

      输入: a = "1010", b = "1011"
      输出:
      "10101"

       

      提示:

      • 每个字符串仅由字符 '0''1' 组成。
      • 1 <= a.length, b.length <= 10^4
      • 字符串如果不是 "0" ,就都不含前导零。
      \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/solution.cpp" new file mode 100644 index 000000000..0df722720 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/solution.cpp" @@ -0,0 +1,101 @@ +#include +using namespace std; +class Solution +{ +public: + string addBinary(string a, string b) + { + string res; + int carry = 0; + int i = a.length() - 1; + int j = b.length() - 1; + for (; i >= 0 && j >= 0; i--, j--) + { + if (a[i] == '1' && b[j] == '1') + { + if (carry > 0) + { + res.push_back('1'); + } + else + { + res.push_back('0'); + } + carry = 1; + } + else if (a[i] == '0' && b[j] == '0') + { + if (carry > 0) + { + res.push_back('1'); + } + else + { + res.push_back('0'); + } + carry = 0; + } + else + { + if (carry > 0) + { + res.push_back('0'); + carry = 1; + } + else + { + res.push_back('1'); + carry = 0; + } + } + } + while (i >= 0) + { + if (a[i--] == '1') + { + if (carry > 0) + { + res.push_back('0'); + carry = 1; + } + else + { + res.push_back('1'); + carry = 0; + } + } + else + { + res.push_back(carry + '0'); + carry = 0; + } + } + while (j >= 0) + { + if (b[j--] == '1') + { + if (carry > 0) + { + res.push_back('0'); + carry = 1; + } + else + { + res.push_back('1'); + carry = 0; + } + } + else + { + res.push_back(carry + '0'); + carry = 0; + } + } + if (carry > 0) + { + res.push_back('1'); + } + reverse(res.begin(), res.end()); + return res; + } +}; \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/solution.json" new file mode 100644 index 000000000..e4304ece4 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "5be46f3f939d4ce19797f395bd4be062" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/solution.md" new file mode 100644 index 000000000..acd764259 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/solution.md" @@ -0,0 +1,181 @@ +# 二进制求和 +

      给你两个二进制字符串,返回它们的和(用二进制表示)。

      输入为 非空 字符串且只包含数字 1 和 0

       

      示例 1:

      输入: a = "11", b = "1"
      输出:
      "100"

      示例 2:

      输入: a = "1010", b = "1011"
      输出:
      "10101"

       

      提示:

      • 每个字符串仅由字符 '0''1' 组成。
      • 1 <= a.length, b.length <= 10^4
      • 字符串如果不是 "0" ,就都不含前导零。
      +

      以下错误的选项是?

      +## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + string a = "1010"; + string b = "1011"; + string res; + res = sol.addBinary(a, b); + cout << res; + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +public: + string addBinary(string a, string b) + { + string res; + int carry = 0; + int m = a.size() - 1, n = b.size() - 1; + while (m >= 0 || n >= 0) + { + int q = (m >= 0) ? a[m] - '0' : 0; + int p = (n >= 0) ? b[n] - '0' : 0; + int k = q + p + carry; + res = to_string(k % 2) + res; + carry = k / 2; + } + if (1 == carry) + res = '1' + res; + return res; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + string addBinary(string a, string b) + { + long aa = 0, bb = 0; + long sum = 0; + string res; + int p = 0; + stringstream outa(a); + stringstream outb(b); + outa >> aa; + outb >> bb; + sum = aa + bb; + bool flag = false; + while (sum != 0) + { + p = sum % 10; + if (flag == true) + p++; + flag = false; + sum /= 10; + if (p == 0) + res = res + "0"; + else if (p % 2 == 0) + { + flag = true; + res = res + "0"; + } + else + res = res + "1"; + } + if (res[res.size() - 1] == '0') + res = res + "1"; + reverse(res.begin(), res.end()); + return res; + } +}; +``` + +### B +```cpp +class Solution +{ +public: + string addBinary(string a, string b) + { + if (b.size() > a.size()) + { + string temp = b; + b = a; + a = temp; + } + int i = a.size() - 1; + int j = b.size() - 1; + if (i != j) + { + for (int k = 0; k < i - j; k++) + b = "0" + b; + } + int count = 0; + for (int k = i; k >= 0; k--) + { + if (a[k] - '0' + b[k] - '0' + count == 0) + { + a[k] = '0'; + count = 0; + } + else if (a[k] - '0' + b[k] - '0' + count == 1) + { + a[k] = '1'; + count = 0; + } + else if (a[k] - '0' + b[k] - '0' + count == 3) + { + a[k] = '1'; + count = 1; + } + else + { + a[k] = '0'; + count = 1; + } + } + if (count == 1) + a = '1' + a; + return a; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + string addBinary(string a, string b) + { + string result = "", rr = ""; + char aa, bb; + int l1 = a.length(), l2 = b.length(), i = l1 - 1, j = l2 - 1, carry = 0, sum = 0; + while (true) + { + if (i < 0) + aa = '0'; + else + aa = a[i]; + if (j < 0) + bb = '0'; + else + bb = b[j]; + sum = (aa - '0') + (bb - '0') + carry; + result += ((sum % 2) + '0'); + carry = sum / 2; + j--; + i--; + if (i < 0 && j < 0) + { + if (carry == 1) + result += "1"; + break; + } + } + int l3 = result.length(); + for (int i = l3 - 1; i >= 0; i--) + rr += result[i]; + return rr; + } +}; +``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/68_x \347\232\204\345\271\263\346\226\271\346\240\271/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/68_x \347\232\204\345\271\263\346\226\271\346\240\271/config.json" new file mode 100644 index 000000000..a71398bd7 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/68_x \347\232\204\345\271\263\346\226\271\346\240\271/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "x 的平方根" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "x 的平方根" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/68_x \347\232\204\345\271\263\346\226\271\346\240\271/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/68_x \347\232\204\345\271\263\346\226\271\346\240\271/desc.html" new file mode 100644 index 000000000..97b915068 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/68_x \347\232\204\345\271\263\346\226\271\346\240\271/desc.html" @@ -0,0 +1,7 @@ +

      实现 int sqrt(int x) 函数。

      +

      计算并返回 x 的平方根,其中 x 是非负整数。

      +

      由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。

      +

      示例 1:

      +
      输入: 4
      输出:
      2
      +

      示例 2:

      +
      输入: 8
      输出:
      2
      说明:
      8 的平方根是 2.82842..., 由于返回类型是整数,小数部分将被舍去。
      \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/68_x \347\232\204\345\271\263\346\226\271\346\240\271/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/68_x \347\232\204\345\271\263\346\226\271\346\240\271/solution.cpp" new file mode 100644 index 000000000..4f139bfa5 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/68_x \347\232\204\345\271\263\346\226\271\346\240\271/solution.cpp" @@ -0,0 +1,35 @@ +#include +using namespace std; +class Solution +{ +public: + int mySqrt(int x) + { + if (x == 0) + { + return 0; + } + unsigned int lo = 1, hi = x; + unsigned int mid = (lo + hi) / 2; + for (;;) + { + if (mid > x / mid) + { + hi = mid; + } + else + { + if (mid + 1 > x / (mid + 1)) + { + break; + } + else + { + lo = mid; + } + } + mid = (lo + hi) / 2; + } + return mid; + } +}; \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/68_x \347\232\204\345\271\263\346\226\271\346\240\271/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/68_x \347\232\204\345\271\263\346\226\271\346\240\271/solution.json" new file mode 100644 index 000000000..049149e61 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/68_x \347\232\204\345\271\263\346\226\271\346\240\271/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "5a369bedac094e1795be872bf50f11a8" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/68_x \347\232\204\345\271\263\346\226\271\346\240\271/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/68_x \347\232\204\345\271\263\346\226\271\346\240\271/solution.md" new file mode 100644 index 000000000..48b31ffef --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/8.leetcode\346\225\260\345\255\246/68_x \347\232\204\345\271\263\346\226\271\346\240\271/solution.md" @@ -0,0 +1,110 @@ +# x 的平方根 +

      实现 int sqrt(int x) 函数。

      +

      计算并返回 x 的平方根,其中 x 是非负整数。

      +

      由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。

      +

      示例 1:

      +
      输入: 4
      输出:
      2
      +

      示例 2:

      +
      输入: 8
      输出:
      2
      说明:
      8 的平方根是 2.82842..., 由于返回类型是整数,小数部分将被舍去。
      +

      以下错误的选项是?

      +## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + + int x = 8; + int res; + res = sol.mySqrt(x); + cout << res; + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +public: + int mySqrt(int x) + { + long long i = 0; + long long res; + long long nextRes; + for (i = 1; i <= x / 2 + 1; i++) + { + res = i * i; + nextRes = (i + 1) * (i + 1); + if (res < x && nextRes > x) + break; + } + return i; + } +}; +``` +## 选项 + +### A +```cpp +class Solution +{ +public: + int mySqrt(int x) + { + if (x == 0) + return 0; + double last = 0; + double res = 1; + while (res != last) + { + last = res; + res = (res + x / res) / 2; + } + return int(res); + } +}; +``` + +### B +```cpp +class Solution +{ +public: + int mySqrt(int x) + { + + long long i = 0; + long long j = x / 2 + 1; + while (i <= j) + { + long long mid = (i + j) / 2; + long long res = mid * mid; + if (res == x) + return mid; + else if (res < x) + i = mid + 1; + else + j = mid - 1; + } + return j; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + int mySqrt(int x) + { + return sqrt(x); + } +}; +``` diff --git "a/data_backup/1.leetcode/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/solution.md" "b/data_backup/1.leetcode/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/solution.md" index 5744ceffe..6ef90b51e 100644 --- "a/data_backup/1.leetcode/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/solution.md" +++ "b/data_backup/1.leetcode/63_\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/solution.md" @@ -4,30 +4,151 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + int a = 3, b = 3; + vector> grid = vector>(a, vector(b)) = {{1, 3, 1}, {1, 5, 1}, {4, 2, 1}}; + int res; + res = sol.minPathSum(grid); + cout << res; + return 0; +} ``` ## 答案 ```cpp +class Solution +{ +private: + int m, n; + int memo[100][100]; + +public: + int minPathSum(vector> &grid) + { + m = grid.size(), n = grid[0].size(); + + for (int i = 0; i < m; i++) + { + memset(memo[i], -1, sizeof(int) * n); + } + return dfs(grid, 0, 0); + } + + int dfs(vector> &grid, int r, int c) + { + + if (r < 0 || r >= m || c < 0 || c >= n) + return 1000000; + if (memo[r][c] != -1) + return memo[r][c]; + + if (r == m - 1 && c == n - 1) + { + memo[r][c] = grid[m][n - 1]; + return memo[r][c]; + } + + int right = dfs(grid, r, c + 1); + int down = dfs(grid, r + 1, c); + memo[r][c] = min(right, down) + grid[r][c]; + return memo[r][c]; + } +}; ``` ## 选项 ### A ```cpp +class Solution +{ +public: + int minPathSum(vector> &grid) + { + int row = grid.size(); + int col = grid[0].size(); + vector f(col, 0); + for (int i = 0; i < row; ++i) + { + f[0] = f[0] + grid[i][0]; + for (int j = 1; j < col; ++j) + { + if (i == 0) + f[j] = f[j - 1] + grid[i][j]; + else + f[j] = min(f[j - 1], f[j]) + grid[i][j]; + } + } + return f[col - 1]; + } +}; ``` ### B ```cpp - +class Solution +{ +public: + int minPathSum(vector> &grid) + { + int row = grid.size(); + int column = grid[0].size(); + for (int i = 1; i < column; ++i) + { + grid[0][i] = grid[0][i - 1] + grid[0][i]; + } + for (int i = 1; i < row; ++i) + { + grid[i][0] = grid[i - 1][0] + grid[i][0]; + } + for (int i = 1; i < row; ++i) + { + for (int j = 1; j < column; ++j) + { + int temp = grid[i - 1][j] > grid[i][j - 1] ? grid[i][j - 1] : grid[i - 1][j]; + grid[i][j] = grid[i][j] + temp; + } + } + return grid[row - 1][column - 1]; + } +}; ``` ### C ```cpp +class Solution +{ +public: + int minPathSum(vector> &grid) + { + if (grid.size() == 0) + return 0; + int m = grid.size(); + int n = grid[0].size(); + vector> m_memo = vector>(m + 1, vector(n + 1, 0)); + + for (int i = n - 1; i >= 0; --i) + m_memo[m - 1][i] = grid[m - 1][i] + m_memo[m - 1][i + 1]; + for (int j = m - 1; j >= 0; --j) + m_memo[j][n - 1] = grid[j][n - 1] + m_memo[j + 1][n - 1]; + for (int i = m - 2; i >= 0; --i) + { + for (int j = n - 2; j >= 0; --j) + { + m_memo[i][j] = grid[i][j] + min(m_memo[i][j + 1], m_memo[i + 1][j]); + } + } + return m_memo[0][0]; + } +}; ``` diff --git "a/data_backup/1.leetcode/64_\346\234\211\346\225\210\346\225\260\345\255\227/solution.md" "b/data_backup/1.leetcode/64_\346\234\211\346\225\210\346\225\260\345\255\227/solution.md" index a2897f46d..af93a713c 100644 --- "a/data_backup/1.leetcode/64_\346\234\211\346\225\210\346\225\260\345\255\227/solution.md" +++ "b/data_backup/1.leetcode/64_\346\234\211\346\225\210\346\225\260\345\255\227/solution.md" @@ -4,30 +4,268 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp - +int main() +{ + Solution sol; + string s = ".1"; + bool res; + res = sol.isNumber(s); + cout << res; + return 0; +} ``` ## 答案 ```cpp +class Solution +{ +public: + bool isNumber(string s) + { + int len = s.size(); + int left = 0, right = len - 1; + bool eExisted = false; + bool dotExisted = false; + bool digitExisited = false; + + while (s[left] == ' ') + ++left; + while (s[right] == ' ') + --right; + + if (left >= right && (s[left] < '0' || s[left] > '9')) + return false; + if (s[left] == '.') + dotExisted = true; + else if (s[left] >= '0' && s[left] <= '9') + digitExisited = true; + else if (s[left] != '+' && s[left] != '-') + return false; + + for (int i = left + 1; i <= right - 1; ++i) + { + if (s[i] >= '0' && s[i] <= '9') + digitExisited = true; + else if (s[i] == 'e' || s[i] == 'E') + { + if (!eExisted && s[i - 1] != '+' && s[i - 1] != '-' && digitExisited) + eExisted = true; + else + return false; + } + else if (s[i] == '+' || s[i] == '-') + { + if (s[i - 1] != 'e' && s[i - 1] != 'E') + return false; + } + else if (s[i] == '.') + { + if (!dotExisted && !eExisted) + dotExisted = true; + else + return false; + } + else + return false; + } + + if (s[right] >= '0' && s[right] <= '9') + return true; + else + return false; + } +}; ``` ## 选项 ### A ```cpp +class Solution +{ +public: + bool isInt(string &s, int &index) + { + if (index < s.size() && (s[index] == '-' || s[index] == '+')) + index++; + return isUnsigned(s, index); + } + + bool isUnsigned(string &s, int &index) + { + int pre = index; + while (index < s.size()) + { + if (s[index] >= '0' && s[index] <= '9') + index++; + else + break; + } + return index > pre; + } + bool isNumber(string s) + { + if (s.empty()) + { + return false; + } + + int index = 0; + while (index < s.size() && s[index] == ' ') + { + index++; + } + + bool ans = isInt(s, index); + if (index < s.size() && s[index] == '.') + { + index++; + ans = isUnsigned(s, index) || ans; + } + + if (index < s.size() && s[index] == 'e') + { + index++; + ans = isInt(s, index) && ans; + } + + while (index < s.size() && s[index] == ' ') + { + index++; + } + + return ans && index == s.size(); + } +}; ``` ### B ```cpp +class Solution +{ +public: + bool isNumber(string s) + { + int tail = s.size() - 1; + int head = 0; + while (tail >= 0) + { + if (s[tail] == ' ') + --tail; + else + break; + } + while (head <= tail) + { + if (s[head] == ' ') + ++head; + else + break; + } + if (head > tail) + return false; + s = s.substr(head, tail - head + 1); + int index = s.find('e'); + if (index == string::npos) + return judgea(s); + else + return judgea(s.substr(0, index)) && judgeb(s.substr(index + 1)); + } + bool judgea(string s) + { + bool have_num = false; + bool have_pointed = false; + for (int i = 0; i < s.size(); i++) + { + if (s[i] >= '0' && s[i] <= '9') + have_num = true; + else if (s[i] == '+' || s[i] == '-') + { + if (i != 0) + return false; + } + else if (s[i] == '.') + { + if (have_pointed) + return false; + have_pointed = true; + } + else + return false; + } + return have_num; + } + bool judgeb(string s) + { + bool have_num = false; + for (int i = 0; i < s.size(); i++) + { + if (s[i] >= '0' && s[i] <= '9') + have_num = true; + else if (s[i] == '-' || s[i] == '+') + { + if (i != 0) + return false; + } + else + return false; + } + return have_num; + } +}; ``` ### C ```cpp - +class Solution +{ +public: + bool isNumber(string s) + { + bool num = false, numAfterE = true, dot = false, sign = false, exp = false; + int n = s.size(); + for (int i = 0; i < n; ++i) + { + if (s[i] == ' ') + { + if (i < n - 1 && s[i + 1] != ' ' && (num || dot || sign || exp)) + return false; + } + else if (s[i] == '+' || s[i] == '-') + { + if (i > 0 && s[i - 1] != 'e' && s[i - 1] != ' ') + return false; + sign = true; + } + else if (s[i] >= '0' && s[i] <= '9') + { + num = true; + numAfterE = true; + } + else if (s[i] == '.') + { + if (dot || exp) + return false; + dot = true; + } + else if (s[i] == 'e') + { + if (exp || !num) + return false; + exp = true; + numAfterE = false; + } + else + return false; + } + return num && numAfterE; + } +}; ``` diff --git "a/data_backup/1.leetcode/65_\345\212\240\344\270\200/solution.md" "b/data_backup/1.leetcode/65_\345\212\240\344\270\200/solution.md" index e9c0ad49e..501e1f677 100644 --- "a/data_backup/1.leetcode/65_\345\212\240\344\270\200/solution.md" +++ "b/data_backup/1.leetcode/65_\345\212\240\344\270\200/solution.md" @@ -4,30 +4,137 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp - +int main() +{ + Solution sol; + vector digits = {4, 3, 2, 1}; + vector res; + res = sol.plusOne(digits); + for (auto i : res) + cout << i << " "; + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ +public: + vector plusOne(vector &digits) + { + int carrybit = 0, n = digits.size(); + vector ans, t(n, 0); + for (int i = n - 1; i >= 0; --i) + { + t[i] = (digits[i] + carrybit) % 10; + carrybit = (digits[i] + carrybit) / 10; + } + if (carrybit) + { + ans.push_back(1); + } + for (int i = 0; i < n; ++i) + ans.push_back(t[i]); + return ans; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + vector plusOne(vector &digits) + { + int len = digits.size() - 1; + for (; len > 0 && digits[len] == 9; --len) + { + digtis[len] = 0; + } + if (len == 0 && digits[0] == 9) + { + digits[0] = 0; + digits.insert(digits.begin(), 1); + } + else + { + ++digits[len]; + } + return digits; + } +}; ``` ### B ```cpp +class Solution +{ +public: + vector plusOne(vector &digits) + { + int i = 0; + int size = digits.size(); + for (i = size - 1; i >= 0; i--) + { + digits[i]++; + digits[i] = digits[i] % 10; + if (digits[i] != 0) + return digits; + } + if (i == -1) + { + digits.insert(digits.begin(), 1); + digits[size] = 0; + } + return digits; + } +}; ``` ### C ```cpp +class Solution +{ +public: + vector plusOne(vector &digits) + { + int len = digits.size() - 1; + for (int i = len; i >= 0; i--) + { + + if ((digits[i] + 1 == 10 && i == len) || digits[i] >= 10) + { + digits[i] = 0; + if (i == 0) + { + digits.insert(digits.begin(), 1); + } + else + { + digits[i - 1] += 1; + } + } + + else + { + if (i == len) + { + digits[i] += 1; + } + break; + } + } + return digits; + } +}; ``` diff --git "a/data_backup/1.leetcode/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/solution.md" "b/data_backup/1.leetcode/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/solution.md" index 695c58bda..acd764259 100644 --- "a/data_backup/1.leetcode/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/solution.md" +++ "b/data_backup/1.leetcode/66_\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/solution.md" @@ -4,30 +4,178 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp - +int main() +{ + Solution sol; + string a = "1010"; + string b = "1011"; + string res; + res = sol.addBinary(a, b); + cout << res; + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ +public: + string addBinary(string a, string b) + { + string res; + int carry = 0; + int m = a.size() - 1, n = b.size() - 1; + while (m >= 0 || n >= 0) + { + int q = (m >= 0) ? a[m] - '0' : 0; + int p = (n >= 0) ? b[n] - '0' : 0; + int k = q + p + carry; + res = to_string(k % 2) + res; + carry = k / 2; + } + if (1 == carry) + res = '1' + res; + return res; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + string addBinary(string a, string b) + { + long aa = 0, bb = 0; + long sum = 0; + string res; + int p = 0; + stringstream outa(a); + stringstream outb(b); + outa >> aa; + outb >> bb; + sum = aa + bb; + bool flag = false; + while (sum != 0) + { + p = sum % 10; + if (flag == true) + p++; + flag = false; + sum /= 10; + if (p == 0) + res = res + "0"; + else if (p % 2 == 0) + { + flag = true; + res = res + "0"; + } + else + res = res + "1"; + } + if (res[res.size() - 1] == '0') + res = res + "1"; + reverse(res.begin(), res.end()); + return res; + } +}; ``` ### B ```cpp - +class Solution +{ +public: + string addBinary(string a, string b) + { + if (b.size() > a.size()) + { + string temp = b; + b = a; + a = temp; + } + int i = a.size() - 1; + int j = b.size() - 1; + if (i != j) + { + for (int k = 0; k < i - j; k++) + b = "0" + b; + } + int count = 0; + for (int k = i; k >= 0; k--) + { + if (a[k] - '0' + b[k] - '0' + count == 0) + { + a[k] = '0'; + count = 0; + } + else if (a[k] - '0' + b[k] - '0' + count == 1) + { + a[k] = '1'; + count = 0; + } + else if (a[k] - '0' + b[k] - '0' + count == 3) + { + a[k] = '1'; + count = 1; + } + else + { + a[k] = '0'; + count = 1; + } + } + if (count == 1) + a = '1' + a; + return a; + } +}; ``` ### C ```cpp - +class Solution +{ +public: + string addBinary(string a, string b) + { + string result = "", rr = ""; + char aa, bb; + int l1 = a.length(), l2 = b.length(), i = l1 - 1, j = l2 - 1, carry = 0, sum = 0; + while (true) + { + if (i < 0) + aa = '0'; + else + aa = a[i]; + if (j < 0) + bb = '0'; + else + bb = b[j]; + sum = (aa - '0') + (bb - '0') + carry; + result += ((sum % 2) + '0'); + carry = sum / 2; + j--; + i--; + if (i < 0 && j < 0) + { + if (carry == 1) + result += "1"; + break; + } + } + int l3 = result.length(); + for (int i = l3 - 1; i >= 0; i--) + rr += result[i]; + return rr; + } +}; ``` diff --git "a/data_backup/1.leetcode/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/solution.md" "b/data_backup/1.leetcode/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/solution.md" index 62b9ed6fd..6e0881b59 100644 --- "a/data_backup/1.leetcode/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/solution.md" +++ "b/data_backup/1.leetcode/67_\346\226\207\346\234\254\345\267\246\345\217\263\345\257\271\351\275\220/solution.md" @@ -66,30 +66,286 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp - +int main() +{ + Solution sol; + vector words = {"Science", "is", "what", "we", "understand", "well", "enough", "to", "explain", + "to", "a", "computer.", "Art", "is", "everything", "else", "we", "do"}; + int maxWidth = 20; + vector res; + res = sol.fullJustify(words, maxWidth); + for (auto i : res) + cout << i << " " << endl; + return 0; +} ``` ## 答案 ```cpp +class Solution +{ +public: + vector fullJustify(vector &words, int maxWidth) + { + vector result; + int wordsVecSize = words.size(); + for (int beginIndex = 0; beginIndex < wordsVecSize;) + { + string tempRec = words[beginIndex]; + int remainLength = maxWidth - words[beginIndex].size(); + int cnt = 1; + int endIndex = beginIndex + 1; + + while (endIndex < wordsVecSize) + { + int tempVaule = remainLength - words[endIndex].size(); + if (tempVaule >= cnt) + { + remainLength = tempVaule; + cnt += 1; + ++endIndex; + } + else + { + break; + } + } + if (cnt == 1) + { + tempRec = string(remainLength, ' '); + } + else if (endIndex != wordsVecSize) + { + vector spaceNum(cnt, remainLength / (cnt - 1)); + remainLength %= (cnt - 1); + for (int i = 1; i <= remainLength; ++i) + { + spaceNum[i] += 1; + } + for (int index = beginIndex + 1; index < endIndex; ++index) + { + tempRec += string(spaceNum[index - beginIndex], ' ') + words[index]; + } + } + else + { + remainLength = maxWidth - words[beginIndex].size(); + for (int index = beginIndex + 1; index < endIndex; ++index) + { + tempRec += " " + words[index]; + remainLength = remainLength - 1 - words[index].size(); + } + tempRec += string(remainLength, ' '); + } + result.push_back(tempRec); + beginIndex = endIndex; + } + return result; + } +}; ``` ## 选项 ### A ```cpp +class Solution +{ +public: + vector fullJustify(vector &words, int maxWidth) + { + vector res; + int i = 0; + while (i < words.size()) + { + int residue_wid = 0; + int begin = i; + while (i < words.size() && residue_wid + words[i].size() <= maxWidth) + { + residue_wid += words[i].size() + 1; + ++i; + } + if (i == words.size()) + { + res.push_back(packagestring(words, begin, i - 1, residue_wid - 1, maxWidth, false)); + break; + } + res.push_back(packagestring(words, begin, i - 1, residue_wid - 1, maxWidth, true)); + } + return res; + } + string packagestring(vector &words, int starter, int ender, int strlen, int maxWidth, bool tail) + { + string res = ""; + if (tail && starter < ender) + { + int cnt = ender - starter; + int blk = (maxWidth - strlen) / cnt + 1; + string spc = string(blk, ' '); + int ofst = (maxWidth - strlen) % cnt; + for (int i = starter; i < ender; ++i) + { + res += words[i] + spc; + if (i - starter < ofst) + res += ' '; + } + res += words[ender]; + } + else + { + string spc = string(maxWidth - strlen, ' '); + for (int i = starter; i < ender; ++i) + { + res += words[i] + ' '; + } + res += words[ender] + spc; + } + return res; + } +}; ``` ### B ```cpp - +class Solution +{ +public: + vector fullJustify(vector &words, int L) + { + vector res; + int i = 0; + while (i < words.size()) + { + int j = i, len = 0; + while (j < words.size() && len + words[j].size() + j - i <= L) + { + len += words[j++].size(); + } + string out; + int space = L - len; + for (int k = i; k < j; ++k) + { + out += words[k]; + if (space > 0) + { + int tmp; + if (j == words.size()) + { + if (j - k == 1) + tmp = space; + else + tmp = 1; + } + else + { + if (j - k - 1 > 0) + { + if (space % (j - k - 1) == 0) + tmp = space / (j - k - 1); + else + tmp = space / (j - k - 1) + 1; + } + else + tmp = space; + } + out.append(tmp, ' '); + space -= tmp; + } + } + res.push_back(out); + i = j; + } + return res; + } +}; ``` ### C ```cpp - +class Solution +{ +public: + vector fullJustify(vector &words, int maxWidth) + { + int n = words.size(), length, num = 0, i, j, k; + string now; + vector ans; + for (i = 0; i < n;) + { + now.clear(); + num = 0; + length = words[i].length(); + for (j = i; j < n - 1; j++) + { + if (words[j + 1].length() + length + num + 1 > maxWidth) + break; + num++; + length += words[j + 1].length(); + } + if (j == n - 1) + { + for (k = i; k < n; k++) + { + now += words[k]; + if (k != n - 1) + now += " "; + } + for (k = 0; k < maxWidth - length - num; k++) + now += " "; + ans.push_back(now); + } + else + { + int black = maxWidth - length; + if (num == 0) + { + now.append(words[i]); + now.append(black, ' '); + ans.push_back(now); + } + else + { + int x = black % num; + int y = black / num; + if (x == 0) + { + string kongge; + for (k = 0; k < y; k++) + kongge += " "; + for (k = i; k <= j; k++) + { + now += words[k]; + if (k != j) + now += kongge; + } + ans.push_back(now); + } + else + { + string kongge; + for (k = 0; k <= y; k++) + kongge += " "; + for (k = i; k <= j; k++) + { + now += words[k]; + if (k != j) + now += kongge; + if (k - i + 1 == x) + kongge.erase(kongge.begin()); + } + ans.push_back(now); + } + } + } + i = j + 1; + } + return ans; + } +}; ``` diff --git "a/data_backup/1.leetcode/68_x \347\232\204\345\271\263\346\226\271\346\240\271/solution.md" "b/data_backup/1.leetcode/68_x \347\232\204\345\271\263\346\226\271\346\240\271/solution.md" index 1d16cfdd5..48b31ffef 100644 --- "a/data_backup/1.leetcode/68_x \347\232\204\345\271\263\346\226\271\346\240\271/solution.md" +++ "b/data_backup/1.leetcode/68_x \347\232\204\345\271\263\346\226\271\346\240\271/solution.md" @@ -10,30 +10,101 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + int x = 8; + int res; + res = sol.mySqrt(x); + cout << res; + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ +public: + int mySqrt(int x) + { + long long i = 0; + long long res; + long long nextRes; + for (i = 1; i <= x / 2 + 1; i++) + { + res = i * i; + nextRes = (i + 1) * (i + 1); + if (res < x && nextRes > x) + break; + } + return i; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + int mySqrt(int x) + { + if (x == 0) + return 0; + double last = 0; + double res = 1; + while (res != last) + { + last = res; + res = (res + x / res) / 2; + } + return int(res); + } +}; ``` ### B ```cpp +class Solution +{ +public: + int mySqrt(int x) + { + long long i = 0; + long long j = x / 2 + 1; + while (i <= j) + { + long long mid = (i + j) / 2; + long long res = mid * mid; + if (res == x) + return mid; + else if (res < x) + i = mid + 1; + else + j = mid - 1; + } + return j; + } +}; ``` ### C ```cpp - +class Solution +{ +public: + int mySqrt(int x) + { + return sqrt(x); + } +}; ``` diff --git "a/data_backup/1.leetcode/69_\347\210\254\346\245\274\346\242\257/solution.md" "b/data_backup/1.leetcode/69_\347\210\254\346\245\274\346\242\257/solution.md" index 38b82432b..1ad125255 100644 --- "a/data_backup/1.leetcode/69_\347\210\254\346\245\274\346\242\257/solution.md" +++ "b/data_backup/1.leetcode/69_\347\210\254\346\245\274\346\242\257/solution.md" @@ -4,30 +4,99 @@ ## aop ### before ```cpp +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + int x = 3; + int res; + res = sol.climbStairs(x); + cout << res; + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ +public: + int climbStairs(int n) + { + double sqrt5 = sqrt(5); + double fibn = pow((1 + sqrt5) / 2, n) - pow((1 - sqrt5) / 2, n + 1); + return (int)(fibn / sqrt5); + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + int climbStairs(int n) + { + vector s; + s.push_back(1); + s.push_back(2); + if (n == 1) + return 1; + if (n == 2) + return 2; + for (int i = 2; i < n; i++) + { + s.push_back(s[i - 1] + s[i - 2]); + } + return s[n - 1]; + } +}; ``` ### B ```cpp - +class Solution +{ +public: + int climbStairs(int n) + { + vector res(n + 1, 0); + res[1] = 1; + res[2] = 2; + for (int i = 3; i <= n; i++) + res[i] = res[i - 1] + res[i - 2]; + return res[n]; + } +}; ``` ### C ```cpp - +class Solution +{ +public: + int climbStairs(int n) + { + if (n == 1) + { + return 1; + } + int first = 1; + int second = 2; + for (int i = 3; i <= n; i++) + { + int third = first + second; + first = second; + second = third; + } + return second; + } +}; ``` diff --git "a/data_backup/1.leetcode/70_\347\256\200\345\214\226\350\267\257\345\276\204/solution.md" "b/data_backup/1.leetcode/70_\347\256\200\345\214\226\350\267\257\345\276\204/solution.md" index a8b068f18..d711a0159 100644 --- "a/data_backup/1.leetcode/70_\347\256\200\345\214\226\350\267\257\345\276\204/solution.md" +++ "b/data_backup/1.leetcode/70_\347\256\200\345\214\226\350\267\257\345\276\204/solution.md" @@ -4,30 +4,222 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + string path = "/a/./b/../../c/"; + string res; + res = sol.simplifyPath(path); + cout << res; + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ +public: + string names[1500]; + int temp = 0, endflag = 0; + void solve(string s, int start, int end, int n) + { + for (int i = start; i < n; i++) + { + if (s[i] == '/' && s[i + 1] != '/') + { + start = i + 1; + } + if (s[i] != '/' && s[i + 1] == '/') + { + end = i; + endflag = 1; + } + else if (s[i] != '/' && i == n - 1) + { + end = i; + endflag = 1; + } + if (endflag == 1) + { + string name = s.substr(start, end - start + 1); + if (name == "..") + { + if (temp > 0) + temp--; + } + else + { + temp = temp + 1; + names[temp] = name; + } + start = end + 1; + endflag = 0; + } + } + } + string simplifyPath(string path) + { + int n = path.length(); + names[temp] = "*"; + for (int i = n - 1; i >= 0; i++) + { + if (path[i] == '/') + continue; + if (path[i] != '/') + { + path = path.substr(0, i - 0 + 1); + break; + } + } + solve(path, 0, n - 1, n); + string str = ""; + if (temp == 0) + { + str = "/"; + } + else + { + for (int i = 0; i <= temp - 1; i++) + { + if (i == 0) + { + str += "/"; + } + else + { + str = str + names[i] + "/"; + } + } + str = str + names[temp]; + } + return str; + } +}; ``` ## 选项 ### A ```cpp +class Solution +{ +public: + string simplifyPath(string path) + { + if (path == "/") + return "/"; + for (int i = 0; i < path.size(); i++) + { + if (path[i] == '/') + path[i] = ' '; + } + vector stack; + istringstream str(path); + string buf; + while (str >> buf) + { + if (buf == ".." && !stack.empty()) + stack.pop_back(); + else if (buf != "." && buf != "..") + stack.push_back(buf); + } + if (stack.empty()) + return "/"; + string res; + for (int i = 0; i < stack.size(); i++) + { + res = res + "/" + stack[i]; + } + return res; + } +}; ``` ### B ```cpp +class Solution +{ +public: + string simplifyPath(string path) + { + stringstream ss(path); + vector strs; + strs.reserve(20); + string curr; + while (getline(ss, curr, '/')) + { + if (curr != "." && curr != "") + { + if (curr != "..") + { + strs.push_back(curr); + } + else if (!strs.empty()) + { + strs.pop_back(); + } + } + } + if (!strs.empty()) + { + string res = ""; + for (string str : strs) + { + res.append("/"); + res.append(str); + } + return res; + } + else + { + + return "/"; + } + } +}; ``` ### C ```cpp - +class Solution +{ +public: + string simplifyPath(string path) + { + vector tmp; + int i = 0; + while (i < path.size()) + { + while (path[i] == '/') + ++i; + if (i == path.size()) + break; + int start = i; + while (path[i] != '/' && i < path.size()) + ++i; + string s = path.substr(start, i - start); + if (s == "..") + { + if (tmp.size() != 0) + tmp.pop_back(); + } + else if (s != ".") + tmp.push_back(s); + } + if (tmp.empty()) + return "/"; + string ans; + for (int i = 0; i < tmp.size(); ++i) + ans += '/' + tmp[i]; + return ans; + } +}; ``` diff --git "a/data_backup/1.leetcode/71_\347\274\226\350\276\221\350\267\235\347\246\273/solution.md" "b/data_backup/1.leetcode/71_\347\274\226\350\276\221\350\267\235\347\246\273/solution.md" index cd00788c8..b60d4b81f 100644 --- "a/data_backup/1.leetcode/71_\347\274\226\350\276\221\350\267\235\347\246\273/solution.md" +++ "b/data_backup/1.leetcode/71_\347\274\226\350\276\221\350\267\235\347\246\273/solution.md" @@ -4,30 +4,184 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + + string word1 = "horse"; + string word2 = "ros"; + int res; + res = sol.minDistance(word1, word2); + cout << res; + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ +public: + int minDistance(string word1, string word2) + { + int len1 = word1.size(); + int len2 = word2.size(); + int **dp = new int *[len1 + 1]; + for (int i = 0; i < len1 + 1; i++) + dp[i] = new int[len2 + 1]; + for (int i = 0; i < len1 + 1; i++) + dp[i - 1][0] = i; + for (int i = 1; i < len2 + 1; i++) + dp[0][i - 1] = i; + for (int i = 1; i < len1 + 1; i++) + { + for (int j = 1; j < len2 + 1; j++) + { + if (word1[i - 1] == word2[j - 1]) + dp[i][j] = dp[i - 1][j - 1]; + else + dp[i][j] = (min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1); + } + } + return dp[len1][len2]; + } +}; ``` ## 选项 ### A ```cpp +class Solution +{ +public: + int minDistance(string word1, string word2) + { + int m = word1.size(), n = word2.size(); + + if (m == 0) + return n; + if (n == 0) + return m; + + int dp[m][n]; + bool w1 = false, w2 = false; + if (word1[0] == word2[0]) + { + w1 = true; + w2 = true; + dp[0][0] = 0; + } + else + dp[0][0] = 1; + for (int i = 1; i < m; i++) + { + if (!w1 && word1[i] == word2[0]) + { + w1 = true; + dp[i][0] = dp[i - 1][0]; + } + else + dp[i][0] = dp[i - 1][0] + 1; + } + for (int j = 1; j < n; j++) + { + if (!w2 && word1[0] == word2[j]) + { + w2 = true; + dp[0][j] = dp[0][j - 1]; + } + else + dp[0][j] = dp[0][j - 1] + 1; + } + for (int i = 1; i < m; i++) + for (int j = 1; j < n; j++) + if (word1[i] == word2[j]) + dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]) + 1, dp[i - 1][j - 1]); + else + dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1; + return dp[m - 1][n - 1]; + } +}; ``` ### B ```cpp +class Solution +{ +public: + int minDistance(string word1, string word2) + { + int n = word1.size(); + int m = word2.size(); + if (n * m == 0) + { + return n + m; + } + + int d[n + 1][m + 1]; + for (int i = 0; i < n + 1; ++i) + { + d[i][0] = i; + } + for (int i = 0; i < m + 1; ++i) + { + d[0][i] = i; + } + + for (int i = 1; i < n + 1; ++i) + { + for (int j = 1; j < m + 1; ++j) + { + int left = d[i - 1][j] + 1; + int down = d[i][j - 1] + 1; + int left_down = d[i - 1][j - 1]; + if (word1[i - 1] != word2[j - 1]) + { + left_down += 1; + } + d[i][j] = min(left, min(down, left_down)); + } + } + return d[n][m]; + } +}; ``` ### C ```cpp +class Solution +{ +public: + int minDistance(string word1, string word2) + { + int m = word1.size(), n = word2.size(); + + vector> dp(m + 1, vector(n + 1, 0)); + + for (int i = 1; i <= n; ++i) + dp[0][i] = dp[0][i - 1] + 1; + for (int i = 1; i <= m; ++i) + dp[i][0] = dp[i - 1][0] + 1; + for (int i = 1; i <= m; ++i) + { + for (int j = 1; j <= n; ++j) + { + if (word1[i - 1] == word2[j - 1]) + dp[i][j] = dp[i - 1][j - 1]; + else + dp[i][j] = min(min(dp[i - 1][j - 1], dp[i - 1][j]), dp[i][j - 1]) + 1; + } + } + return dp[m][n]; + } +}; ``` diff --git "a/data_backup/1.leetcode/72_\347\237\251\351\230\265\347\275\256\351\233\266/solution.md" "b/data_backup/1.leetcode/72_\347\237\251\351\230\265\347\275\256\351\233\266/solution.md" index 285b10b72..c0206fcd7 100644 --- "a/data_backup/1.leetcode/72_\347\237\251\351\230\265\347\275\256\351\233\266/solution.md" +++ "b/data_backup/1.leetcode/72_\347\237\251\351\230\265\347\275\256\351\233\266/solution.md" @@ -4,30 +4,222 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + int a = 3, b = 3; + vector> matrix = vector>(a, vector(b)) = {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}; + sol.setZeroes(matrix); + for (auto i : matrix) + { + for (auto j : i) + cout << j << " "; + cout << endl; + } + return 0; +} ``` ## 答案 ```cpp +class Solution +{ +public: + void setZeroes(vector> &matrix) + { + int row = matrix.size(); + int col = matrix[0].size(); + vector pos; + + for (int i = 0; i < row; i++) + { + for (int j = 0; j < col; j++) + { + if (matrix[i][j] == 0) + { + pos.push_back(i); + pos.push_back(j); + } + } + } + + for (int i = 1; i < pos.size(); i += 2) + { + int x = pos[i]; + int y = pos[i + 1]; + for (int j = 0; j < row; j++) + { + matrix[j][y] = 0; + } + for (int k = 0; k < col; k++) + { + matrix[x][k] = 0; + } + } + } +}; ``` ## 选项 ### A ```cpp +class Solution +{ +public: + void setZeroes(vector> &matrix) + { + if (matrix.size() <= 0) + return; + + int row = -1, col = -1; + + int i = 0; + + for (i = 0; i < matrix.size(); i++) + { + for (int j = 0; j < matrix[0].size(); j++) + { + if (matrix[i][j] == 0) + { + row = i; + col = j; + break; + } + } + if (row != -1) + break; + } + if (i == matrix.size()) + return; + + for (int k = i + 1; k < matrix.size(); k++) + { + for (int j = 0; j < matrix[0].size(); j++) + { + if (matrix[k][j] == 0) + { + matrix[row][j] = 0; + matrix[k][col] = 0; + } + } + } + + for (i = 0; i < matrix[0].size(); i++) + { + if (i == col) + continue; + if (matrix[row][i] == 0) + for (int j = 0; j < matrix.size(); j++) + matrix[j][i] = 0; + } + + for (i = 0; i < matrix.size(); i++) + { + if (i == row) + continue; + if (matrix[i][col] == 0) + for (int j = 0; j < matrix[0].size(); j++) + matrix[i][j] = 0; + } + + for (i = 0; i < matrix.size(); i++) + matrix[i][col] = 0; + for (i = 0; i < matrix[0].size(); i++) + matrix[row][i] = 0; + } +}; ``` ### B ```cpp - +class Solution +{ +public: + void setZeroes(vector> &matrix) + { + bool row = false, col = false; + int m = matrix.size(), n = matrix[0].size(); + for (int i = 0; i < m; ++i) + { + if (matrix[i][0] == 0) + col = true; + } + for (int i = 0; i < n; ++i) + { + if (matrix[0][i] == 0) + row = true; + } + for (int i = 1; i < m; ++i) + for (int j = 1; j < n; ++j) + { + if (matrix[i][j] == 0) + matrix[i][0] = 0, matrix[0][j] = 0; + } + for (int i = 1; i < m; ++i) + for (int j = 1; j < n; ++j) + { + if (matrix[i][0] == 0 || matrix[0][j] == 0) + matrix[i][j] = 0; + } + if (col) + { + for (int i = 0; i < m; ++i) + matrix[i][0] = 0; + } + if (row) + { + for (int i = 0; i < n; ++i) + matrix[0][i] = 0; + } + } +}; ``` ### C ```cpp - +class Solution +{ +public: + void setZeroes(vector> &matrix) + { + int m = matrix.size(), n = matrix[0].size(); + vector row(m, false), column(n, false); + int i, j; + for (i = 0; i < m; i++) + { + for (j = 0; j < n; j++) + { + if (matrix[i][j] == 0) + { + row[i] = true; + column[j] = true; + } + } + } + for (i = 0; i < m; i++) + { + if (row[i]) + { + for (j = 0; j < n; j++) + matrix[i][j] = 0; + } + } + for (i = 0; i < n; i++) + { + if (column[i]) + { + for (j = 0; j < m; j++) + matrix[j][i] = 0; + } + } + } +}; ``` diff --git "a/data_backup/1.leetcode/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/solution.md" "b/data_backup/1.leetcode/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/solution.md" index 729f645db..07c9f77e1 100644 --- "a/data_backup/1.leetcode/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/solution.md" +++ "b/data_backup/1.leetcode/73_\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/solution.md" @@ -4,30 +4,170 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + int a = 3, b = 4; + + vector> matrix = vector>(a, vector(b)) = {{1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 60}}; + int target = 3; + bool res; + res = sol.searchMatrix(matrix, target); + + cout << res; + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ +public: + bool searchMatrix(vector> &matrix, int target) + { + if (matrix.size() == 0 || matrix[0].size() == 0 || target < matrix[0][0] || target > matrix[matrix.size() - 1][matrix[0].size() - 1]) + return false; + int up = 0, down = matrix.size() - 1, left = 0, right = matrix[0].size() - 1; + while (up < down) + { + int mid = up + (down - up + 1) / 2; + if (matrix[mid][0] == target) + return true; + else if (matrix[mid][0] > target) + down = mid - 1; + else + up = mid + 1; + } + while (left < right) + { + int mid = left + (right - left + 1) / 2; + if (matrix[up][mid] == target) + return true; + else if (matrix[up][mid] > target) + right = mid - 1; + else + left = mid + 1; + } + if (matrix[up][left] == target) + return true; + return false; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + bool searchMatrix(vector> &matrix, int target) + { + if (matrix.size() == 0 || matrix[0].size() == 0) + return false; + decltype(matrix.size()) row_front = 0, c_front = 0, row_back = matrix.size(), c_back = matrix[0].size(); + while (row_front < row_back) + { + auto k = row_front + (row_back - row_front) / 2; + if (matrix[k][0] == target) + { + return true; + } + else if (matrix[k][0] < target) + { + row_front = k + 1; + } + else if (matrix[k][0] > target) + { + row_back = k; + } + } + if (row_front == 0) + { + return false; + } + decltype(matrix.size()) target_line = row_front - 1; + while (c_front < c_back) + { + auto j = c_front + (c_back - c_front) / 2; + if (matrix[target_line][j] == target) + { + return true; + } + else if (matrix[target_line][j] < target) + { + c_front = j + 1; + } + else if (matrix[target_line][j] > target) + { + c_back = j; + } + } + return false; + } +}; ``` ### B ```cpp +class Solution +{ +public: + bool searchMatrix(vector> &matrix, int target) + { + if (matrix.empty()) + return false; + + int m = matrix.size(); + int n = matrix[0].size(); + int left = 0; + int right = m * n - 1; + while (left <= right) + { + int middle = (left + right) / 2; + int middle_element = matrix[middle / n][middle % n]; + if (middle_element == target) + return true; + else if (middle_element < target) + left = middle + 1; + else + right = middle - 1; + } + return false; + } +}; ``` ### C ```cpp - +class Solution +{ +public: + bool searchMatrix(vector> &matrix, int target) + { + if (matrix.empty()) + return false; + int m = matrix.size(); + int n = matrix[0].size(); + int row = 0, col = n - 1; + while (row < m && col >= 0) + { + if (matrix[row][col] < target && col == n - 1) + row++; + else if (matrix[row][col] == target) + return true; + else + col--; + } + return false; + } +}; ``` diff --git "a/data_backup/1.leetcode/74_\351\242\234\350\211\262\345\210\206\347\261\273/solution.md" "b/data_backup/1.leetcode/74_\351\242\234\350\211\262\345\210\206\347\261\273/solution.md" index f5a9f0d60..1be741536 100644 --- "a/data_backup/1.leetcode/74_\351\242\234\350\211\262\345\210\206\347\261\273/solution.md" +++ "b/data_backup/1.leetcode/74_\351\242\234\350\211\262\345\210\206\347\261\273/solution.md" @@ -4,30 +4,146 @@ ## aop ### before ```cpp +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + int a = 3, b = 4; + vector nums = {2, 0, 2, 1, 1, 0}; + sol.sortColors(nums); + for (auto i : nums) + cout << i << " "; + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ +public: + void sortColors(vector &nums) + { + int zero = 0, two = nums.size() - 1; + int i = 0; + while (i <= two) + { + if (nums[i] <= 1) + { + int temp = nums[i]; + nums[i] = nums[zero]; + nums[zero] = temp; + zero++; + i++; + } + else + { + int temp = nums[i]; + nums[i] = nums[two]; + nums[two] = temp; + two--; + } + } + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + void sortColors(vector &nums) + { + int p0 = 0; + int p1 = 0; + int p2 = nums.size() - 1; + while (p1 <= p2) + { + if (nums[p1] == 0) + swap(nums[p1++], nums[p0++]); + else if (nums[p1] == 2) + swap(nums[p1], nums[p2--]); + else + p1++; + } + } +}; ``` ### B ```cpp - +class Solution +{ +public: + void sortColors(vector &nums) + { + int a[3] = {0}; + int b[3] = {0, 1, 2}; + int num = 0; + for (int i = 0; i < nums.size(); i++) + { + if (nums[i] == 0) + a[0]++; + if (nums[i] == 1) + a[1]++; + if (nums[i] == 2) + a[2]++; + } + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < a[i]; j++) + { + nums[num] = b[i]; + num++; + } + } + } +}; ``` ### C ```cpp - +class Solution +{ +public: + void sortColors(vector &nums) + { + int start = 0; + int end = nums.size() - 1; + int temp; + for (int i = 0; i < nums.size(); i++) + { + if (i > end) + break; + if (nums[i] == 2) + { + if (i == end) + continue; + temp = nums[i]; + nums[i] = nums[end]; + nums[end] = temp; + end--; + i--; + } + else if (nums[i] == 0) + { + if (i == start) + continue; + temp = nums[i]; + nums[i] = nums[start]; + nums[start] = temp; + start++; + i--; + } + } + } +}; ``` diff --git "a/data_backup/1.leetcode/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/solution.md" "b/data_backup/1.leetcode/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/solution.md" index a467a2e0b..9717e9ad5 100644 --- "a/data_backup/1.leetcode/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/solution.md" +++ "b/data_backup/1.leetcode/75_\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/solution.md" @@ -4,30 +4,174 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + string s = "ADOBECODEBANC"; + string t = "ABC"; + string res; + res = sol.minWindow(s, t); + cout << res; + return 0; +} ``` ## 答案 ```cpp +class Solution +{ +public: + string minWindow(string s, string t) + { + unordered_map hs, ht; + for (auto c : t) + ht[c]++; + + string res; + int cnt = 0; + for (int i = 0, j = 0; i < s.size(); i++) + { + hs[s[i]]++; + if (hs[s[i]] <= ht[s[i]]) + cnt++; + + while (hs[s[j]] > ht[s[j]]) + hs[s[j++]]--; + if (cnt == t.size()) + { + if (res.empty()) + res = s.substr(j, i - j); + } + } + return res; + } +}; ``` ## 选项 ### A ```cpp +class Solution +{ +public: + string minWindow(string s, string t) + { + vector need(58, 0), window(58, 0); + for (auto c : t) + ++need[c - 'A']; + int left = 0, right = 0; + int count = 0; + int target = 0; + for (auto c : need) + { + if (c) + ++target; + } + int start = 0; + int len = INT_MAX; + while (right < s.size()) + { + auto c = s[right] - 'A'; + ++right; + if (need[c] > 0) + { + ++window[c]; + if (need[c] == window[c]) + ++count; + } + while (count == target) + { + if (right - left < len) + { + start = left; + len = right - left; + } + auto c = s[left] - 'A'; + ++left; + if (need[c] > 0) + { + if (need[c] == window[c]) + --count; + --window[c]; + } + } + } + return len == INT_MAX ? "" : s.substr(start, len); + } +}; ``` ### B ```cpp - +class Solution +{ +public: + string minWindow(string s, string t) + { + unordered_map findChar; + int n1 = s.length(), n2 = t.length(), i, j; + for (i = 0; i < n2; i++) + findChar[t[i]]++; + int start = 0, cnt = 0, minLength = n1 + 1, now = 0; + for (i = 0; i < n1; i++) + { + if ((--findChar[s[i]]) >= 0) + cnt++; + if (cnt == n2) + { + while (++findChar[s[now]] <= 0) + now++; + cnt--; + if (i - now + 1 < minLength) + { + start = now; + minLength = i - now + 1; + } + now++; + } + } + return minLength > n1 ? "" : s.substr(start, minLength); + } +}; ``` ### C ```cpp - +class Solution +{ +public: + string minWindow(string s, string t) + { + string ans = ""; + map lettercount; + for (char c : t) + ++lettercount[c]; + int count = 0, left = 0, minlen = INT_MAX; + for (int i = 0; i < s.size(); ++i) + { + if (--lettercount[s[i]] >= 0) + ++count; + while (count == t.size()) + { + if (minlen > i - left + 1) + { + minlen = i - left + 1; + ans = s.substr(left, minlen); + } + if (++lettercount[s[left]] > 0) + --count; + ++left; + } + } + return ans; + } +}; ``` diff --git "a/data_backup/1.leetcode/76_\347\273\204\345\220\210/solution.md" "b/data_backup/1.leetcode/76_\347\273\204\345\220\210/solution.md" index 0377b0b1f..d39ebb680 100644 --- "a/data_backup/1.leetcode/76_\347\273\204\345\220\210/solution.md" +++ "b/data_backup/1.leetcode/76_\347\273\204\345\220\210/solution.md" @@ -6,30 +6,166 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + int n = 4; + int k = 2; + vector> res; + res = sol.combine(n, k); + for (auto i : res) + { + for (auto j : i) + cout << j << " "; + cout << endl; + } + return 0; +} ``` ## 答案 ```cpp +class Solution +{ +public: + vector> combine(int n, int k) + { + vector ones(n), lib(n); + vector> result; + for (int i = 0; i < n; i++) + { + ones[i] = (i < k) ? 1 : 0; + lib[i] = i + 1; + } + while (1) + { + vector solution; + bool flag = false; + for (int i = 0; i < n; i++) + { + if (ones[i]) + solution.push_back(lib[i]); + } + result.push_back(solution); + int count = 0; + for (int i = 0; i < n - 1; i++) + { + if (ones[i] == 1 && ones[i + 1] == 0) + { + flag = true; + ones[i] = 0; + ones[i + 1] = 1; + for (int j = 0; j < i; j++) + { + ones[j] = count > 0 ? 1 : 0; + } + break; + } + else if (ones[i]) + count++; + } + if (!flag) + break; + } + return result; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + vector> combine(int n, int k) + { + vector> ans; + vector tmp; + combineDFS(n, k, 1, tmp, ans); + return ans; + } + void combineDFS(int n, int k, int level, vector &tmp, vector> &ans) + { + if (tmp.size() == k) + { + ans.push_back(tmp); + return; + } + for (int i = level; i <= n; ++i) + { + tmp.push_back(i); + combineDFS(n, k, i + 1, tmp, ans); + tmp.pop_back(); + } + } +}; ``` ### B ```cpp - +class Solution +{ +public: + vector> combine(int n, int k) + { + vector> result; + int i = 0; + vector p(k, 0); + while (i >= 0) + { + p[i]++; + if (p[i] > n) + --i; + else if (i == k - 1) + result.push_back(p); + else + { + ++i; + p[i] = p[i - 1]; + } + } + return result; + } +}; ``` ### C ```cpp +class Solution +{ +public: + vector> res; + vector tmp; + int n; + void backtrack(vector &tmp, int idx, int cnt, int k) + { + if (cnt == k) + { + res.push_back(tmp); + return; + } + for (int i = idx + 1; i <= n; ++i) + { + tmp[cnt] = i; + backtrack(tmp, i, cnt + 1, k); + tmp[cnt] = 0; + } + } + vector> combine(int n, int k) + { + this->n = n; + tmp = vector(k, 0); + backtrack(tmp, 0, 0, k); + return res; + } +}; ``` diff --git "a/data_backup/1.leetcode/77_\345\255\220\351\233\206/solution.md" "b/data_backup/1.leetcode/77_\345\255\220\351\233\206/solution.md" index 289a889cb..d2ab9a9dc 100644 --- "a/data_backup/1.leetcode/77_\345\255\220\351\233\206/solution.md" +++ "b/data_backup/1.leetcode/77_\345\255\220\351\233\206/solution.md" @@ -4,30 +4,141 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp - +int main() +{ + Solution sol; + vector nums = {1, 2, 3}; + vector> res; + res = sol.subsets(nums); + for (auto i : res) + { + for (auto j : i) + cout << j << " "; + cout << endl; + } + return 0; +} ``` ## 答案 ```cpp +class Solution +{ +private: + void subsetss(int numssize, int i, vector &nums, vector> &output, vector &newtemp) + { + if (i >= numssize) + { + output.push_back(newtemp); + return; + } + newtemp.push_back(nums[i]); + subsetss(numssize, i + 1, nums, output, newtemp); + newtemp.pop_back(); + } +public: + vector> subsets(vector &nums) + { + int numssize = nums.size(); + int i = 0; + vector> output; + vector newtemp; + subsetss(numssize, i, nums, output, newtemp); + return output; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + vector> subsets(vector &nums) + { + int numssize = nums.size(); + int numscount = 1 << numssize; + vector> output; + int i = 0; + while (i < numscount) + { + vector newtemp; + for (int x = 0; x < numssize; x++) + { + if ((1 << x) & i) + { + newtemp.push_back(nums[x]); + } + } + i++; + output.push_back(newtemp); + } + return output; + } +}; ``` ### B ```cpp +class Solution +{ +public: + vector> subsets(vector &nums) + { + vector> output; + vector subset; + int length = nums.size(); + output.push_back(subset); + int current = 0; + int output_size; + while (current < length) + { + output_size = output.size(); + for (int i = 0; i < output_size; i++) + { + vector newinsert = output[i]; + newinsert.insert(newinsert.end(), nums[current]); + output.push_back(newinsert); + } + current++; + } + return output; + } +}; ``` ### C ```cpp +class Solution +{ +public: + vector> subsets(vector &nums) + { + vector> res; + if (nums.empty()) + return res; + res.push_back({}); + int n = nums.size(); + for (int i = 0; i < n; i++) + { + int nRes = res.size(); + for (int j = 0; j < nRes; j++) + { + vector temp = res[j]; + temp.push_back(nums[i]); + res.push_back(temp); + } + } + return res; + } +}; ``` diff --git "a/data_backup/1.leetcode/78_\345\215\225\350\257\215\346\220\234\347\264\242/solution.md" "b/data_backup/1.leetcode/78_\345\215\225\350\257\215\346\220\234\347\264\242/solution.md" index 04e9be3fd..ef634f2f7 100644 --- "a/data_backup/1.leetcode/78_\345\215\225\350\257\215\346\220\234\347\264\242/solution.md" +++ "b/data_backup/1.leetcode/78_\345\215\225\350\257\215\346\220\234\347\264\242/solution.md" @@ -4,30 +4,200 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + int a = 3, b = 4; + vector> board = {{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}}; + string word = "ABCCED"; + bool res; + res = sol.exist(board, word); + cout << res; + return 0; +} ``` ## 答案 ```cpp +class Solution +{ +public: + vector> dirs{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; + bool exist(vector> &board, string word) + { + int n = board.size(), m = board[0].size(); + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < m; ++j) + { + if (dfs(board, word, 0, i, j)) + return true; + } + } + return false; + } + bool dfs(vector> &board, string &word, int idx, int x, int y) + { + if (board[x][y] != word[idx]) + return false; + if (idx == word.size() - 1) + return true; + char tmp = board[x][y]; + board[x][y] = 0; + for (const auto &dir : dirs) + { + if (x + dir[0] < 0 || x + dir[0] >= board.size() || y + dir[1] < 0 || y + dir[1] >= board[0].size()) + continue; + if (dfs(board, word, idx, x + dir[0], y + dir[1])) + return true; + } + board[x][y] = tmp; + return false; + } +}; ``` ## 选项 ### A ```cpp +class Solution +{ +public: + bool search(vector> &board, string word, int i, int j, int pos) + { + if (pos == word.size()) + return true; + if (i < 0 || j < 0 || i >= board.size() || j >= board[i].size()) + return false; + if (word[pos] != board[i][j]) + return false; + char temp = board[i][j]; + board[i][j] = '.'; + bool result = search(board, word, i + 1, j, pos + 1) || search(board, word, i - 1, j, pos + 1) || search(board, word, i, j - 1, pos + 1) || search(board, word, i, j + 1, pos + 1); + board[i][j] = temp; + return result; + } + bool exist(vector> &board, string word) + { + for (int i = 0; i < board.size(); i++) + for (int j = 0; j < board[i].size(); j++) + if (search(board, word, i, j, 0)) + return true; + return false; + } +}; ``` ### B ```cpp +class Solution +{ +public: + bool exist(vector> &board, string word) + { + rows = board.size(), columns = board[0].size(); + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < columns; j++) + { + if (backtrack(board, word, i, j, 0)) + return true; + } + } + return false; + } + +private: + int rows, columns; + bool backtrack(vector> &board, string &word, int x, int y, int index) + { + + if (x >= rows || x < 0 || y >= columns || y < 0 || board[x][y] != word[index]) + { + return false; + } + + if (index == word.size() - 1) + { + return true; + } + board[x][y] = ' '; + + if (backtrack(board, word, x - 1, y, index + 1) || backtrack(board, word, x + 1, y, index + 1) || backtrack(board, word, x, y - 1, index + 1) || backtrack(board, word, x, y + 1, index + 1)) + { + return true; + } + + board[x][y] = word[index]; + return false; + } +}; ``` ### C ```cpp - +class Solution +{ +public: + bool DFS(vector> &board, string word, int row, int col, int index, vector> &flag) + { + if (index == word.length()) + { + return true; + } + if (row < 0 || row >= board.size() || col < 0 || col >= board[0].size() || board[row][col] != word[index]) + { + return false; + } + if (0 == flag[row][col]) + { + flag[row][col] = 1; + if (DFS(board, word, row - 1, col, index + 1, flag)) + { + return true; + } + if (DFS(board, word, row + 1, col, index + 1, flag)) + { + return true; + } + if (DFS(board, word, row, col - 1, index + 1, flag)) + { + return true; + } + if (DFS(board, word, row, col + 1, index + 1, flag)) + { + return true; + } + flag[row][col] = 0; + } + return false; + } + bool exist(vector> &board, string word) + { + int m = board.size(); + int n = board[0].size(); + int k = word.length(); + vector> flag(m, vector(n, 0)); + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + if (DFS(board, word, i, j, 0, flag)) + { + return true; + } + } + } + return false; + } +}; ``` diff --git "a/data_backup/1.leetcode/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/solution.md" "b/data_backup/1.leetcode/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/solution.md" index c5b3b7aef..40586b0b2 100644 --- "a/data_backup/1.leetcode/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/solution.md" +++ "b/data_backup/1.leetcode/79_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/solution.md" @@ -31,30 +31,135 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + vector nums = {1, 1, 1, 2, 2, 3}; + int res; + res = sol.removeDuplicates(nums); + cout << res << endl; + for (auto i : nums) + cout << i << " "; + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ +public: + int removeDuplicates(vector &nums) + { + int n = nums.size(); + if (n <= 2) + { + return n; + } + int sp = 1; + for (int fp = 2; fp < n; fp++) + { + if (nums[fp] != nums[sp - 1]) + { + nums[++sp] = nums[fp]; + } + } + return sp; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + int removeDuplicates(vector &nums) + { + unordered_map maps; + int n = 0; + for (int i = 0; i < nums.size(); ++i) + { + if (maps[nums[i]] < 2) + { + maps[nums[i]]++; + n++; + } + else + { + nums.erase(nums.begin() + i); + --i; + } + } + return n; + } +}; ``` ### B ```cpp - +class Solution +{ +public: + int removeDuplicates(vector &nums) + { + if (nums.empty()) + return 0; + int len; + len = 1; + for (int i = 1; i < nums.size(); i++) + { + if (i == 1 || nums[i] != nums[len - 2]) + { + nums[len] = nums[i]; + ++len; + } + } + return len; + } +}; ``` ### C ```cpp +class Solution +{ +public: + int removeDuplicates(vector &nums) + { + + int ret = nums.size(), quantity = nums[0], num = 0; + + for (vector::iterator iter = nums.begin(); iter != nums.end(); iter++) + { + + if (*iter == quantity) + { + num++; + + if (num > 2) + { + iter = nums.erase(iter); + iter--; + ret--; + num = 2; + } + } + else + { + quantity = *iter; + num = 1; + } + } + return ret; + } +}; ``` diff --git "a/data_backup/1.leetcode/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/solution.md" "b/data_backup/1.leetcode/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/solution.md" index 2704330ea..7b5e412bd 100644 --- "a/data_backup/1.leetcode/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/solution.md" +++ "b/data_backup/1.leetcode/80_\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204 II/solution.md" @@ -33,30 +33,224 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp - +int main() +{ + Solution sol; + vector nums = {2, 5, 6, 0, 0, 1, 2}; + int target = 0; + int res; + res = sol.search(nums, target); + cout << res << endl; + return 0; +} ``` ## 答案 ```cpp +class Solution +{ +public: + bool search(vector &nums, int target) + { + if (nums.empty() || nums.size() == 0) + { + return false; + } + int start = 0; + int end = nums.size() - 1; + int mid; + while (start <= end) + { + mid = start + (end - start) / 2; + if (nums[mid] == target) + { + return true; + } + if (nums[start] < nums[mid]) + { + + if (nums[mid] > target && nums[start] <= target) + { + end = mid - 1; + } + else + { + start = mid + 1; + } + } + else + { + + if (nums[mid] < target && nums[end] >= target) + { + start = mid + 1; + } + else + { + end = mid - 1; + } + } + } + return false; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + bool search(const vector &nums, int target) + { + int first = 0, last = nums.size(); + while (first != last) + { + const int mid = first + (last - first) / 2; + if (nums[mid == target]) + { + return true; + } + if (nums[first] < nums[mid]) + { + if (nums[first] <= target && target < nums[mid]) + { + last = mid; + } + else + { + first = mid + 1; + } + } + else if (nums[first] > nums[mid]) + { + if (nums[mid] < target && target <= nums[last - 1]) + { + first = mid + 1; + } + else + { + last = mid; + } + } + else + { + first++; + } + } + return false; + } +}; ``` ### B ```cpp - +class Solution +{ +public: + bool search(vector &nums, int target) + { + if (nums.empty()) + return false; + int i = 0, j = nums.size() - 1, mid; + mid = (i + j) / 2; + if (nums[mid] == target) + return true; + else if (mid != i && nums[i] == nums[mid]) + { + i = mid + 1; + while (i <= j && nums[i] == nums[mid]) + i++; + if (i == nums.size()) + { + i = 0; + j = mid - 1; + while (i <= j && nums[j] == nums[mid]) + j--; + } + } + while (i <= j) + { + mid = (i + j) / 2; + if (nums[mid] == target) + return true; + else if (nums[mid] >= nums[i]) + { + if (target >= nums[i] && target < nums[mid]) + { + j = mid - 1; + while (i <= j && nums[j] == nums[mid]) + j--; + } + else + { + i = mid + 1; + while (i <= j && nums[i] == nums[mid]) + i++; + } + } + else + { + if (target > nums[mid] && target <= nums[j]) + { + i = mid + 1; + while (i <= j && nums[i] == nums[mid]) + i++; + } + else + { + j = mid - 1; + while (i <= j && nums[j] == nums[mid]) + j--; + } + } + } + return false; + } +}; ``` ### C ```cpp - +class Solution +{ +public: + bool search(vector &nums, int &target) + { + int n = nums.size(); + if (n == 0) + return false; + int left = 0, right = n - 1; + while (left <= right) + { + int mid = (left + right) / 2; + if (nums[mid] == target) + return true; + else if (nums[mid] < nums[right]) + { + if (nums[mid] < target && nums[right] >= target) + left = mid + 1; + else + right = mid - 1; + } + else if (nums[mid] > nums[right]) + { + if (nums[left] <= target && nums[mid] > target) + right = mid - 1; + else + left = mid + 1; + } + else + --right; + } + return false; + } +}; ``` diff --git "a/data_backup/1.leetcode/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/solution.md" "b/data_backup/1.leetcode/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/solution.md" index 1ab02a0b8..c2255cd01 100644 --- "a/data_backup/1.leetcode/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/solution.md" +++ "b/data_backup/1.leetcode/81_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 II/solution.md" @@ -4,7 +4,15 @@ ## aop ### before ```cpp +#include +using namespace std; +struct ListNode +{ + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; ``` ### after ```cpp @@ -13,21 +21,129 @@ ## 答案 ```cpp - +class Solution +{ +public: + ListNode *deleteDuplicates(ListNode *head) + { + if (!head || !head->next) + return head; + auto dummy = new ListNode(-1); + dummy->next = head; + auto pre = dummy, cur = head; + while (cur && cur->next) + { + if (cur->val != cur->next->val) + { + pre = cur; + cur = cur->next; + } + else + { + while (cur->next && cur->val == cur->next->val) + { + cur = cur->next; + } + } + } + return dummy->next; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + ListNode *deleteDuplicates(ListNode *head) + { + if (!head || !head->next) + return head; + if (head->val == head->next->val) + { + while (head->next && head->val == head->next->val) + { + head = head->next; + } + return deleteDuplicates(head->next); + } + else + head->next = deleteDuplicates(head->next); + return head; + } +}; ``` ### B ```cpp - +class Solution +{ +public: + ListNode *deleteDuplicates(ListNode *head) + { + ListNode *new_head = new ListNode(0); + ListNode *cur = new_head; + ListNode *temp1 = head; + while (temp1) + { + ListNode *temp2 = temp1->next; + if (!temp2) + { + cur->next = temp1; + temp1 = temp1->next; + cur = cur->next; + } + else if (temp2 && temp1->val != temp2->val) + { + cur->next = temp1; + temp1 = temp1->next; + cur = cur->next; + } + else + { + while (temp2 && temp1->val == temp2->val) + { + temp2 = temp2->next; + } + temp1 = temp2; + } + } + cur->next = NULL; + return new_head->next; + } +}; ``` ### C ```cpp +class Solution +{ +public: + ListNode *deleteDuplicates(ListNode *head) + { + if (head == NULL || head->next == NULL) + return head; + ListNode *L = new ListNode(0); + L->next = head; + ListNode *slow = L; + while (slow->next) + { + ListNode *fast = slow->next; + while (fast->next && fast->val == fast->next->val) + { + fast = fast->next; + } + if (slow->next == fast) + slow = slow->next; + else + { + slow->next = fast->next; + } + } + return L->next; + } +}; ``` diff --git "a/data_backup/1.leetcode/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/solution.md" "b/data_backup/1.leetcode/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/solution.md" index 9dacc8517..023444117 100644 --- "a/data_backup/1.leetcode/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/solution.md" +++ "b/data_backup/1.leetcode/82_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/solution.md" @@ -4,7 +4,15 @@ ## aop ### before ```cpp +#include +using namespace std; +struct ListNode +{ + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; ``` ### after ```cpp @@ -13,21 +21,104 @@ ## 答案 ```cpp - +class Solution +{ +public: + ListNode *deleteDuplicates(ListNode *head) + { + if (!head || !head->next) + return head; + head->next = deleteDuplicates(head->next); + if (head->val != head->next->val) + head = head->next; + return head; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + ListNode *deleteDuplicates(ListNode *head) + { + if (head == NULL || head->next == NULL) + return head; + ListNode *p = head; + ListNode *q = head->next; + while (p->next != NULL) + { + if (p->val == q->val) + { + if (q->next == NULL) + p->next = NULL; + else + { + p->next = q->next; + q = q->next; + } + } + else + { + p = p->next; + q = q->next; + } + } + return head; + } +}; ``` ### B ```cpp - +class Solution +{ +public: + ListNode *deleteDuplicates(ListNode *head) + { + ListNode *cur = head; + while (cur != NULL) + { + ListNode *rear = cur->next; + if (rear == NULL) + return head; + if (cur->val == rear->val) + cur->next = rear->next; + else + cur = cur->next; + } + return head; + } +}; ``` ### C ```cpp +class Solution +{ +public: + ListNode *deleteDuplicates(ListNode *head) + { + if (head == NULL) + return head; + ListNode *ptr{head}; + while (ptr->next != NULL) + { + if (ptr->val == ptr->next->val) + { + ListNode *p = ptr->next; + ptr->next = p->next; + delete p; + } + else + { + ptr = ptr->next; + } + } + return head; + } +}; ``` diff --git "a/data_backup/1.leetcode/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/solution.md" "b/data_backup/1.leetcode/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/solution.md" index 2ba104f54..978359db3 100644 --- "a/data_backup/1.leetcode/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/solution.md" +++ "b/data_backup/1.leetcode/83_\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/solution.md" @@ -4,30 +4,163 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp - +int main() +{ + Solution sol; + vector heights = {2, 1, 5, 6, 2, 3}; + int res; + res = sol.largestRectangleArea(heights); + cout << res << endl; + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ +public: + int largestRectangleArea(vector &heights) + { + if (heights.empty()) + return 0; + stack st; + heights.push_back(0); + int res = 0; + for (int i = 0; i < heights.size(); i++) + { + while (!st.empty() && heights[i] < heights[st.top()]) + { + int curHeight = heights[st.top()]; + st.pop(); + int width = st.empty() ? i : i - st.top(); + if (width * curHeight > res) + res = width * curHeight; + } + st.push(i); + } + return res; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + int largestRectangleArea(vector &heights) + { + int sz = heights.size(); + int ma = 0; + for (int i = 0; i < sz; i++) + { + int len = 1; + int hei = heights[i]; + int sta = i - 1, en = i + 1; + while (sta >= 0 && heights[sta] >= hei) + { + len++; + sta--; + } + while (en < sz && heights[en] >= hei) + { + len++; + en++; + } + ma = max(ma, len * hei); + } + return ma; + } +}; ``` ### B ```cpp +class Solution +{ +public: + int largestRectangleArea(vector &heights) + { + int n = heights.size(); + + vector left(n), right(n, n); + + stack mono_stack; + for (int i = 0; i < n; ++i) + { + + while (!mono_stack.empty() && heights[mono_stack.top()] >= heights[i]) + { + right[mono_stack.top()] = i; + mono_stack.pop(); + } + + left[i] = (mono_stack.empty() ? -1 : mono_stack.top()); + + mono_stack.push(i); + } + + int ans = 0; + for (int i = 0; i < n; ++i) + { + + ans = max(ans, (right[i] - left[i] - 1) * heights[i]); + } + return ans; + } +}; ``` ### C ```cpp - +class Solution +{ +public: + int largestRectangleArea(vector &heights) + { + int n = heights.size(); + stack index; + int area = 0; + for (int i = 0; i < heights.size(); i++) + { + if (index.empty() || heights[index.top()] < heights[i]) + index.push(i); + else + { + while (!index.empty() && heights[index.top()] >= heights[i]) + { + int tmp = index.top(); + index.pop(); + int length = 0; + if (index.empty()) + length = i; + else + length = i - index.top() - 1; + area = max(area, length * heights[tmp]); + } + index.push(i); + } + } + while (!index.empty()) + { + int tmp = index.top(); + index.pop(); + int length = 0; + if (index.empty()) + length = n; + else + length = n - index.top() - 1; + area = max(area, length * heights[tmp]); + } + return area; + } +}; ``` diff --git "a/data_backup/1.leetcode/84_\346\234\200\345\244\247\347\237\251\345\275\242/solution.md" "b/data_backup/1.leetcode/84_\346\234\200\345\244\247\347\237\251\345\275\242/solution.md" index 926f7de0d..c1613e6b5 100644 --- "a/data_backup/1.leetcode/84_\346\234\200\345\244\247\347\237\251\345\275\242/solution.md" +++ "b/data_backup/1.leetcode/84_\346\234\200\345\244\247\347\237\251\345\275\242/solution.md" @@ -4,30 +4,222 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp - +int main() +{ + Solution sol; + vector> matrix = {{'1', '0', '1', '0', '0'}, + {'1', '0', '1', '1', '1'}, + {'1', '1', '1', '1', '1'}, + {'1', '0', '0', '1', '0'}}; + int res; + res = sol.maximalRectangle(matrix); + cout << res << endl; + return 0; +} ``` ## 答案 ```cpp +class Solution +{ +public: + int largestRectangleArea(vector &heights) + { + stack h; + heights.push_back(0); + int ans = 0, hsize = heights.size(); + for (int i = 0; i < hsize; i++) + { + while (!h.empty() && heights[h.top()] > heights[i]) + { + int top = h.top(); + h.pop(); + ans = max(ans, heights[top] * (h.empty() ? i : (i - h.top()))); + } + h.push(i); + } + return ans; + } + int maximalRectangle(vector> &matrix) + { + if (matrix.empty()) + return 0; + int n = matrix.size(), m = matrix[0].size(), ans = 0; + vector> num(n, vector(m, 0)); + for (int j = 0; j < m; j++) + { + num[0][j] = (matrix[0][j] == '0') ? 0 : 1; + for (int i = 1; i < n; i++) + num[i][j] = (matrix[i][j] == '0') ? 0 : num[i - 1][j] + 1; + } + for (int i = 0; i < n; i++) + { + int area = largestRectangleArea(num[i]); + ans = max(ans, area); + } + return ans; + } +}; ``` ## 选项 ### A ```cpp +class Solution +{ +public: + int maximalRectangle(vector> &matrix) + { + int res = 0; + vector height; + for (int i = 0; i < matrix.size(); ++i) + { + height.resize(matrix[i].size()); + for (int j = 0; j < matrix[i].size(); ++j) + { + height[j] = matrix[i][j] == '0' ? 0 : (1 + height[j]); + } + res = max(res, largestRectangleArea(height)); + } + return res; + } + int largestRectangleArea(vector &heights) + { + if (heights.empty()) + return 0; + stack st; + heights.push_back(0); + int res0 = 0; + for (int i = 0; i < heights.size(); i++) + { + while (!st.empty() && heights[i] < heights[st.top()]) + { + int curHeight = heights[st.top()]; + st.pop(); + int width = st.empty() ? i : i - st.top() - 1; + if (width * curHeight > res0) + res0 = width * curHeight; + } + st.push(i); + } + return res0; + } +}; ``` ### B ```cpp +class Solution +{ +public: + int maximalRectangle(vector> &matrix) + { + if (matrix.size() == 0) + { + return 0; + } + int m = matrix.size(); + int n = matrix[0].size(); + int left[n]; + int right[n]; + int height[n]; + + memset(right, n, sizeof(right)); + memset(left, n, sizeof(left)); + memset(height, n, sizeof(height)); + + int maxarea = 0; + for (int i = 0; i < m; ++i) + { + int cur_left = 0, cur_right = n; + for (int j = 0; j < n; ++j) + { + if (matrix[i][j] == '1') + { + height[j]++; + } + else + { + height[j] = 0; + } + } + + for (int j = 0; j < n; ++j) + { + if (matrix[i][j] == '1') + { + left[j] = max(left[j], cur_left); + } + else + { + left[j] = 0, cur_left = j + 1; + } + } + + for (int j = n - 1; j >= 0; --j) + { + if (matrix[i][j] == '1') + { + right[j] = min(right[j], cur_right); + } + else + { + right[j] = n; + cur_right = j; + } + } + + for (int j = 0; j < n; ++j) + { + maxarea = max(maxarea, (right[j] - left[j]) * height[j]); + } + } + return maxarea; + } +}; ``` ### C ```cpp +class Solution +{ +public: + int maximalRectangle(vector> &matrix) + { + if (matrix.size() == 0) + { + return 0; + } + int maxarea = 0; + int dp[matrix.size()][matrix[0].size()]; + memset(dp, 0, sizeof(dp)); + + for (int i = 0; i < matrix.size(); ++i) + { + for (int j = 0; j < matrix[0].size(); ++j) + { + if (matrix[i][j] == '1') + { + dp[i][j] = j == 0 ? 1 : dp[i][j - 1] + 1; + int width = dp[i][j]; + for (int k = i; k >= 0; k--) + { + width = min(width, dp[k][j]); + maxarea = max(maxarea, width * (i - k + 1)); + } + } + } + } + return maxarea; + } +}; ``` diff --git "a/data_backup/1.leetcode/85_\345\210\206\351\232\224\351\223\276\350\241\250/solution.md" "b/data_backup/1.leetcode/85_\345\210\206\351\232\224\351\223\276\350\241\250/solution.md" index 1d3467a28..8612e2016 100644 --- "a/data_backup/1.leetcode/85_\345\210\206\351\232\224\351\223\276\350\241\250/solution.md" +++ "b/data_backup/1.leetcode/85_\345\210\206\351\232\224\351\223\276\350\241\250/solution.md" @@ -4,7 +4,15 @@ ## aop ### before ```cpp +#include +using namespace std; +struct ListNode +{ + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; ``` ### after ```cpp @@ -13,21 +21,161 @@ ## 答案 ```cpp - +class Solution +{ +public: + ListNode *partition(ListNode *head, int x) + { + if (!head || !head->next) + return head; + ListNode *p = head; + ListNode *q = head; + ListNode *qq = q; + int flag = 0; + while (q && q->val < x) + { + flag = 1; + p = q; + q = q->next; + } + while (q) + { + if (flag == 0 && q->val < x) + { + qq->next = q->next; + q->next = p; + p = q; + head = p; + q = qq->next; + flag = 1; + } + else if (flag == 1 && q->val < x) + { + qq->next = q->next; + q->next = p->next; + p->next = q; + p = p->next; + q = qq->next; + } + } + return head; + } +}; ``` ## 选项 ### A ```cpp +class Solution +{ +public: + ListNode *partition(ListNode *head, int x) + { + ListNode *dummy = new ListNode(-1); + dummy->next = head; + ListNode *pre = dummy, *cur; + while (pre->next && pre->next->val < x) + pre = pre->next; + cur = pre; + while (cur->next) + { + if (cur->next->val < x) + { + ListNode *tmp = cur->next; + cur->next = tmp->next; + tmp->next = pre->next; + pre->next = tmp; + pre = pre->next; + } + else + { + cur = cur->next; + } + } + return dummy->next; + } +}; ``` ### B ```cpp - +class Solution +{ +public: + ListNode *partition(ListNode *head, int x) + { + ListNode *lessNode = new ListNode(-1); + ListNode *moreNode = new ListNode(-1); + ListNode *l = lessNode; + ListNode *m = moreNode; + while (head) + { + if (head->val >= x) + { + m->next = head; + m = m->next; + } + else + { + l->next = head; + l = l->next; + } + head = head->next; + } + l->next = moreNode->next; + m->next = NULL; + return lessNode->next; + } +}; ``` ### C ```cpp - +class Solution +{ +public: + ListNode *partition(ListNode *head, int x) + { + if (head == NULL || head->next == NULL) + return head; + ListNode *ahead, *p, *after, *p1; + p = head; + while (p && p->val < x) + { + ahead = p; + p = p->next; + } + if (p == head) + ahead = p; + if (p) + after = p->next; + p1 = p; + while (after) + { + if (after->val < x) + { + if (p == head) + { + head = after; + ahead = head; + } + else + { + ahead->next = after; + ahead = after; + } + p1->next = after->next; + } + else + { + p1 = after; + } + after = after->next; + } + if (ahead != p) + ahead->next = p; + return head; + } +}; ``` diff --git "a/data_backup/1.leetcode/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/solution.md" "b/data_backup/1.leetcode/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/solution.md" index d0943744e..72efaedf1 100644 --- "a/data_backup/1.leetcode/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/solution.md" +++ "b/data_backup/1.leetcode/86_\346\211\260\344\271\261\345\255\227\347\254\246\344\270\262/solution.md" @@ -62,30 +62,180 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp - +int main() +{ + Solution sol; + string s1 = "great"; + string s2 = "rgeat"; + bool res; + res = sol.isScramble(s1, s2); + cout << res << endl; + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ + unordered_map memo; + +public: + bool isScramble(string s1, string s2) + { + if (memo.count(s1 + '_' + s2) != 0) + { + return memo[s1 + '_' + s2]; + } + if (s1 == s2) + { + memo[s1 + '_' + s2] = true; + return true; + } + else if (s1.size() == 1) + { + memo[s1 + '_' + s2] = false; + return false; + } + bool do_it = false; + bool not_do = false; + for (int i = 1; i < s1.size(); i++) + { + do_it = isScramble(s1.substr(0, i), s2.substr(s1.size() - 1, s1.size())); + not_do = isScramble(s1.substr(0, i), s2.substr(0, i)); + + if (do_it || not_do) + { + memo[s1 + '_' + s2] = true; + return true; + } + } + memo[s1 + '_' + s2] = false; + return false; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + bool isScramble(string s1, string s2) + { + int n1 = s1.length(), n2 = s2.length(); + if (n1 != n2) + return false; + vector>> dp(n1 + 1, vector>(n1 + 1, vector(n1 + 1, false))); + int i, j, k; + for (i = 1; i <= n1; i++) + { + for (j = 1; j <= n1; j++) + { + dp[i][j][1] = (s1[i - 1] == s2[j - 1]); + } + } + for (int len = 2; len <= n1; len++) + { + for (i = 1; i <= n1 && i + len <= n1 + 1; i++) + { + for (j = 1; j <= n1 && j + len <= n1 + 1; j++) + { + for (k = 1; k < len; k++) + { + if (dp[i][j][k] && dp[i + k][j + k][len - k]) + { + dp[i][j][len] = true; + break; + } + if (dp[i][j + len - k][k] && dp[i + k][j][len - k]) + { + dp[i][j][len] = true; + break; + } + } + } + } + } + + return dp[1][1][n1]; + } +}; ``` ### B ```cpp - +class Solution +{ +public: + bool isScramble(string s1, string s2) + { + + if (s1.size() != s2.size()) + return false; + if (s1 == s2) + return true; + vector hash(26, 0); + + for (int i = 0; i < s1.size(); i++) + hash.at(s1[i] - 'a')++; + + for (int j = 0; j < s2.size(); j++) + hash.at(s2[j] - 'a')--; + + for (int k = 0; k < 26; k++) + { + if (hash.at(k) != 0) + return false; + } + + for (int i = 1; i < s1.size(); i++) + { + if ( + (isScramble(s1.substr(0, i), s2.substr(0, i)) && isScramble(s1.substr(i, s1.size() - i), s2.substr(i, s1.size() - i))) || (isScramble(s1.substr(0, i), s2.substr(s1.size() - i)) && isScramble(s1.substr(i), s2.substr(0, s1.size() - i)))) + return true; + } + return false; + } +}; ``` ### C ```cpp - +class Solution +{ +public: + bool isScramble(string s1, string s2) + { + if (s1.size() != s2.size()) + return false; + if (s1 == s2) + return true; + string str1 = s1, str2 = s2; + sort(str1.begin(), str1.end()); + sort(str2.begin(), str2.end()); + if (str1 != str2) + return false; + for (int i = 1; i < s1.size(); ++i) + { + string s11 = s1.substr(0, i); + string s12 = s1.substr(i); + string s21 = s2.substr(0, i); + string s22 = s2.substr(i); + if (isScramble(s11, s21) && isScramble(s12, s22)) + return true; + s21 = s2.substr(s2.size() - i); + s22 = s2.substr(0, s2.size() - i); + if (isScramble(s11, s21) && isScramble(s12, s22)) + return true; + } + return false; + } +}; ``` -- GitLab