Fork me on GitHub

随笔分类 -  LeetCode

摘要:/** * 给出n个数字,代表直方图的条高,直方图每一条的宽度为1,请计算直方图中最大矩形的面积 * * 上图是每条宽度为1, 高度 =[2,1,5,6,2,3].的直方图 * * 图中的阴影部分是该直方图中面积最大的矩形,面积为10个单位 * 例如: * 给出的高度 =[2,1,5,6,2,3], * 返回10. */ /** * 给出n个数字,代表直方图的条高,直方图每一条的宽度为1,请计算直 阅读全文
posted @ 2019-08-20 10:14 gentleKay 阅读(189) 评论(0) 推荐(0)
摘要:/** * 给定一个链表,删除链表的倒数第n个节点并返回链表的头指针 * 例如, * 给出的链表为:1->2->3->4->5, n= 2.↵↵ * 删除了链表的倒数第n个节点之后,链表变为1->2->3->5. * 备注: * 题目保证n一定是合法的 * 请尝试只用一步操作完成该功能 */ /** * 给定一个链表,删除链表的倒数第n个节点并返回链表的头指针 * 例如, * ... 阅读全文
posted @ 2019-08-20 10:14 gentleKay 阅读(189) 评论(0) 推荐(0)
摘要:/** * 给出一个非负整数数组,你最初在数组第一个元素的位置 * 数组中的元素代表你在这个位置可以跳跃的最大长度 * 你的目标是用最少的跳跃次数来到达数组的最后一个元素的位置 * 例如 * 给出数组 A =[2,3,1,1,4] * 最少需要两次才能跳跃到数组最后一个元素的位置。 *(从数组下标为 0的位置跳长度1到达下标1的位置,然后跳长度3到数组最后一个元素的位置) */ /** * 给... 阅读全文
posted @ 2019-08-19 11:24 gentleKay 阅读(213) 评论(0) 推荐(0)
摘要:/** *环形路上有n个加油站,第i个加油站的汽油量是gas[i]. * 你有一辆车,车的油箱可以无限装汽油。从加油站i走到下一个加油站(i+1)花费的油量是cost[i],你从一个加油站出发,刚开始的时候油箱里面没有汽油。 * 求从哪个加油站出发可以在环形路上走一圈。返回加油站的下标,如果没有答案的话返回-1。 * 注意: * 答案保证唯一。 * */ /** *环形路上有n个加油站,第i个... 阅读全文
posted @ 2019-08-19 11:23 gentleKay 阅读(271) 评论(0) 推荐(0)
摘要:/** * 给出一棵树的前序遍历和中序遍历,请构造这颗二叉树 * 注意: * 可以假设树中不存在重复的节点 */ /** * 给出一棵树的前序遍历和中序遍历,请构造这颗二叉树 * 注意: * 可以假设树中不存在重复的节点 */ public class Main55 { public static void main(String[] args) { int[] preorde = {1,2,4, 阅读全文
posted @ 2019-08-16 09:54 gentleKay 阅读(104) 评论(0) 推荐(0)
摘要:/** * 给出一个索引k,返回杨辉三角的第k行 * 例如,k=3, * 返回[1,3,3,1]. * 备注: * 你能将你的算法优化到只使用O(k)的额外空间吗? * * Given an index k, return the k th row of the Pascal's triangle. * For example, given k = 3, * Return[1,3,3,1]. * ... 阅读全文
posted @ 2019-08-15 10:19 gentleKay 阅读(213) 评论(0) 推荐(0)
摘要:/** * 给定一个二叉树,返回该二叉树由底层到顶层的层序遍历,(从左向右,从叶子节点到根节点,一层一层的遍历) * 例如: * 给定的二叉树是{3,9,20,#,#,15,7}, * 3↵ / ↵ 9 20↵ / ↵ 15 7 * 该二叉树由底层到顶层层序遍历的结果是 * [↵ [15,7]↵ [9,20],↵ [3],↵] * */ impo... 阅读全文
posted @ 2019-08-15 10:18 gentleKay 阅读(159) 评论(0) 推荐(0)
摘要:/** *编写一个函数来查找字符串数组中的最长公共前缀。 * 如果不存在公共前缀,返回空字符串 ""。 * 说明: 所有输入只包含小写字母 a-z 。 */ /** *编写一个函数来查找字符串数组中的最长公共前缀。 * 如果不存在公共前缀,返回空字符串 ""。 * 说明: 所有输入只包含小写字母 a-z 。 */ public class Main53 { public sta... 阅读全文
posted @ 2019-08-15 10:18 gentleKay 阅读(179) 评论(0) 推荐(0)
摘要:/** * 给定一个由非负整数填充的m x n的二维数组, * 现在要从二维数组的左上角走到右下角, * 请找出路径上的所有数字之和最小的路径。 * 注意:你每次只能向下或向右移动。 * * Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which min... 阅读全文
posted @ 2019-08-14 15:15 gentleKay 阅读(207) 评论(0) 推荐(0)
摘要:/** * 给出一个值numRows,生成杨辉三角的前numRows行 * 例如,给出 numRows = 5, * 返回 * [↵ [1],↵ [1,1],↵ [1,2,1],↵ [1,3,3,1],↵ [1,4,6,4,1]↵] */ import java.util.ArrayList; /** * 给出一个值numRows,生成杨辉三角的前numRows... 阅读全文
posted @ 2019-08-14 15:14 gentleKay 阅读(292) 评论(0) 推荐(0)
摘要:/** * Given a number represented as an array of digits, plus one to the number. * * 给定一个以数字数组表示的数字,再加上一个数字。 */ /** * Given a number represented as an array of digits, plus one to the number. * * 给... 阅读全文
posted @ 2019-08-13 11:20 gentleKay 阅读(240) 评论(0) 推荐(0)
摘要:/** * Follow up for "Remove Duplicates": * What if duplicates are allowed at most twice? * For example, * Given sorted array A =[1,1,1,2,2,3], * Your function should return length =5, and A is now[1,1... 阅读全文
posted @ 2019-08-13 11:08 gentleKay 阅读(117) 评论(0) 推荐(0)
摘要:/** * Follow up for "Search in Rotated Sorted Array": * What if duplicates are allowed? * Would this affect the run-time complexity? How and why? * Write a function to determine if a given target is i... 阅读全文
posted @ 2019-08-13 11:07 gentleKay 阅读(217) 评论(0) 推荐(0)
摘要:/** * Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. * For example, * Given n =3, * You should return the following matrix: * [ * [ 1, 2, 3 ], * [ 8, 阅读全文
posted @ 2019-08-12 10:49 gentleKay 阅读(140) 评论(0) 推荐(0)
摘要:/** * Follow up for problem "Populating Next Right Pointers in Each Node". * What if the given tree could be any binary tree? Would your previous solution still work? * Note: * You may only use consta 阅读全文
posted @ 2019-08-12 10:48 gentleKay 阅读(125) 评论(0) 推荐(0)
摘要:/** * Suppose a sorted array is rotated at some pivot unknown to you beforehand. * (i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2). * You are given a target value to search. If found in the array return 阅读全文
posted @ 2019-08-12 10:47 gentleKay 阅读(167) 评论(0) 推荐(0)
摘要:/** * Given two sorted integer arrays A and B, merge B into A as one sorted array. * Note: * You may assume that A has enough space to hold additional elements from B. * The number of elements initial 阅读全文
posted @ 2019-08-09 10:34 gentleKay 阅读(153) 评论(0) 推荐(0)
摘要:/** * Given a binary tree, check whether it is a mirror of itself * (ie, symmetric around its center). * 1 * / \ * 2 2 * / \ / \ * 3 4 4 3 * * 对于二叉树,检查它是否是自身的镜像(即,围绕其中心对称)。 * 例如,此二叉树是对称的: * 1 * / \ * 阅读全文
posted @ 2019-08-09 10:33 gentleKay 阅读(309) 评论(0) 推荐(0)
摘要:/** * 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. * For example: * Given the below binary tree 阅读全文
posted @ 2019-08-09 10:32 gentleKay 阅读(130) 评论(0) 推荐(0)
摘要:解析: * 这道题目的意思是 这个数独是否有效,返回 true, false. * 有三种情况不成立,将返回false: * 第一种: 循环每一行, 如果出现了相同的数字就不成立, 遇到 . 直接跳过 * 第二种: 循环每一列, 如果出现了相同的数字就不成立, 遇到 . 直接跳过 * 第三种: 循环 阅读全文
posted @ 2019-08-08 11:49 gentleKay 阅读(820) 评论(0) 推荐(0)