|
1 | 1 | Ctrl+Shift+P(MacOS:cmd+shift+p)呼出命令面板,输入Markdown Preview Enhanced: Create Toc会生成一段类似,保存生成目录。
|
2 | 2 |
|
3 | 3 |
|
| 4 | +<!-- @import "[TOC]" {cmd="toc" depthFrom=1 depthTo=6 orderedList=false} --> |
| 5 | + |
| 6 | +<!-- code_chunk_output --> |
| 7 | + |
| 8 | +- [翻转链表](#翻转链表) |
| 9 | +- [实现二叉树先序,中序和后序遍历](#实现二叉树先序中序和后序遍历) |
| 10 | +- [设计LRU缓存结构](#设计lru缓存结构) |
| 11 | +- [两个链表的第一个公共结点](#两个链表的第一个公共结点) |
| 12 | +- [求平方根](#求平方根) |
| 13 | +- [寻找第K大](#寻找第k大) |
| 14 | +- [判断链表中是否有环](#判断链表中是否有环) |
| 15 | +- [合并有序链表](#合并有序链表) |
| 16 | +- [合并k个已排序的链表](#合并k个已排序的链表) |
| 17 | +- [数组中相加和为0的三元组](#数组中相加和为0的三元组) |
| 18 | +- [删除链表的倒数第n个节点](#删除链表的倒数第n个节点) |
| 19 | +- [二分查找](#二分查找) |
| 20 | +- [两个链表生成相加链表](#两个链表生成相加链表) |
| 21 | +- [二叉树的之字形层序遍历](#二叉树的之字形层序遍历) |
| 22 | +- [链表内指定区间反转](#链表内指定区间反转) |
| 23 | +- [二叉树的镜像](#二叉树的镜像) |
| 24 | +- [数组中只出现一次的数字](#数组中只出现一次的数字) |
| 25 | +- [最长的括号子串](#最长的括号子串) |
| 26 | +- [把二叉树打印成多行](#把二叉树打印成多行) |
| 27 | +- [合并两个有序的数组](#合并两个有序的数组) |
| 28 | +- [二叉树的最大路径和](#二叉树的最大路径和) |
| 29 | +- [买卖股票的最佳时机](#买卖股票的最佳时机) |
| 30 | +- [二叉树中是否存在节点和为指定值的路径](#二叉树中是否存在节点和为指定值的路径) |
| 31 | +- [设计getMin功能的栈](#设计getmin功能的栈) |
| 32 | +- [LFU缓存结构设计](#lfu缓存结构设计) |
| 33 | +- [N皇后问题](#n皇后问题) |
| 34 | +- [带权值的最小路径和](#带权值的最小路径和) |
| 35 | +- [反转数字](#反转数字) |
| 36 | +- [二叉搜索树的第k个结点](#二叉搜索树的第k个结点) |
| 37 | +- [子数组最大乘积](#子数组最大乘积) |
| 38 | +- [最长递增子序列](#最长递增子序列) |
| 39 | +- [在两个长度相等的排序数组中找到上中位数](#在两个长度相等的排序数组中找到上中位数) |
| 40 | +- [判断t1树中是否有与t2树拓扑结构完全相同的子树](#判断t1树中是否有与t2树拓扑结构完全相同的子树) |
| 41 | +- [反转字符串](#反转字符串) |
| 42 | +- [最大正方形](#最大正方形) |
| 43 | + |
| 44 | +<!-- /code_chunk_output --> |
| 45 | + |
| 46 | + |
| 47 | + |
4 | 48 | ### 翻转链表
|
5 | 49 |
|
6 | 50 | ```java
|
@@ -1386,44 +1430,188 @@ public class Solution {
|
1386 | 1430 | }
|
1387 | 1431 | ```
|
1388 | 1432 |
|
1389 |
| -### |
1390 |
| - |
1391 |
| -```java |
1392 |
| - |
1393 |
| -``` |
1394 |
| - |
1395 |
| -### |
| 1433 | +### 最长递增子序列 |
1396 | 1434 |
|
1397 | 1435 | ```java
|
| 1436 | +public int[] LIS (int[] arr) { |
| 1437 | + // write code here |
| 1438 | + if(arr == null || arr.length <= 0){ |
| 1439 | + return null; |
| 1440 | + } |
1398 | 1441 |
|
1399 |
| -``` |
1400 |
| - |
1401 |
| -### |
1402 |
| - |
1403 |
| -```java |
| 1442 | + int len = arr.length; |
| 1443 | + int[] count = new int[len]; // 存长度 |
| 1444 | + int[] end = new int[len]; // 存最长递增子序列 |
| 1445 | + |
| 1446 | + //init |
| 1447 | + int index = 0; // end 数组下标 |
| 1448 | + end[index] = arr[0]; |
| 1449 | + count[0] = 1; |
| 1450 | + |
| 1451 | + for(int i = 0; i < len; i++){ |
| 1452 | + if(end[index] < arr[i]){ |
| 1453 | + end[++index] = arr[i]; |
| 1454 | + count[i] = index; |
| 1455 | + } |
| 1456 | + else{ |
| 1457 | + int left = 0, right = index; |
| 1458 | + while(left <= right){ |
| 1459 | + int mid = (left + right) >> 1; |
| 1460 | + if(end[mid] >= arr[i]){ |
| 1461 | + right = mid - 1; |
| 1462 | + } |
| 1463 | + else{ |
| 1464 | + left = mid + 1; |
| 1465 | + } |
| 1466 | + } |
| 1467 | + end[left] = arr[i]; |
| 1468 | + count[i] = left; |
| 1469 | + } |
| 1470 | + } |
1404 | 1471 |
|
| 1472 | + //因为返回的数组要求是字典序,所以从后向前遍历 |
| 1473 | + int[] res = new int[index + 1]; |
| 1474 | + for(int i = len - 1; i >= 0; i--){ |
| 1475 | + if(count[i] == index){ |
| 1476 | + res[index--] = arr[i]; |
| 1477 | + } |
| 1478 | + } |
| 1479 | + return res; |
| 1480 | +} |
1405 | 1481 | ```
|
1406 | 1482 |
|
1407 |
| -### |
| 1483 | +### 在两个长度相等的排序数组中找到上中位数 |
1408 | 1484 |
|
1409 | 1485 | ```java
|
1410 |
| - |
| 1486 | +public int findMedianinTwoSortedAray (int[] arr1, int[] arr2) { |
| 1487 | + // write code here |
| 1488 | + int n = arr1.length; |
| 1489 | + if(n==0){ |
| 1490 | + return 0; |
| 1491 | + } |
| 1492 | + //arr1左右两端 |
| 1493 | + int l1=0,r1=n-1; |
| 1494 | + //arr2左右两端 |
| 1495 | + int l2=0,r2=n-1; |
| 1496 | + int mid1,mid2; |
| 1497 | + //终止条件为l1=r1,即两个数组都只有一个元素,此时的上中位数为两数的最小值 |
| 1498 | + while(l1< r1){ |
| 1499 | + //arr1中位数 |
| 1500 | + mid1 = l1+((r1-l1)>>1); |
| 1501 | + //arr2中位数 |
| 1502 | + mid2 = l2+((r2-l2)>>1); |
| 1503 | + int k = r1-l1+1; |
| 1504 | + if(arr1[mid1] == arr2[mid2]){ //若两数组中位数相等,整体中位数也是这个 |
| 1505 | + return arr1[mid1]; |
| 1506 | + } |
| 1507 | + else if(arr1[mid1] > arr2[mid2]){ |
| 1508 | + if(k%2 == 0){//区间元素个数为偶数 |
| 1509 | + r1 = mid1; //整体中位数在arr1左区间,包括mid1 |
| 1510 | + l2 = mid2+1; //整体中位数在arr2右区间,不包括mid2 |
| 1511 | + } |
| 1512 | + else if(k%2 == 1){ //区间元素个数为奇数 |
| 1513 | + r1 = mid1; //整体中位数在arr1左区间,包括mid1 |
| 1514 | + l2 = mid2; //整体中位数在arr2右区间,包括mid2 |
| 1515 | + } |
| 1516 | + } |
| 1517 | + else if (arr1[mid1] < arr2[mid2]){ |
| 1518 | + if(k%2 == 0){//区间元素个数为偶数 |
| 1519 | + r2 = mid2; //整体中位数在arr2左区间,包括mid2 |
| 1520 | + l1 = mid1+1; //整体中位数在arr1右区间,不包括mid1 |
| 1521 | + } |
| 1522 | + else if(k%2 == 1){ //区间元素个数为奇数 |
| 1523 | + r2 = mid2; //整体中位数在arr2左区间,包括mid2 |
| 1524 | + l1 = mid1; //整体中位数在arr1右区间,包括mid1 |
| 1525 | + } |
| 1526 | + } |
| 1527 | + } |
| 1528 | + //当区间内只有一个元素时,两个区间中最小值即为整体中位数 |
| 1529 | + return Math.min(arr1[l1],arr2[l2]); |
| 1530 | +} |
1411 | 1531 | ```
|
1412 | 1532 |
|
1413 |
| -### |
| 1533 | +### 判断t1树中是否有与t2树拓扑结构完全相同的子树 |
1414 | 1534 |
|
1415 | 1535 | ```java
|
| 1536 | +/** |
| 1537 | + * |
| 1538 | + * @param root1 TreeNode类 |
| 1539 | + * @param root2 TreeNode类 |
| 1540 | + * @return bool布尔型 |
| 1541 | + */ |
| 1542 | +public boolean isContains (TreeNode root1, TreeNode root2) { |
| 1543 | + // write code here |
| 1544 | + if(root1 == null || root2 == null){ |
| 1545 | + return false; |
| 1546 | + } |
| 1547 | + return recur(root1, root2) || isContains(root1.left,root2) || isContains(root1.right,root2); |
| 1548 | +} |
1416 | 1549 |
|
| 1550 | +public boolean recur(TreeNode root1, TreeNode root2){ |
| 1551 | + if(root2 == null){ |
| 1552 | + return true; |
| 1553 | + } |
| 1554 | + if(root1 == null || root1.val != root2.val){ |
| 1555 | + return false; |
| 1556 | + } |
| 1557 | + return recur(root1.left,root2.left) && recur(root1.right,root2.right); |
| 1558 | +} |
1417 | 1559 | ```
|
1418 | 1560 |
|
1419 |
| -### |
| 1561 | +### 反转字符串 |
1420 | 1562 |
|
1421 | 1563 | ```java
|
1422 |
| - |
| 1564 | +/** |
| 1565 | +* 反转字符串 |
| 1566 | +* @param str string字符串 |
| 1567 | +* @return string字符串 |
| 1568 | +*/ |
| 1569 | +public String solve (String str) { |
| 1570 | + // write code here |
| 1571 | + if(str == null){ |
| 1572 | + return null; |
| 1573 | + } |
| 1574 | + char[] c = new char[str.length()]; |
| 1575 | + int left = 0, right = str.length() - 1; |
| 1576 | + |
| 1577 | + while(left <= right){ |
| 1578 | + c[left] = str.charAt(right); |
| 1579 | + c[right] = str.charAt(left); |
| 1580 | + left++; |
| 1581 | + right--; |
| 1582 | + } |
| 1583 | + return new String(c); |
| 1584 | +} |
1423 | 1585 | ```
|
1424 | 1586 |
|
1425 |
| -### |
| 1587 | +### 最大正方形 |
1426 | 1588 |
|
1427 | 1589 | ```java
|
1428 |
| - |
| 1590 | +/** |
| 1591 | +* 最大正方形 |
| 1592 | +* @param matrix char字符型二维数组 |
| 1593 | +* @return int整型 |
| 1594 | +*/ |
| 1595 | +public int solve (char[][] matrix) { |
| 1596 | + // write code here |
| 1597 | + int m = matrix.length; |
| 1598 | + int n = matrix[0].length; |
| 1599 | + int dp[][] = new int [m][n]; |
| 1600 | + for(int i = 0; i < m; i++){ |
| 1601 | + dp[i][0] = matrix[i][0] - '0'; |
| 1602 | + } |
| 1603 | + for(int j = 0; j < n; j++){ |
| 1604 | + dp[0][j] = matrix[0][j] - '0'; |
| 1605 | + } |
| 1606 | + int max = 0; |
| 1607 | + for(int i = 1; i < m; i++){ |
| 1608 | + for(int j = 1; j < n; j++){ |
| 1609 | + if(matrix[i][j] == '1'){ |
| 1610 | + dp[i][j] = Math.min(Math.min(dp[i-1][j-1],dp[i][j-1]),dp[i-1][j]) + 1; |
| 1611 | + max = Math.max(max,dp[i][j]); |
| 1612 | + } |
| 1613 | + } |
| 1614 | + } |
| 1615 | + return max*max; |
| 1616 | +} |
1429 | 1617 | ```
|
0 commit comments