总的链接 :
面试经典 150 题 - 学习计划 - 力扣(LeetCode)全球极客挚爱的技术成长平台
108 . 将有序数组转换成二叉搜索树 :
链接 :
思路 :
选择中间位置左边的数字作为根节点,然后递归调用该方法,遍历顺序采用中序遍历 :
代码 : (java)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
TreeNode f(int[] nums,int l,int r){
if(l>r) return null ;
int mid = l + r >> 1 ;
TreeNode root = new TreeNode(nums[mid]) ;
root.left = f(nums,l,mid-1);
root.right = f(nums,mid+1,r);
return root ;
}
public TreeNode sortedArrayToBST(int[] nums) {
int n = nums.length ;
TreeNode ans = f(nums,0,n-1);
return ans ;
}
}
148 . 排序链表 :
归并排序 :
class Solution {
public ListNode sortList(ListNode head) {
return sortList(head, null);
}
public ListNode sortList(ListNode head, ListNode tail) {
if (head == null) {
return head;
}
if (head.next == tail) {
head.next = null;
return head;
}
ListNode slow = head, fast = head;
while (fast != tail) {
slow = slow.next;
fast = fast.next;
if (fast != tail) {
fast = fast.next;
}
}
ListNode mid = slow;
ListNode list1 = sortList(head, mid);
ListNode list2 = sortList(mid, tail);
ListNode sorted = merge(list1, list2);
return sorted;
}
public ListNode merge(ListNode head1, ListNode head2) {
ListNode dummyHead = new ListNode(0);
ListNode temp = dummyHead, temp1 = head1, temp2 = head2;
while (temp1 != null && temp2 != null) {
if (temp1.val <= temp2.val) {
temp.next = temp1;
temp1 = temp1.next;
} else {
temp.next = temp2;
temp2 = temp2.next;
}
temp = temp.next;
}
if (temp1 != null) {
temp.next = temp1;
} else if (temp2 != null) {
temp.next = temp2;
}
return dummyHead.next;
}
}
427 . 建立四叉树
题意 : 矩阵代表四叉树,正方形区域的值全相同的时候,那么这个区域代表一个叶节点,否则不是叶节点,它的叶节点就是四分对应矩阵区域所代表的的子树 ;
用递归+前缀和优化 :
详细请看代码 :
class Solution {
public:
Node* construct(vector<vector<int>>& grid) {
int n = grid.size();
vector<vector<int>> sums(n + 1, vector<int>(n + 1));
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
sums[i][j] = sums[i - 1][j] + sums[i][j - 1] - sums[i - 1][j - 1] + grid[i - 1][j - 1];//前缀和
function<Node*(int, int, int, int)> dfs = [&](int ax, int ay, int bx, int by) {
auto sum = sums[bx][by] - sums[bx][ay] - sums[ax][by] + sums[ax][ay];
int mx = (ax + bx) >> 1, my = (ay + by) >> 1; // 找到中间值
if(sum == (bx - ax) * (by - ay)) // 判断是否全为 1
return new Node(true, true); // 是一个叶节点 , 并且结束递归
if(sum) return new Node(true, false, dfs(ax, ay, mx, my), dfs(ax, my, mx, by), dfs(mx, ay, bx, my), dfs(mx, my, bx, by));
return new Node(false, true);//全0的情况
};
// dfs(r0,c0,r1,c1)代表处理从r0到r1-1行,从c0到c1-1列的部分
return dfs(0, 0, n, n);
}
};
23 . 合并k个升序链表
分治合并 :
先将每两个链表进行合并 , 完成之后得到k/2个链表,然后继续下去,最后就能够得到一个排好序的合并链表;
/**
* Definition for singly-linked list.
* 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* mergeTwoLists(ListNode *a, ListNode *b) {
if ((!a) || (!b)) return a ? a : b;
ListNode head, *tail = &head, *aPtr = a, *bPtr = b;
while (aPtr && bPtr) {
if (aPtr->val < bPtr->val) {
tail->next = aPtr; aPtr = aPtr->next;
} else {
tail->next = bPtr; bPtr = bPtr->next;
}
tail = tail->next;
}
tail->next = (aPtr ? aPtr : bPtr);
return head.next;
}
ListNode* mergeKLists(vector<ListNode*>& lists) {
ListNode *ans = nullptr;
for (size_t i = 0; i < lists.size(); ++i) {
ans = mergeTwoLists(ans, lists[i]);
}
return ans;
}
};