leetcode

LeetCode 42:接雨水

a 夏天 提交于 2019-12-21 23:42:51
LeetCode 42:接雨水 题目描述 解题思路 代码实现 总结 题目描述 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 解题思路 借助栈的结构来解决,设定 指针初始位置为0 ,在越界的前提下, 依次向后 移动,在 初始及栈顶元素的高度大于当前指针所指高度 时, 压栈 操作, 指针后 移。当 栈不为空且 , 栈顶元素高度小于当前指针的高度 时,将 栈顶 高度的值 存入中间变量 ,并将栈顶 弹 出。此时如果 栈为空 的话,说明没有另外一面墙和其配对, 则break 继续 压栈指针后移 。 否则 ,记录两面墙之间的距离,用 两面墙中的最小值与距离相乘 ,即 为面积 。,重复上述操作,记录累计的面积和!!!!! 代码实现 去 博客设置 页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片 . import java . util . Stack ; public class waterContaining { //接雨水 public int trap ( int [ ] height ) { //acc: 8ms 30% int sum = 0 ; //雨水总量 Stack < Integer > stack = new Stack < > ( ) ; int current = 0 ; //指针初始位置 while

LeetCode All in One 题目讲解汇总(持续更新中...)

∥☆過路亽.° 提交于 2019-12-21 22:27:45
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 function such as sqrt . Example 1: Input: 16 Returns: True Example 2: Input: 14 Returns: False Credits: Special thanks to @elmirap for adding this problem and creating all test cases. 这道题给了我们一个数,让我们判断其是否为完全平方数,那么显而易见的是,肯定不能使用 brute force,这样太不高效了,那么最小是能以指数的速度来缩小范围,那么我最先想出的方法是这样的,比如一个数字 49,我们先对其除以2,得到 24,发现 24 的平方大于 49,那么再对 24 除以2,得到 12,发现 12 的平方还是大于 49,再对 12 除以2,得到6,发现6的平方小于 49,于是遍历6到 12 中的所有数,看有没有平方等于 49 的,有就返回 true,没有就返回 false,参见代码如下: 解法一: class

LeetCode All in One 题目讲解汇总(持续更新中...)

一曲冷凌霜 提交于 2019-12-21 14:26:09
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123 . Find the total sum of all root-to-leaf numbers. Note: A leaf is a node with no children. Example: Input: [1,2,3] 1 / \ 2 3 Output: 25 Explanation: The root-to-leaf path 1->2 represents the number 12. The root-to-leaf path 1->3 represents the number 13. Therefore, sum = 12 + 13 = 25. Example 2: Input: [4,9,0,5,1] 4 / \ 9 0 / \ 5 1 Output: 1026 Explanation: The root-to-leaf path 4->9->5 represents the number 495. The

硬核! 逛了4年Github ,一口气把我收藏的 Java 开源项目分享给你!

拜拜、爱过 提交于 2019-12-20 01:54:39
Awsome Java Great Java project on Github(Github 上非常棒的 Java 开源项目). English Version 大家都知道 Github 是一个程序员福地,这里有各种厉害的开源框架、软件或者教程。这些东西对于我们学习和进步有着莫大的进步,所以我有了这个将 Github 上非常棒的 Java 开源项目整理下来的想法。我会按照几个维度对项目进行分类,以便大家查阅。当然,如果你觉得不错的话,欢迎给本项目点个 Star。我会用我的业余时间持续完善这份名单,谢谢🙏。 欢迎大家推荐自己觉得不错的 Java 项目,下面项目的排序很大程度是根据当前项目的 Star 数量。 教程 Java JavaGuide :【Java学习 面试指南】 一份涵盖大部分Java程序员所需要掌握的核心知识。 CS-Notes :技术面试必备基础知识、Leetcode 题解、后端面试、Java 面试、春招、秋招、操作系统、计算机网络、系统设计。 advanced-java :互联网 Java 工程师进阶知识完全扫盲:涵盖高并发、分布式、高可用、微服务、海量数据处理等领域知识。 architect-awesome :后端架构师技术图谱。 toBeTopJavaer :Java工程师成神之路 。 tutorials:该项目是一系列小而专注的教程 - 每个教程都涵盖

LeetCode 1108. Defanging an IP Address (IP 地址无效化)

北慕城南 提交于 2019-12-20 00:48:26
题目标签:String   题目给了我们一组 ip address,让我们把 . 变成 [.],这题可以用replace,但是这样做的话,好像没意义了。所以还是走一下array,具体看code。 Java Solution: Runtime: 0 ms, faster than 100 % Memory Usage: 34 MB, less than 100 % 完成日期:08/01/2019 关键点:n/a class Solution { public String defangIPaddr(String address) { char[] charsArr = address.toCharArray(); StringBuilder sb = new StringBuilder(); for(char c : charsArr) { if(c == '.') sb.append("[.]"); else sb.append(c); } return sb.toString(); } } 参考资料:n/a LeetCode 题目列表 - LeetCode Questions List 题目来源:https://leetcode.com/ 来源: https://www.cnblogs.com/jimmycheng/p/11300831.html

LeetCode All in One 题目讲解汇总(持续更新中...)

允我心安 提交于 2019-12-18 03:27:14
In a N x N grid representing a field of cherries, each cell is one of three possible integers. 0 means the cell is empty, so you can pass through; 1 means the cell contains a cherry, that you can pick up and pass through; -1 means the cell contains a thorn that blocks your way. Your task is to collect maximum number of cherries possible by following the rules below: Starting at the position (0, 0) and reaching (N-1, N-1) by moving right or down through valid path cells (cells with value 0 or 1); After reaching (N-1, N-1), returning to (0, 0) by moving left or up through valid path cells; When

LeetCode 496. 下一个更大元素 I(Next Greater Element I) 35

坚强是说给别人听的谎言 提交于 2019-12-18 03:09:48
496. 下一个更大元素 I 496. Next Greater Element I 题目描述 给定两个没有重复元素的数组 nums1 和 nums2,其中 nums1 是 nums2 的子集。找到 nums1 中每个元素在 nums2 中的下一个比其大的值。 nums1 中数字 x 的下一个更大元素是指 x 在 nums2 中对应位置的右边的第一个比 x 大的元素。如果不存在,对应位置输出 -1。 每日一算法 2019/6/7 Day 35 LeetCode 496. Next Greater Element I 示例 1: 输入: nums1 = [4,1,2], nums2 = [1,3,4,2]. 输出: [-1,3,-1] 解释: 对于 num1 中的数字 4,你无法在第二个数组中找到下一个更大的数字,因此输出 -1。 对于 num1 中的数字 1,第二个数组中数字 1 右边的下一个较大数字是 3。 对于 num1 中的数字 2,第二个数组中没有下一个更大的数字,因此输出 -1。 示例 2: 输入: nums1 = [2,4], nums2 = [1,2,3,4]. 输出: [3,-1] 解释: 对于 num1 中的数字 2,第二个数组中的下一个较大数字是 3。 对于 num1 中的数字 4,第二个数组中没有下一个更大的数字,因此输出 -1。 注意: nums1 和

leetcode 367. Valid Perfect Square

╄→尐↘猪︶ㄣ 提交于 2019-12-17 22:03:43
https://leetcode.com/problems/valid-perfect-square/ 给定一个正整数num,判断它是不是一个完全平方数 一、问题分析 测试用例: Input: 16 Output: true Input: 14 Output: false 完全平方数为:0, 1, 4, 9, 16, ... 也就是平方根为整数的正整数 二、代码实现 class Solution { public boolean isPerfectSquare(int num) { //0、1都是完全平方数 if (num == 0 || num == 1) { return true; } int left = 2, right = num; while (left < right) { int mid = left + (right - left) / 2; //注意这里不能写成int square = mid * mid; 一些测试用例会溢出 long square = (long)mid * mid; //System.out.println(square); if (square == num) { return true; } else if (square < num) { left = mid + 1; } else { right = mid - 1; } } /

leetcode string match相关题目

你离开我真会死。 提交于 2019-12-17 05:22:56
1. Regular Expression Matching( https://oj.leetcode.com/problems/regular-expression-matching/ ) 解法一:递归 (1)当p[j+1] != '*'时, 若s[i] == p[j] 或p[j]=='.', 返回isMatch(i+1, j+1)。 若s[i] != p[j], return False (2)当p[j+1] == '*'时,若s[i] == p[j], 则要看s[i]开始的字串,若s[i],s[i+1]....s[k]都等于p[j],则要分别判断isMathc(i, j+2), isMatch(i+1, j+2), isMath(i+2, j+2)...isMatch(k, j+2),因为b*指的是0个或多个b python代码如下: 1 def isMatch(self, s, p): 2 3 def match(idxs, idxp): 4 if idxp == len(p): 5 return idxs == len(s) 6 7 if idxp == len(p)-1 or p[idxp+1] != '*': 8 if idxs == len(s) or (s[idxs] != p[idxp] and p[idxp] != '.'): 9 return False

Java开发牛人十大必备网站

不想你离开。 提交于 2019-12-17 02:10:35
英文原文: Top 10 Websites for Advanced Level Java Developers   以下是我收集的 Java 开发牛人必备的网站。这些网站可以提供信息,以及一些很棒的讲座, 还能解答一般问题、面试问题等。质量是衡量一个网站的关键因素,我个人认为这些网站质量都很好。接下来,我会跟大家分享我是如何使用这些网站学习和娱乐的。或许你会认为有些网站适合任何水平的开发者,但是我认为:对于 Java 开发牛人来说,网站的好坏取决于如何使用它们。    1、Stackoverflow   Stackoverflow.com 可能是编程界中最流行的网站了,该网站有成千上万个好问题和答案。学习 API 或者编程语言,通常依赖于代码示例,stackoverflow 就可以提供大量的代码片段。   Stackoverflow 的另一个优点在于它的社交性。你可以在一些特定的标签下查看问题,比如“Java”、“regex”,你就会看到哪些是高频问题。这对于学习,以及 Java 博主书写热门话题都是一种非常好的资源。   网站地址: http://stackoverflow.com/    2、DZone   这是一个非常有趣的网站,有相当多的开发者在这个网站上分享他们博客文章。就像一场冒险一样,在这里,你永远不知道下一秒会看到什么内容。   网站地址: http://www