leetcode

[LeetCode] 864. Shortest Path to Get All Keys 获得所有钥匙的最短路径

匿名 (未验证) 提交于 2019-12-02 23:49:02
grid "." "#" "@" "a" "b" , ...) are keys, and ( "A" "B" , ...) are locks. K -1 . Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6 Note: grid[i][j] '#' '@' 'a'-``'f``' 'A'-'F' [1, 6] Github 同步地址: https://github.com/grandyang/leetcode/issues/864 参考资料: https://leetcode.com/problems/shortest-path-to-get-all-keys/ LeetCode All in One 题目讲解汇总(持续更新中...)

leetcode每日刷题计划-简单篇day22

匿名 (未验证) 提交于 2019-12-02 23:47:01
orzorz不能断了打卡我一定要在十二点之前交。。。划水一道题 面向大作业疯狂试探.jpg Num 171 Excel表序列号 没难点。 class Solution { public : int change ( int i , int len ) { int ans = 1 ; for ( int j = 1 ; j <= len - 1 - i ; j ++) ans = ans * 26 ; return ans ; } int titleToNumber ( string s ) { int ans = 0 ; for ( int i = 0 ; i < s . length (); i ++) { ans = ans +( int )( s [ i ]- 64 )* change ( i , s . length ()); } return ans ; } }; View Code 转载请标明出处: leetcode每日刷题计划-简单篇day22 文章来源: leetcode每日刷题计划-简单篇day22

(栈)leetcode 394

