leetcode

LeetCode #45 Jump Game II

有些话、适合烂在心里 提交于 2020-01-16 07:04:12
Question Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps. Example: Input: [2,3,1,1,4] Output: 2 Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index. Note: You can assume that you can always reach the last index. 贪心 O(n) 这是一道贪心题,难就难在是一道贪心题。只要想通了,就好解决了。 贪心的对象不是当前能够达到最远的点,比如sample中[2,3,1,1,4]显然第一步显然不是1,而是3。当然也是不是数字大小

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

心已入冬 提交于 2020-01-16 05:14:21
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. Example 1: Input: [1,3,5,6], 5 Output: 2 Example 2: Input: [1,3,5,6], 2 Output: 1 Example 3: Input: [1,3,5,6], 7 Output: 4 Example 4: Input: [1,3,5,6], 0 Output: 0 这道题基本没有什么难度,实在不理解为啥还是 Medium 难度的,完完全全的应该是 Easy 啊(貌似现在已经改为 Easy 类了),三行代码搞定的题,只需要遍历一遍原数组,若当前数字大于或等于目标值,则返回当前坐标,如果遍历结束了,说明目标值比数组中任何一个数都要大,则返回数组长度n即可,代码如下: 解法一: class Solution { public: int searchInsert(vector<int>& nums, int target) { for

670. Maximum Swap**

☆樱花仙子☆ 提交于 2020-01-16 00:10:57
670. Maximum Swap** https://leetcode.com/problems/maximum-swap/ 题目描述 Given a non-negative integer, you could swap two digits at most once to get the maximum valued number. Return the maximum valued number you could get. Example 1: Input: 2736 Output: 7236 Explanation: Swap the number 2 and the number 7. Example 2: Input: 9973 Output: 9973 Explanation: No swap. Note: The given number is in the range [ 0 , 1 0 8 ] [0, 10^8] [ 0 , 1 0 8 ] C++ 实现 1 贪婪策略. 来自 LeetCode 的解答: https://leetcode.com/problems/maximum-swap/solution/ 发现英文的表述更为精准, 这里引用一下: Intuition At each digit of the input number in order,

leetcode 28. Implement strStr() 详解 python3

天涯浪子 提交于 2020-01-15 23:19:24
一.问题描述 Implement strStr() . Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack = "hello", needle = "ll" Output: 2 Example 2: Input: haystack = "aaaaa", needle = "bba" Output: -1 Clarification: What should we return when needle is an empty string? This is a great question to ask during an interview. For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf() . 二.解题思路 总体来说这道题还是非常简单的,主要是考察思维的一个细度,对特殊情况如空字符串的处理。 一开始没看到它的声明:即空字符串返回0,第一版直接出错。

Leetcode 第2题 两数相加(Java实现)

点点圈 提交于 2020-01-15 08:11:09
Leetcode 第2题 两数相加(Java实现) 解题思路 刚刚学完韩顺平老师的《数据结构与算法》的链表部分,就来leetcode上找了一道题来练习。 首先定义一个ListNode的类,val用来存放整型数据,.next指向下一个节点; 再定义两个链表相加的函数,输入为链表l1和l2; 2.1定义一个头节点head,它不用来存放数据,只是用于索引; 2.2 定义一个类型为ListNode的中间变量temp,用来存放相加后的值; 2.3 用carry来存两数相加时溢出的值; 2.4 来一个while循环,循环终止条件是l1和l2都指向空节点; 2.4.1 从第一个节点对l1和l2进行相加操作; 2.4.2 逐个节点的对res进行赋值; 2.5 退出循环后,看一下carry中是否还有值,有值的话继续给下一个res节点进行赋值; 完毕 代码实现 public static ListNode addTwoNumbers ( ListNode l1 , ListNode l2 ) { ListNode head = new ListNode ( 0 ) ; ListNode res = head ; int carry = 0 ; while ( l1 != null || l2 != null ) { if ( l1 != null && l2 != null ) { int x =

Java开发必备的9个英文网站

我是研究僧i 提交于 2020-01-15 04:12:31
以下是收集的10个Java开发相关的网站,这些网站质量都很好,它们提供了业界信息以及一些很棒的讲座, 还能解答常见开发过程中遇到的问题、面试问题等。下面,我们具体来了解下: Stackoverflow Stackoverflow.com 可能是编程界中最流行的网站了,该网站有成千上万个好问题和答案。学习API或者编程语言,通常依赖于代码示例,stackoverflow就可以提供大量的代码片段。Stackoverflow的另一个优点在于它的社交性。你可以在一些特定的标签下查看问题,比如“Java”、“regex”,你就会看到哪些是高频问题。这对于学习,以及Java博主书写热门话题都是一种非常好的资源。 DZone 这是一个非常有趣的网站,有相当多的开发者在这个网站上分享他们的博客文章。就像一场冒险一样,在这里,你永远不知道下一秒会看到什么内容。 LeetCode 如果有Java方面的面试问题,例如“在Java中,内存中的数组是什么样的”,你可以从很多Java教程中找到答案。但是,遇到类似于“如何将SortedArray转化成一个BalancedTree”这样的问题,你就应该去LeetCode了。LeetCode包含一系列算法问题, 它能为有关信息技术的面试提供一个社会性的平台。这个网站最好的地方就是,它可以通过大小不同的数据在线检查你的代码是否正确。很多人认为

研发十大站点

↘锁芯ラ 提交于 2020-01-15 04:06:48
1、Stackoverflow Stackoverflow.com 可能是编程界中最流行的网站了,该网站有成千上万个好问题和答案。学习API或者编程语言,通常依赖于代码示例,stackoverflow就可以提供大量的代码片段。 Stackoverflow的另一个优点在于它的社交性。你可以在一些特定的标签下查看问题,比如“Java”、“regex”,你就会看到哪些是高频问题。这对于学习,以及Java博主书写热门话题都是一种非常好的资源。 网站地址: http://stackoverflow.com/ 2、DZone 这是一个非常有趣的网站,有相当多的开发者在这个网站上分享他们博客文章。就像一场冒险一样,在这里,你永远不知道下一秒会看到什么内容。 网站地址: http://www.dzone.com 3、LeetCode 如果有Java方面的面试问题,例如“在Java中,内存中的数组是什么样的”,你可以从很多Java教程中找到答案。但是,遇到类似于“如何将SortedArray转化成一个BalancedTree”这样的问题,你就应该去求助LeetCode了。LeetCode包含一系列算法问题, 它能为有关信息技术的面试提供一个社会性的平台。这个网站最好的地方就是,它可以通过大小不同的数据在线检查你的代码是否正确。很多人认为,面试成功的关键在于重复这三个重要步骤:编码->阅读->讨论。

Java开发十大必备网站

筅森魡賤 提交于 2020-01-15 03:54:38
质量是衡量一个网站的关键因素,我个人认为这些网站质量都很好。接下来,我会跟大家分享我是如何使用这些网站学习和娱乐的。或许你会认为有些网站适合任何水平的开发者,但是我认为:对于Java开发牛人来说,网站的好坏取决于如何使用它们。 1、Stackoverflow Stackoverflow.com 可能是编程界中最流行的网站了,该网站有成千上万个好问题和答案。学习API或者编程语言,通常依赖于代码示例,stackoverflow就可以提供大量的代码片段。 Stackoverflow的另一个优点在于它的社交性。你可以在一些特定的标签下查看问题,比如“Java”、“regex”,你就会看到哪些是高频问题。这对于学习,以及Java博主书写热门话题都是一种非常好的资源。 网站地址: http://stackoverflow.com/ 2、DZone 这是一个非常有趣的网站,有相当多的开发者在这个网站上分享他们博客文章。就像一场冒险一样,在这里,你永远不知道下一秒会看到什么内容。 网站地址: http://www.dzone.com 3、LeetCode 如果有Java方面的面试问题,例如“在Java中,内存中的数组是什么样的”,你可以从很多Java教程中找到答案。但是,遇到类似于“如何将SortedArray转化成一个BalancedTree”这样的问题,你就应该去求助LeetCode了

《1.3-1.8 Leetcode》

我是研究僧i 提交于 2020-01-14 09:08:55
1. 对称二叉树 题目描述: 给定一个二叉树,检查它是否是镜像对称的。 例如,二叉树 [1,2,2,3,4,4,3] 是对称的。 代码: package leetcode . week3 ; /** * @author chengzhengda * @version 1.0 * @date 2020-01-08 13:13 * @desc 对称二叉树 */ public class t101 { public static boolean isSymmetric ( TreeNode root ) { if ( root == null ) { return true ; } return istric ( root . left , root . right ) ; } public static boolean istric ( TreeNode left , TreeNode right ) { if ( left != null && right != null ) { if ( ! left . data . equals ( right . data ) ) { return false ; } } else if ( left == null && right == null ) { return true ; } else { return false ; }

Leetcode study time

最后都变了- 提交于 2020-01-14 04:49:38
August 2, 2015 在http://zzk.cnblogs.com/ 用"找一找", 花了几个小时, 找出比较好的Leetcode博客. Read the leetcode question and solutions: 1. Find lowest common ancestor in binary tree: blog of a facebook programmer working on leetcode questions: 递归版本:空间O(1),时间O(n) http://www.cnblogs.com/chkkch/archive/2012/11/26/2788795.html Try it myself and compare to solutions in my blog: http://juliachencoding.blogspot.ca/2015/07/leetcode-lowest-common-ancestor-in.html 2. Reverse nodes in k group http://www.cnblogs.com/lichen782/p/leetcode_Reverse_Nodes_in_kGroup.html http://www.cnblogs.com/grandyang/p/4606710.html 3. binary