随笔分类 -  leetcode-midium

摘要:给定一个非负整数数组,你最初位于数组的第一个位置。 数组中的每个元素代表你在该位置可以跳跃的最大长度。 你的目标是使用最少的跳跃次数到达数组的最后一个位置。 示例: 输入: [2,3,1,1,4]输出: 2解释: 跳到最后一个位置的最小跳跃数是 2。 从下标为 0 跳到下标为 1 的位置,跳 1 步 阅读全文
posted @ 2019-09-05 11:47 yocichen 阅读(240) 评论(0) 推荐(0)
摘要:“学而不思则惘” 题面:给定一个排序好的数组,两个整数 k 和 x,从数组中找到最靠近 x(两数之差最小)的 k 个数。返回的结果必须要是按升序排好的。如果有两个数与 x 的差值一样,优先选择数值较小的那个数。 示例 1: 输入: [1,2,3,4,5], k=4, x=3输出: [1,2,3,4] 阅读全文
posted @ 2019-09-04 19:44 yocichen 阅读(494) 评论(0) 推荐(0)
摘要:给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。 最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。” 例如,给定如下二叉搜索树: root = [6,2,8,0 阅读全文
posted @ 2019-08-25 23:45 yocichen 阅读(810) 评论(0) 推荐(0)
摘要:一道关于骑士救公主故事的题目。 一些恶魔抓住了公主(P)并将她关在了地下城的右下角。地下城是由 M x N 个房间组成的二维网格。我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下城并通过对抗恶魔来拯救公主。 骑士的初始健康点数为一个正整数。如果他的健康点数在某一时刻降至 0 或以下,他 阅读全文
posted @ 2019-06-29 16:39 yocichen 阅读(346) 评论(0) 推荐(0)
摘要:题意懒得抄了,大概是:在升序数组中给定整数target,找到第一个和最后一个target的索引,找到返回{index1, index2},否则返回{-1, -1}; 时间复杂度要求:O(logn) 分析:要求对数时间,又是查找,我们不难想到二分查找。但是有一点,怎么查到第一个和最后一个呢?这困扰了我 阅读全文
posted @ 2019-06-28 17:56 yocichen 阅读(182) 评论(0) 推荐(0)
摘要:题面 这个题面挺简单的,不难理解。给定非负数组,每一个元素都可以看作是一个格子。其中每一个元素值都代表当前可跳跃的格子数,判断是否可以到达最后的格子。 样例 算法 只要存在一条路径可以到达最后就说明可以。我们可以从后往前看,只要前面存在元素索引加上其元素值大于目标元素索引值,就代表从前面格子可以跳到 阅读全文
posted @ 2019-06-02 15:51 yocichen 阅读(175) 评论(0) 推荐(0)
摘要:题面 给定数组,将红-0、白-1、蓝-2,原地排序,要求相同颜色在一起。 样例 算法(初级/垃圾) 遍历数组,统计0、1、2个数,在重新写入数组中。 O(n) 源码 阅读全文
posted @ 2019-05-31 23:41 yocichen 阅读(162) 评论(0) 推荐(0)
摘要:如题,不用乘除法和mod实现两数相除。 这里引用一位clever boy 的解法。 数学大法好!!! 阅读全文
posted @ 2019-05-24 23:03 yocichen 阅读(112) 评论(0) 推荐(0)
摘要:题面 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). 层序遍历二叉树,要求从上到下,从左到右,输出结果为二维ve 阅读全文
posted @ 2019-05-22 16:12 yocichen 阅读(274) 评论(0) 推荐(0)
摘要:题面 原题挺长的,还是英文,就不抄了,😄。 给定字符串,可能由若干个空格开头,之后可能会跟一串数组,数字可能由-/+开头,之后会跟有若干其他字母,找出并计算该数字并返回。 Note: 超出INT_MAX 和 INT_MIN 返回它俩。(所以考虑用更大类型来暂存中间结果。) 样例 Example 1 阅读全文
posted @ 2019-05-20 23:07 yocichen 阅读(202) 评论(0) 推荐(0)
摘要:题面 A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at an 阅读全文
posted @ 2019-05-19 18:04 yocichen 阅读(227) 评论(0) 推荐(0)
摘要:题面 A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at an 阅读全文
posted @ 2019-05-19 16:06 yocichen 阅读(230) 评论(0) 推荐(0)
摘要:题面 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its pat 阅读全文
posted @ 2019-05-19 00:00 yocichen 阅读(207) 评论(0) 推荐(0)
摘要:题面 给定x, n, 求pow(x, n)。 样例 Example 1: Example 2: Example 3: Note: -100.0 < x < 100.0 n is a 32-bit signed integer, within the range [−231, 231 − 1] 要小心 阅读全文
posted @ 2019-05-17 10:27 yocichen 阅读(173) 评论(0) 推荐(0)
摘要:题面 Given a linked list, swap every two adjacent nodes and return its head. You may not modify the values in the list's nodes, only nodes itself may be 阅读全文
posted @ 2019-05-16 21:42 yocichen 阅读(233) 评论(0) 推荐(0)
摘要:题面 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses 给定int n,代表n组括号,编码生成所有有效的括号组合(即符合括号嵌套规则) 样例 g 阅读全文
posted @ 2019-05-16 11:58 yocichen 阅读(218) 评论(0) 推荐(0)
摘要:题面 删除倒数第n个节点,可以保证给定的n有效。 样例 Note: Given n will always be valid. 思路 这个题,挺经典的题目。我们使用快慢指针解决,即:一个指针先行n, 之后另一个指针跟它一起走,直到后面的指针到链表尾部,结束,返回结果。 分析样例 n = 2 算法 1 阅读全文
posted @ 2019-05-16 10:33 yocichen 阅读(143) 评论(0) 推荐(0)
摘要:题面 Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the 阅读全文
posted @ 2019-05-14 16:06 yocichen 阅读(129) 评论(0) 推荐(0)
摘要:题面 Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. A mapping of digit to 阅读全文
posted @ 2019-05-14 15:13 yocichen 阅读(177) 评论(0) 推荐(0)
摘要:题面 Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b+ c + d = target? Find all unique 阅读全文
posted @ 2019-05-13 21:52 yocichen 阅读(169) 评论(0) 推荐(0)