|
32 | 32 | ## Linked List |
33 | 33 | 1. Reverse Linked List II Medium |
34 | 34 | 2. Merge K sorted List Medium |
| 35 | +3. Reorder List Medium |
| 36 | +4. Add two Numbers Medium |
| 37 | +5. Add two number II Medium |
| 38 | +6. Linked List Insertion Sort Medium |
| 39 | +7. Swap Nodes in Pairs Medium |
35 | 40 |
|
36 | 41 | ## String |
37 | 42 | 1. Valid Anagram Easy |
| 43 | +2. Integer to English Words Hard |
38 | 44 |
|
39 | 45 |
|
40 | 46 | ## Implementation |
@@ -1213,4 +1219,257 @@ public: |
1213 | 1219 | return true; |
1214 | 1220 | } |
1215 | 1221 | }; |
| 1222 | +``` |
| 1223 | + |
| 1224 | +### **Integer to English Words** |
| 1225 | + |
| 1226 | +***Big O:*** O(N) speed, O(1) space |
| 1227 | +``` |
| 1228 | +Tips: |
| 1229 | +
|
| 1230 | +Divide and conquer. Divide it into thousands and conquer it one by one. |
| 1231 | +``` |
| 1232 | +```c++ |
| 1233 | +class Solution { |
| 1234 | +public: |
| 1235 | + string thousandToWords(int num) { |
| 1236 | + string ret = ""; |
| 1237 | + if (num >= 100) { |
| 1238 | + ret += digits[num/100] + "Hundred "; |
| 1239 | + num %= 100; |
| 1240 | + } |
| 1241 | + if (num == 0) return ret; |
| 1242 | + |
| 1243 | + ret += (num < 20) ? digits[num] : tens[num/10] + digits[num%10]; |
| 1244 | + return ret; |
| 1245 | + } |
| 1246 | + |
| 1247 | + string numberToWords(int num) { |
| 1248 | + string ans = ""; |
| 1249 | + |
| 1250 | + if (num == 0) |
| 1251 | + return "Zero"; |
| 1252 | + |
| 1253 | + for (int i = 0; i < 4; i++) { |
| 1254 | + if (num >= units[i].first) { |
| 1255 | + ans += thousandToWords(num/units[i].first) + units[i].second; |
| 1256 | + num %= units[i].first; |
| 1257 | + } |
| 1258 | + } |
| 1259 | + ans += thousandToWords(num); |
| 1260 | + if (ans.size() > 0) |
| 1261 | + ans.pop_back(); |
| 1262 | + |
| 1263 | + return ans; |
| 1264 | + } |
| 1265 | + |
| 1266 | +private: |
| 1267 | + string digits[20] = {"", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ", "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen "}; |
| 1268 | + string tens[10] = {"", "Ten ", "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "}; |
| 1269 | + pair<int, string> units[4] = {{1000000000, "Billion "}, {1000000, "Million "}, {1000, "Thousand "}, {100, "Hundred "}}; |
| 1270 | +}; |
| 1271 | +``` |
| 1272 | +### **Reorder list** |
| 1273 | +
|
| 1274 | +***Big O:*** O(N) speed, O(1) space |
| 1275 | +``` |
| 1276 | +Tips: |
| 1277 | + |
| 1278 | +Find mid node, reverse, and merge. |
| 1279 | +``` |
| 1280 | +```c++ |
| 1281 | +class Solution { |
| 1282 | +public: |
| 1283 | + ListNode *reverseNode (ListNode* head) { |
| 1284 | + ListNode *pre, *cur; |
| 1285 | + |
| 1286 | + pre = nullptr; |
| 1287 | + cur = head; |
| 1288 | + while (cur) { |
| 1289 | + ListNode *nxt = cur->next; |
| 1290 | + cur->next = pre; |
| 1291 | + pre = cur; |
| 1292 | + cur = nxt; |
| 1293 | + } |
| 1294 | + |
| 1295 | + return pre; |
| 1296 | + } |
| 1297 | + |
| 1298 | + void reorderList(ListNode* head) { |
| 1299 | + if (head == nullptr || head->next == nullptr) |
| 1300 | + return; |
| 1301 | + |
| 1302 | + ListNode *slow, *fast; |
| 1303 | + slow = fast = head; |
| 1304 | + |
| 1305 | + while (fast && fast->next) { |
| 1306 | + slow = slow->next; |
| 1307 | + fast = fast->next->next; |
| 1308 | + } |
| 1309 | + ListNode *tail = reverseNode(slow); |
| 1310 | + ListNode *first = head; |
| 1311 | + |
| 1312 | + while(tail->next != nullptr) { |
| 1313 | + ListNode *tmp = first->next; |
| 1314 | + first->next = tail; |
| 1315 | + first = tmp; |
| 1316 | + |
| 1317 | + tmp = tail->next; |
| 1318 | + tail->next = first; |
| 1319 | + tail = tmp; |
| 1320 | + } |
| 1321 | + |
| 1322 | + return; |
| 1323 | + } |
| 1324 | +}; |
| 1325 | +``` |
| 1326 | + |
| 1327 | +### **Add two number** |
| 1328 | + |
| 1329 | +***Big O:*** O(N) speed, O(1) space |
| 1330 | +``` |
| 1331 | +Tips: |
| 1332 | +
|
| 1333 | +Carry ripple adder |
| 1334 | +``` |
| 1335 | +```c++ |
| 1336 | +class Solution { |
| 1337 | +public: |
| 1338 | + ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { |
| 1339 | + int carry = 0; |
| 1340 | + ListNode dummy, *tmp; |
| 1341 | + |
| 1342 | + tmp = &dummy; |
| 1343 | + while (l2 || l1 || carry) { |
| 1344 | + int sum = 0; |
| 1345 | + |
| 1346 | + if (l1 && l2) |
| 1347 | + sum = (carry + l1->val + l2->val); |
| 1348 | + else if (l1 != nullptr || l2 != nullptr) |
| 1349 | + sum = (carry + ((l1) ? l1->val : l2->val)); |
| 1350 | + else |
| 1351 | + sum = carry; |
| 1352 | + |
| 1353 | + carry = sum/10; |
| 1354 | + |
| 1355 | + ListNode *new_node = new ListNode(sum%10, nullptr); |
| 1356 | + tmp -> next = new_node; |
| 1357 | + tmp = tmp->next; |
| 1358 | + l1 = l1 ? l1->next : nullptr; |
| 1359 | + l2 = l2 ? l2->next : nullptr; |
| 1360 | + } |
| 1361 | + |
| 1362 | + return dummy.next; |
| 1363 | + } |
| 1364 | +}; |
| 1365 | +``` |
| 1366 | +
|
| 1367 | +### **Add two number II** |
| 1368 | +
|
| 1369 | +***Big O:*** O(N) speed, O(1) space |
| 1370 | +``` |
| 1371 | +Tips: |
| 1372 | + |
| 1373 | +Approach 1: |
| 1374 | + |
| 1375 | +Reverse each list and then do add two number. |
| 1376 | + |
| 1377 | +Approach 2: |
| 1378 | + |
| 1379 | +Convert the number as string and then calculate from the last position. |
| 1380 | + |
| 1381 | +Or use two stacks. |
| 1382 | +``` |
| 1383 | +```c++ |
| 1384 | +class Solution { |
| 1385 | +public: |
| 1386 | + ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { |
| 1387 | + string a, b; |
| 1388 | + ListNode *result = nullptr; |
| 1389 | + while(l1) { a.push_back(l1->val+'0'); l1 = l1->next;} |
| 1390 | + while(l2) { b.push_back(l2->val+'0'); l2 = l2->next;} |
| 1391 | + int l = a.size()-1, r = b.size()-1, carry = 0; |
| 1392 | + while(l >= 0 || r >= 0 || carry == 1) { |
| 1393 | + int c = (l >= 0 ? a[l--]-'0' : 0) + ( r >= 0 ? b[r--]-'0' : 0) + carry; |
| 1394 | + ListNode *temp = new ListNode(c%10); |
| 1395 | + temp->next = result; |
| 1396 | + result = temp; |
| 1397 | + carry = c/10; |
| 1398 | + } |
| 1399 | + return result; |
| 1400 | + } |
| 1401 | +}; |
| 1402 | +``` |
| 1403 | + |
| 1404 | +### **Linked List Insertion Sort** |
| 1405 | + |
| 1406 | +***Big O:*** O(N) speed, O(1) space |
| 1407 | +``` |
| 1408 | +Tips: |
| 1409 | +
|
| 1410 | +Keep a separate sorted list by creating a sentinal node. Compare and insert nodes into that separate list. |
| 1411 | +
|
| 1412 | +``` |
| 1413 | +```c++ |
| 1414 | +class Solution { |
| 1415 | +public: |
| 1416 | + ListNode * insertionSortList(ListNode * head) { |
| 1417 | + // write your code here |
| 1418 | + ListNode sentinal, *tmp, *tmp_s; |
| 1419 | + sentinal.next = nullptr; |
| 1420 | + |
| 1421 | + tmp = head; |
| 1422 | + while (tmp) { |
| 1423 | + ListNode* next = tmp->next; |
| 1424 | + tmp_s = &sentinal; |
| 1425 | + |
| 1426 | + while (tmp_s->next != nullptr && tmp_s->next->val < tmp->val) { |
| 1427 | + tmp_s = tmp_s->next; |
| 1428 | + } |
| 1429 | + |
| 1430 | + tmp->next = tmp_s->next; |
| 1431 | + tmp_s->next = tmp; |
| 1432 | + tmp = next; |
| 1433 | + } |
| 1434 | + |
| 1435 | + return sentinal.next; |
| 1436 | + } |
| 1437 | +}; |
| 1438 | +``` |
| 1439 | + |
| 1440 | +### **Swap nodes in pair** |
| 1441 | + |
| 1442 | +***Big O:*** O(N) speed, O(1) space |
| 1443 | +``` |
| 1444 | +Tips: |
| 1445 | +
|
| 1446 | +We swap the nodes on the go. After swapping a pair of nodes, say A and B, we need to link the node B to the node that was right before A. To establish this linkage we save the previous node of node A in prevNode. |
| 1447 | +
|
| 1448 | +``` |
| 1449 | +```c++ |
| 1450 | +class Solution { |
| 1451 | +public: |
| 1452 | + ListNode * swapPairs(ListNode * head) { |
| 1453 | + // write your code here |
| 1454 | + if (!head || !head->next) |
| 1455 | + return head; |
| 1456 | + |
| 1457 | + ListNode *l1, *l2, *pre, sentinal; |
| 1458 | + |
| 1459 | + l1 = head; |
| 1460 | + pre = &sentinal; |
| 1461 | + |
| 1462 | + while (l1 && l1->next) { |
| 1463 | + l2 = l1->next; |
| 1464 | + ListNode *l2_next = l2->next; |
| 1465 | + l1->next = l2_next; |
| 1466 | + l2->next = l1; |
| 1467 | + pre->next = l2; |
| 1468 | + pre = l1; |
| 1469 | + l1 = l2_next; |
| 1470 | + } |
| 1471 | + |
| 1472 | + return sentinal.next; |
| 1473 | + } |
| 1474 | +}; |
1216 | 1475 | ``` |
0 commit comments