LeetCode 69. x 的平方根
实现 int sqrt(int x) 函数。
计算并返回 x 的平方根,其中 x 是非负整数。
由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。
示例 1:
输入: 4
输出: 2
示例 2:
输入: 8
输出: 2
说明: 8 的平方根是 2.82842...,
由于返回类型是整数,小数部分将被舍去。
Coding:
public class Sqrt {
package com.leetcode.two;
import java.util.Scanner;
/**
* @Auther: Jibny Zhan
* @Date: 2019/12/3 20:56
* @Description:
*/
public class Sqrt {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
System.out.println(mySqrt2(input));
}
//暴力
private static int mySqrt(int x) {
if (x <= 1) return x;
for (int i = 2; i <= x; i++)
if (i > x / i)
return i - 1;
return -1;
}
//二分法
private static int mySqrt1(int x) {
if (x <= 1) return x;
int left = 0, right = x;
while (left <= right) {
int mid = (right + left) >>> 1;
if (mid > x / mid)
right = mid - 1;
else
left = mid + 1;
}
return right;
}
//牛顿迭代法
public static int mySqrt2(int x) {
int res = x;
while (res > x / res) {
res = (res + x / res) / 2;
}
return res;
}
}
}
LeetCode 109. 有序链表转换二叉搜索树
给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树。
本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。
示例:
给定的有序链表: [-10, -3, 0, 5, 9],
一个可能的答案是:[0, -3, 9, -10, null, 5],
它可以表示下面这个高度平衡二叉搜索树:
0
/
-3 9
/ /
-10 5
三种解法
- 二叉树中序遍历数组有序
- 链表快慢指针
- 数组二分递归
Coding:
package com.leetcode;
import java.util.ArrayList;
import java.util.List;
/**
* @Auther: Jibny Zhan
* @Date: 2019/12/3 21:18
* @Description:
*/
public class SortedListToBST {
private static ListNode pNode;
public static void main(String[] args) {
ListNode head = new ListNode(0);
initListNode(head);
// TreeNode treeNode = sortedArrayToBST(head);
// TreeNode treeNode = sortedListToBST(head);
TreeNode treeNode = sortedTreeToBST(head);
prePrintTree(treeNode);
System.out.println();
midPrintTree(treeNode);
}
//中序遍历二叉树得到的就是从小到大的有序数组
private static TreeNode sortedTreeToBST(ListNode head) {
int size = findSize(head);
pNode = head;
return sortedTreeToBST(0, size - 1);
}
private static int findSize(ListNode head) {
int size = 0;
while (head != null) {
head = head.next;
size++;
}
return size;
}
private static TreeNode sortedTreeToBST(int start, int end) {
if (start > end) return null;
int mid = start + ((end - start) >> 1);
//划分左右子树
TreeNode left = sortedTreeToBST(start, mid - 1);
TreeNode treeNode = new TreeNode(pNode.val);
treeNode.left = left;
pNode = pNode.next;
TreeNode right = sortedTreeToBST(mid+1,end);
treeNode.right = right;
return treeNode;
}
//快慢指针
private static TreeNode sortedListToBST(ListNode head) {
return sortedListToBST(head, null);
}
private static TreeNode sortedListToBST(ListNode head, ListNode tail) {
if (head == null || head == tail)
return null;
ListNode slow = head;
ListNode fast = head;
while (fast.next != tail && fast.next.next != tail) {
fast = fast.next.next;
slow = slow.next;
}
TreeNode treeNode = new TreeNode(slow.val);//根节点
treeNode.left = sortedListToBST(head, slow);
treeNode.right = sortedListToBST(slow.next, tail);
return treeNode;
}
//转成数组,数组二分,递归构建中心节点
private static TreeNode sortedArrayToBST(ListNode head) {
List<Integer> arrayList = new ArrayList<>();
ListNode p = head;
while (p != null) {
arrayList.add(p.val);
p = p.next;
}
return sortedArrayToBST(arrayList, 0, arrayList.size());
}
private static TreeNode sortedArrayToBST(List<Integer> arr, int left, int right) {
if (left == right) return null;
int mid = (left + right) >>> 1;
TreeNode treeNode = new TreeNode(arr.get(mid));
treeNode.left = sortedArrayToBST(arr, left, mid);
treeNode.right = sortedArrayToBST(arr, mid + 1, right);
return treeNode;
}
//初始化
public static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
//前序打印
public static void prePrintTree(TreeNode binTree) {
if (binTree != null) {
System.out.println(binTree.val);
prePrintTree(binTree.left);
prePrintTree(binTree.right);
}
}
//中序打印
public static void midPrintTree(TreeNode binTree) {
if (binTree != null) {
prePrintTree(binTree.left);
System.out.println(binTree.val);
prePrintTree(binTree.right);
}
}
public static ListNode initListNode(ListNode head) {
ListNode p = head;
p.val = -10;
p.next = new ListNode(-3);
p = p.next;
p.next = new ListNode(0);
p = p.next;
p.next = new ListNode(5);
p = p.next;
p.next = new ListNode(9);
p = p.next;
p.next = null;
return head;
}
}