随笔分类 - Binary Search
摘要:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. N
阅读全文
摘要:Given an array where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is d
阅读全文
摘要:[抄题]: [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: [思维问题]: 总算自己写出一道题来了。 [英文数据结构或算法,为什么不用别的数据结构或算法]: [一句话思路]: 总有case过不了,可以稍微调试一下,起
阅读全文
摘要:[抄题]: Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an
阅读全文
摘要:[抄题]: Given an array of citations in ascending order (each citation is a non-negative integer) of a researcher, write a function to compute the resear
阅读全文
摘要:[抄题]: Given a list of sorted characters letters containing only lowercase letters, and given a target letter target, find the smallest element in the
阅读全文
摘要:[抄题]: Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library f
阅读全文
摘要:[抄题]: Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a non-negative integer. Since the return type is
阅读全文
摘要:一句话思路:从左边开始的三步翻转法 一刷报错: 风格上: 总结:start.end是位置随机定义的,什么地方都有可能是start.end 用到的English数据结构,trick or not:array arraylist(因为无序就没用二分法) 时间复杂度n:最多每个数挪一次,空间复杂度1:数组
阅读全文
摘要:一句话思路:反正只是寻找一个最小区间,断开也能二分。根据m第一次的落点,来分情况讨论。 一刷报错: 一句话总结思路:要把while放在外面,if放在里面。 二刷ac public class Solution { /* * @param nums: an integer rotated sorted
阅读全文
摘要:跟进“搜索旋转排序数组”,假如有重复元素又将如何? 一句话思路:不能二分,因为复杂度是n eg全是0,找一个1 class Solution { public boolean search(int[] nums, int target) { int i; if (nums.length == 0 |
阅读全文
摘要:一句话思路:找最大的元素。没有target? 一刷报错: 总结:用二分法往大的一边走,缩小到最后的小区间,比较一下两头就行了。 class Solution { /** * @param A: An integers array. * @return: return any of peek posi
阅读全文
摘要:一句话思路:从左下角开始找。复杂度是o(m+n)。 一刷报错: 错误的原因在于:没有意识到这个矩阵可以是不严格单调递增的,在最左一列、最下一行找到后,继续朝右上方找,还有可能找到。 一句话总结:注意不严格单调递增的特点,在边界线上找到后还要继续找。而且还要注意不要数组越界。(有0时写小于号)
阅读全文
摘要:一句话思路:找数字变成了找所谓的bad version,换了个名字而已。挺没有思考含量的一道题,其实。随便看看就行吧。 一刷报错: /* The isBadVersion API is defined in the parent class VersionControl. boolean isBad
阅读全文
摘要:给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。 你可以假设在数组中无重复元素。 一句话思路:找到第一个大于等于target的数。问题:以为要改模板,不会。 思路真身:不用改模板。模板最后得出的是一个最小的范围了 只有两个数,对这个范围的小线
阅读全文
摘要:一句话思路:新建数组存储位置,输出数组 一刷报错: 二刷改了小格式之后ac: 总结:bound[0],是从前往后查找的,从前开始查。 bound[1],是从后往前查找的,从后开始查。 风格上: intb;
阅读全文
摘要:[一刷]报错了无数遍 一句话思路:对有序整数进行二分查找 问题: 格式: 总结:要分清楚角标和数字的关系,先定义角标,最后返回的也是角标 public class Solution { /* * @param nums: An integer array sorted in ascending or
阅读全文