【LeetCode-155 Min Stack Solution (with Java)】教程文章相关的互联网学习教程文章

LeetCode 35 Search Insert Position (C,C++,Java,Python)

Problem: Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0Solution:二分查找,当找不到时l=r+1,所以根据最后一次l和r的变动来判定应该插入的位置,如果最后一次是l=mid+...

LeetCode 62. Unique Paths不同路径 (C++/Java)【代码】【图】

题目: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 any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish‘ in the diagram below).How many possible unique paths are there?Above is a 7 x 3 grid. How many possible unique paths are there?Note: m and n will...

Leetcode 350. Intersection of Two Arrays II JAVA语言【代码】

Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. Note: Each element in the result should appear as many times as it shows in both arrays. The result can be in any order.题意:求两个数组的交集,每个元素可以出现多次,返回的数组顺序随意。public class Solution { public int[] intersect(int[] nums1, int[] nums2) { ...

LeetCode算法题-Binary Number with Alternating Bits(Java实现)【代码】

这是悦乐书的第292次更新,第310篇原创01 看题和准备今天介绍的是LeetCode算法题中Easy级别的第160题(顺位题号是693)。给定正整数,检查它是否具有交替位:即它的二进制数的任意两个相邻位总是具有不同的值。例如: 输入:5输出:true说明:5的二进制表示是:101 输入:7输出:false说明:7的二进制表示为:111。 输入:11输出:false说明:11的二进制表示是:1011。 输入:10输出:true说明:10的二进制表示是:1010。本次解题使...

【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联全部单词的子串)】【代码】【图】

【030-Substring with Concatenation of All Words(串联全部单词的子串)】【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】原题  You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters. For example, given: s: "barfoot...

Java for LeetCode 169 Majority Element【代码】

Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times.You may assume that the array is non-empty and the majority element always exist in the array.解题思路:编程之美P130(寻找发帖水王)原题,如果删除两个不同的num,那么剩下的num,最多的元素依旧出现多于? n/2 ? timesJAVA实现如下: public int majorityElement(int[] nums) {LinkedList...

Java [Leetcode 318]Maximum Product of Word Lengths【代码】

题目描述:Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0.Example 1:Given ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]Return 16The two words can be "abcw", "xtfn".Example 2:Given ["a", "ab", "abc", "d", ...

[LeetCode-JAVA] Permutations【代码】

题目:Given a collection of numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1] 思路:最近都是在做DFS有关的,第一时间的反应就是可以用DFS的思想,唯一需要注意的就是在遍历的过程中,要查看是否是已经被选择的数字,这里用Set来进行排查,思路比较简单。 代码:public List<List<Integer>> permute(int[] nums) {Lis...

leetcode 57 Insert Interval ----- java【代码】

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.Example 1:Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].Example 2:Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].This is because the new inter...

leetcode 646. 最长数对链 java【代码】

题目:给出 n 个数对。 在每一个数对中,第一个数字总是比第二个数字小。现在,我们定义一种跟随关系,当且仅当 b < c 时,数对(c, d) 才可以跟在 (a, b) 后面。我们用这种形式来构造一个数对链。给定一个对数集合,找出能够形成的最长数对链的长度。你不需要用到所有的数对,你可以以任何顺序选择其中的一些数对来构造。示例 :输入: [[1,2], [2,3], [3,4]]输出: 2解释: 最长的数对链是 [1,2] -> [3,4]注意:给出数对的个数在 [1, ...

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java【代码】

【LeetCode】Minimum Depth of Binary TreeGiven a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.递归和非递归,此提比较简单。广度优先遍历即可。关键之处就在于如何保持访问深度。下面是4种代码: 1import java.util.ArrayList;2import java.util.LinkedList;3import java.util.List;4import java.util.Queue;5 ...

Java [Leetcode 102]Binary Tree Level Order Traversal【代码】

题目描述:Given a binary tree, return the level order traversal of its nodes‘ values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3/ 9 20/ 15 7 return its level order traversal as:[[3],[9,20],[15,7] ]解题思路:运用广度优先搜索方法,同http://www.cnblogs.com/zihaowang/p/5149745.html类似。代码如下:/*** Definition for a binary tree node.* pu...

LeetCode算法题-Second Minimum Node In a Binary Tree(Java实现)【代码】

这是悦乐书的第285次更新,第302篇原创01 看题和准备今天介绍的是LeetCode算法题中Easy级别的第153题(顺位题号是671)。给定非空的特殊二叉树,其由具有非负值的节点组成,其中该树中的每个节点具有恰好两个或零个子节点。 如果节点具有两个子节点,则该节点的值是其两个子节点中的较小值。给定这样的二叉树,您需要输出由整个树中所有节点的值组成的集合中的第二个最小值。如果不存在这样的第二个最小值,则输出-1。例如: 2/...

【Leetcode】Unique Paths in JAVA

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 any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish‘ in the diagram below). How many possible unique paths are there? 我的第一反应就是递归~~~每走一步下面的情况相当于变成m-1,n或者m,n-1的情形 也就是说:wa...

Java [Leetcode 101]Symmetric Tree【代码】

题目描述:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1/ 2 2/ \ / 3 4 4 3 But the following is not: 1/ 2 2\ 3 3解题思路:递归法代码如下:/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { v...

STACK - 相关标签