【leetcode刷题笔记】Simplify Path】的更多相关文章

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 and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1 return true…
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己的切身经历更新): 算法不是纯粹拼智商的,智商高,就一定很厉害,不够聪明,就一定不行.算法是一种技能,是可以通过科学合理的方式训练出来的能力.目前国内大厂的算法考察,基本不会超过leetcode 中等难度,上限难度基本都是leetcode 中等题里面的中等难度 基本的算法数据结构是有限的.比如说链表…
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票). 思路:买高不买低,向上即可 class Solution(object): def maxProfit(self, prices): "…
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链接:https://leetcode-cn.com/problemset/all/ 一.题意 难度:中等 https://leetcode-cn.com/problems/integer-to-roman/ 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5…
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口(两个指针包围的区域为当前的窗口),经常用于区间搜索. 若两个指针指向同一数组,但是遍历方向相反,那我们可以用来进行搜索,这时我们要搜索的数组往往需要排序好. 2.双指针类型 在目前的刷到过的题目种,遇到了两类双指针问题 第一类是快慢指针,顾名思义,就是一个fast指针,一个slow指针,一前一后,…
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c" Corner Cases: Did you consider the case where path = "/../"?In th…
LeetCode1-9 本文更多是作为一个习题笔记,没有太多讲解 1.两数之和 题目请点击链接 ↑ 最先想到暴力解法,直接双循环,但是这样复杂度为n平方 public int[] twoSum(int[] nums, int target) { for (int i = nums.length - 1; i >= 0; i--) { for (int j = 0; j < i; j++) { if (nums[i] + nums[j] == target) { int[] twoSum = n…
(1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie…
题目描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即为整数的值.如果第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数. 字符串可以在形成整数的字符后面包括多余的字符,这些字符可以被忽略,它们对于函数没有影响. 当字符串中的第一个非空字符序列不是个有效的整数:或字符串为空:或字符串仅包含空白字符时,则不进行转换. 若函数不能执行有…
1.  minimum-depth-of-binary-tree 题目描述 Given 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. 求二叉树的最小深度:   解题思路 递归遍历二叉树的每一个节点: (1)如果该节点为null,返回深度为0…
题目描述: 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab"输出:[ ["aa","b"], ["a","a","b"]] 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/palindrome-partitioning 解题思路: 1.显然回溯法:求所有可能…
题目描述: 给定一个整数,写一个函数来判断它是否是2的幂. 题目分析: 判断一个整数是不是2的幂,可根据二进制来分析.2的幂如2,4,8,等有一个特点: 二进制数首位为1,其他位为0,如2为10,4为100 2&(2-1)=0   4&(4-1)=0 即得出结论如果一个数n为2的幂,则n(n-1)=0 此题通过率37.6%,挺高的 解答代码: C++版: class Solution { public: bool isPowerOfTwo(int n) { ) return false;…
题目描述: 给定一个整数 (32位有符整数型),请写出一个函数来检验它是否是4的幂. 示例:当 num = 16 时 ,返回 true . 当 num = 5时,返回 false. 问题进阶:你能不使用循环/递归来解决这个问题吗? 题目分析: 如231题同样思路,还是通过位操作来解决这道 首先判断下输入为0和负数的情况 然后分析4的幂的特点0,4,16 化为二进制0,0100,00010000 跟求2的幂不同的是此处少了2,8 化为2进制   ,0010,00001000 0 0 0 0 0 1…
题目描述: 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量). 示例: 输入: 输出: 解释: 32位整数 的二进制表示为 . 题目分析: 判断32位二进制中1的个数,此题利用n&(n-1)性质可解 解答代码: C++版: class Solution { public: int hammingWeight(uint32_t n) { ; ){ n=n&(n-); ans++; } return ans; } }; Code Pytho…
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given:start = "hit…
Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome partitioning ["aa",…
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example,S = "ADOBECODEBANC"T = "ABC" Minimum window is "BANC". Note:If there is no such window i…
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be us…
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ]  The minimum path sum from top to bottom is 11 (…
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" or 1211. Given an…
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题解:开始想到的方法比较偷懒,直接遍历一遍数组,把每个ListNode对应的值放到数组里面,然后把数组转换成BST,想来这个题的本意肯定不是这样. 自己也想到了bottom-up的方法,但是没想明白怎么个bottom-up法,真的是昨天做梦都在想=.= 今天早上终于弄懂了,用递归…
Sort a linked list in O(n log n) time using constant space complexity. 题解:实现一个链表的归并排序即可.主要分为三部分: 1.找到中点并返回的函数findMiddle; 2.归并函数merge; 3.排序函数sortList. 数组的findMiddle函数非常容易实现,链表就有一点tricky了.首先设置两个指针,一个slow初始化为head,一个fast初始化为head.next,然后slow一次走一步,fast一次走两…
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space cha…
Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. 题解:水题不解释 /** * Definition for singly-linked list. * struct Li…
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example,Given the following matrix: [ [ , , ], [ , , ], [ , , ] ] You should return [1,2,3,6,9,8,7,4,5]. 题解:还是找规律的题:设有四个变量Xs,Xe,Ys,Ye,如下图所示,它…
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3): Given n and k, return the kth permutation sequence. Note: Given n will be between…
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 解题:果然不能晚上做题,效率好低.看了讨论才学会的解法.设置一个指针next指向当前访问的…
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 解题:应该是很简单的一道题,纠结了好久T_T 基本思路很简单,用栈模拟就可以了.首先根节…
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3解题: 有一个很简单的结论,就是节点数为n的二叉树共有h(n)种形态,其中h(n)是卡特兰数,h…
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). Ho…