匿名 (未验证) 提交于 2019-12-02 23:47:01
class Solution { public : string decodeString ( string s ) { stack <int> nums ; stack <string> ch ; int num = 0 ; string str = "" ; for ( auto c : s ){ if ( isdigit ( c )) num = num * 10 + ( c - '0' ); else if ( isalpha ( c )) str += c ; else if ( c == '[' ){ ch . push ( str ); nums . push ( num ); str = "" ; num = 0 ; } else { string tmp = str ; for ( int i = 0 ; i < nums . top ()- 1 ; ++ i ) //本身一次已经在内,故nums.top()-1 str += tmp ; //这里字符相加的顺序不能颠倒 str = ch . top ()+ str ; nums . pop (); ch . pop (); } } return str ; } }; 转载请标明出处: (栈)leetcode 394 文章来源: (栈)leetcode 394

[LeetCode] 878. Nth Magical Number 第N个神奇数字

匿名 (未验证) 提交于 2019-12-02 23:47:01
magical 10^9 + 7 . Example 1: Input: N = 1, A = 2, B = 3 Output: 2 Example 2: Input: N = 4, A = 2, B = 3 Output: 6 Example 3: Input: N = 5, A = 2, B = 4 Output: 10 Example 4: Input: N = 3, A = 6, B = 4 Output: 8 Note: Github 同步地址: https://github.com/grandyang/leetcode/issues/878 参考资料: https://leetcode.com/problems/nth-magical-number/ LeetCode All in One 题目讲解汇总(持续更新中...)

LeetCode 1019. Next Greater Node In Linked List (链表中的下一个更大节点)

匿名 (未验证) 提交于 2019-12-02 23:47:01
题目标签:Linked List, Stack   题目给了我们一个 Linked List,让我们找出对于每一个数字,它的下一个更大的数字。   首先把 Linked List 里的数字 存入 ArrayList, 方便后面的操作。   然后遍历 ArrayList,首先每一个数字,都会存入stack;所以就可以利用stack回到之前的数字,存入它的 next Greater Node。 Java Solution: Memory Usage: 40 MB, less than 95 % 完成日期:05/06/2019 关键点:利用stack /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public int[] nextLargerNodes(ListNode head) { ArrayList<Integer> nums = new ArrayList<>(); Stack<Integer> stack = new Stack<>(); int [] res; // save all numbers into

leetcode:反转字符串

匿名 (未验证) 提交于 2019-12-02 23:43:01
题目 编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。 不要给另外的数组分配额外的空间,你必须 原地 修改输入数组 、使用 O(1) 的额外空间解决这一问题。 你可以假设数组中的所有字符都是 ASCII 码表中的可打印字符。 示例 1: 输入: ["h","e","l","l","o"] 输出: ["o","l","l","e","h"] 示例 2: 输入: ["H","a","n","n","a","h"] 输出: ["h","a","n","n","a","H"] ˼· 这道题在此前是返回字符数组, 但现在换成了原地修改, 无返回值. 这使得高级函数的使用都被限制住了, 如果可以返回字符数组, 那么在 Python 中可以直接用 return s[::-1] 这条语句即可实现反转, 在 Java 等高级语言中也有 reverse() 这样的函数. 现在题目修改之后, 思路被限定了, 你需要 使用两个指针 来进行数据交换. 遍历半个 数组, 依次交换两个数即可. 遗憾的是, LeetCode 在这道题的解答排名上尚未舍弃之前允许返回值的解答, 我无法判断这种解法是否最优, 如有更优解法, 欢迎留言告知. 如下图所示, 排名靠前的解答并不符合现在的题目 示意 实现 尽管我已经尽量优化语句, 如在 Python 中使用这样 s[i], s

Leetcode 695.岛屿的最大面积

匿名 (未验证) 提交于 2019-12-02 23:43:01
给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合。你可以假设二维矩阵的四个边缘都被水包围着。 找到给定的二维数组中最大的岛屿面积。(如果没有岛屿,则返回面积为0。) 示例 1: [[0,0,1,0,0,0,0,1,0,0,0,0,0], [0,0,0,0,0,0,0,1,1,1,0,0,0], [0,1,1,0,1,0,0,0,0,0,0,0,0], [0,1,0,0,1,1,0,0,1,0,1,0,0], [0,1,0,0,1,1,0,0,1,1,1,0,0], [0,0,0,0,0,0,0,0,0,0,1,0,0], [0,0,0,0,0,0,0,1,1,1,0,0,0], [0,0,0,0,0,0,0,1,1,0,0,0,0]] 对于上面这个给定矩阵应返回 6。注意答案不应该是11,因为岛屿只能包含水平或垂直的四个方向的‘1’。 示例 2: [[0,0,0,0,0,0,0,0]] 对于上面这个给定的矩阵, 返回 0。 注意: 给定的矩阵grid 的长度和宽度都不超过 50。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/max-area-of-island Javascript 代码实现: /** * @param {number[]

[LeetCode] 407. Trapping Rain Water II_hard tag: Heap

匿名 (未验证) 提交于 2019-12-02 23:41:02
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining. Note: Both m and n are less than 110. The height of each unit cell is greater than 0 and is less than 20,000. Example: Given the following 3x6 height map : [ [ 1 , 4 , 3 , 1 , 3 , 2 ], [ 3 , 2 , 1 , 3 , 2 , 4 ], [ 2 , 3 , 3 , 2 , 3 , 1 ] ] Return 4. The above image represents the elevation map [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]] before the rain. After the rain, water is trapped between the blocks. The total volume of

leetcode 189. 旋转数组

匿名 (未验证) 提交于 2019-12-02 23:36:01
leetcode 189. 旋转数组 1. 题目描述 2. 解题思路 难度:简单 题库地址: https://leetcode-cn.com/problems/rotate-array/ 1. 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。 示例 1: 输入: [1,2,3,4,5,6,7] 和 k = 3 输出: [5,6,7,1,2,3,4] 解释: 向右旋转 1 步: [7,1,2,3,4,5,6] 向右旋转 2 步: [6,7,1,2,3,4,5] 向右旋转 3 步: [5,6,7,1,2,3,4] 示例 2: 输入: [-1,-100,3,99] 和 k = 2 输出: [3,99,-1,-100] 解释: 向右旋转 1 步: [99,-1,-100,3] 向右旋转 2 步: [3,99,-1,-100] 说明: 尽可能想出更多的解决方案,至少有三种不同的方法可以解决这个问题。 要求使用空间复杂度为 O(1) 的原地算法。 2. 解题思路 英文版的有答案: https://leetcode.com/problems/rotate-array/solution/ 这里记录一下第4种方法的理解: Approach #4 Using Reverse [Accepted] 假设对长度为 n 的数组,进行 k 次旋转。 首先要找到 最终有效 的次数

Leetcode学习笔记:#543. Diameter of Binary Tree

匿名 (未验证) 提交于 2019-12-02 23:35:02
Leetcode学习笔记:#543. Diameter of Binary Tree Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. 实现: int max = 0 ; public int diameterOfBinaryTree ( TreeNode root ) { maxDepth ( root ); return max ; } private int maxDepth ( TreeNode root ) { if ( root == null ) return 0 ; int left = maxDepth ( root . left ); int right = maxDepth ( root . right ); max = Math . max ( max , left + right ); return Math . max ( left , right ) + 1