![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
【leetcode刷题日记】
乐观的Zqq
谁在呼叫舰队
展开
-
leetcodes
21. Merge Two Sorted ListsMerge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.Example:Input: 1->2->4,...原创 2019-05-06 21:27:18 · 397 阅读 · 0 评论 -
【Leetcode刷题笔记】110. Balanced Binary Tree 判断一棵树是否是平衡树,即左子树右子树深度差小于等于1
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as:a binary tree in which the depth of the two subtrees of every node never differ ...原创 2019-02-25 22:01:08 · 175 阅读 · 0 评论 -
【Leetcode刷题笔记】 160. Intersection of Two Linked Lists
题目思路:求两个链表是否存在交集。交集:在当前节点到最后节点的所有值都相同首先只能想到比较笨的暴力方法:在A和B的所有节点,遍历到最后的节点,判断是否相同。参考了一些博客之后:博客明白了隐含条件,如果两个链表长度不一样时,那么交集点肯定存在在长度差的地方。于是也就有了解决方法1:首先求出两个链表长度,并且将较长的链表进行后移,知道达到长度相同的点。然后开始判断两个链表节点是否相同,...原创 2019-03-01 13:32:02 · 127 阅读 · 0 评论 -
【Leetcode刷题笔记】984. String Without AAA or BBB 字符串贪心
Given two integers A and B, return any string S such that:S has length A + B and contains exactly A ‘a’ letters, and exactly B ‘b’ letters;The substring ‘aaa’ does not occur in S;The substring ‘bbb...原创 2019-03-01 11:53:07 · 138 阅读 · 0 评论 -
【Leetcode刷题笔记】687. Longest Univalue Path 寻找单路径二叉树中节点值相同的边的个数
Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root.Note: The length of path between two nodes is r...原创 2019-02-28 22:50:35 · 113 阅读 · 0 评论 -
【Leetcode刷题笔记】680. Valid Palindrome II 回文串II 可以删除一个字符再判断
bool validPalindrome(string s) { if(s.size()<2)return false; int l=0,r=s.size()-1; while(l<r){ if(s[l]==s[r]){ l++; r--; ...原创 2019-02-28 21:31:05 · 209 阅读 · 0 评论 -
【Leetcode刷题笔记】290. Word Pattern map以及string映射
Given a pattern and a string str, find if str follows the same pattern.Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.Example ...原创 2019-02-28 21:14:22 · 191 阅读 · 0 评论 -
【Leetcode刷题笔记】914. X of a Kind in a Deck of Cards GCD最小公约数
n a deck of cards, each card has an integer written on it.Return true if and only if you can choose X >= 2 such that it is possible to split the entire deck into 1 or more groups of cards, where:...原创 2019-02-28 20:46:51 · 110 阅读 · 0 评论 -
【Leetcode刷题笔记】219. Contains Duplicate II 判断在k范围之间有无重复数据
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k....原创 2019-02-28 19:23:46 · 136 阅读 · 0 评论 -
【Leetcode刷题笔记】558. Quad Tree Intersection 四叉树求OR
A quadtree is a tree data in which each internal node has exactly four children: topLeft, topRight, bottomLeft and bottomRight. Quad trees are often used to partition a two-dimensional space by recurs...原创 2019-02-26 11:04:05 · 138 阅读 · 0 评论 -
【Leetcode刷题笔记】459. Repeated Substring Pattern KMP算法
Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase Engli...原创 2019-02-26 13:07:46 · 207 阅读 · 0 评论 -
【Leetcode刷题笔记】210. Course Schedule II 有向图能否完全遍历?无环?
class Solution {public: /* 思路:在有向图中能否找到一个无环的路径,遍历所有的节点。 如果有环,则证明无法找到。 贪心思想:首先找到节点中入度为0的节点来进行初始遍历,为什么要入度为0呢? 因为入度为0的情况下,没有其他节点能达到这个节点; 那么如果我们需要遍历所有节点的话,那么我们必须首先从这个节点开始。 ...原创 2019-04-03 17:00:13 · 275 阅读 · 0 评论 -
【Leetcode刷题笔记】148. Sort List 进行链表的排序,不使用额外的存储空间
使用mergeSort思想来进行链表排序;主要思想:分治法ListNode* sortList(ListNode *head){ //当前为空或者只有一个节点 ListNode *slow = head; ListNode *fast = head; //使用两个指针,一快一慢,来找到当前链表的中段,然后二分到两块进行处理 while(fast&&fast->...原创 2019-04-03 16:18:47 · 502 阅读 · 0 评论 -
【Leetcode刷题笔记】204. Count Primes 快速求N以内的素数个数
int countPrimes(int n) { if(n<=2)return 0; int ans = 1; vector<bool>res(n,false); int upper = sqrt(n); for(int i=3;i<n;i=i+2){ if(!res[i]){ ans++; ...原创 2019-03-03 23:17:18 · 591 阅读 · 0 评论 -
【Leetcode刷题笔记】400. Nth Digit 自然数第n个数字
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6,7, 8, 9, 10, 11, …Note: n is positive and will fit within the range of a 32-bit signedinteger (n < 231).int findNthDigit(i...原创 2019-03-03 21:29:09 · 182 阅读 · 0 评论 -
【Leetcode刷题笔记】190. Reverse Bits bit位运算
Example 1:Input: 00000010100101000001111010011100Output: 00111001011110000010100101000000Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261...原创 2019-03-03 20:57:00 · 193 阅读 · 0 评论 -
【Leetcode刷题笔记】205. Isomorphic Strings
Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with another ch...原创 2019-02-26 18:56:53 · 90 阅读 · 0 评论 -
【算法刷题】二维数组按左到右递增,上到下递增。查找某个值
两种解决方法:1.按照二分查找的思想,由于题目的描述,二维数组按左到右递增,那么就相当于每一行都是有序的,可以把每一行都当做一个独立的数组进行二分查找。bool Find1(int target, vector<vector<int> > array) { for(int i=0;i<array.size();i++){ int l =0...原创 2019-03-02 17:00:58 · 987 阅读 · 0 评论 -
【Leetcode刷题笔记】 754. Reach a Number 数学求和问题
参考 https://www.cnblogs.com/ranjiewen/p/9362599.html754. Reach a NumberYou are standing at position 0 on an infinite number line. There is a goal at position target.On each move, you can either go ...原创 2019-03-02 11:04:49 · 146 阅读 · 0 评论 -
【Leetcode刷题笔记】141. Linked List Cycle 在链表中快速判断是否存在环
bool hasCycle(ListNode *head) { if(!head ||!head->next)return false; ListNode *fast=head,*low = head; while(fast&&fast->next){ fast = fast->next->next; ...原创 2019-02-28 12:59:38 · 132 阅读 · 0 评论 -
【Leetcode刷题笔记】438. Find All Anagrams in a String 滑动窗口
Given a string s and a non-empty string p, find all the start indices of p’s anagrams in s.Strings consists of lowercase English letters only and the length of both strings s and p will not be larger...原创 2019-02-28 12:06:40 · 115 阅读 · 0 评论 -
【Leetcode刷题笔记】572. Subtree of Another Tree判断一个树是否是另一个树的子树
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node...原创 2019-02-23 15:00:35 · 212 阅读 · 0 评论 -
628. Maximum Product of Three Numbers
Given an integer array, find three numbers whose product is maximum and output the maximum product.Example 1:Input: [1,2,3]Output: 6Example 2:Input: [1,2,3,4]Output: 24Note:The length of the g...原创 2018-10-04 22:18:05 · 83 阅读 · 0 评论 -
【leetcode刷题日记】121. Best Time to Buy and Sell Stock
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 (i.e., buy one and sell one share of the stock), ...原创 2018-10-11 17:29:28 · 77 阅读 · 0 评论 -
【leetcode刷题日记】 01. Binary Watch
问题描述:A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).Each LED represents a zero or one, with the least significant bi...原创 2018-10-11 17:09:54 · 93 阅读 · 0 评论 -
598. Range Addition II
Given an m * n matrix M initialized with all 0’s and several update operations.Operations are represented by a 2D array, and each operation is represented by an array with two positive integers a and...原创 2018-09-23 11:38:09 · 102 阅读 · 0 评论 -
453. Minimum Moves to Equal Array Elements
Minimum Moves to Equal Array ElementsGiven a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 element...原创 2018-09-23 11:38:27 · 76 阅读 · 0 评论 -
599. Minimum Index Sum of Two Lists
Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings.You need to help them find out their common interest with the l...原创 2018-09-26 00:05:58 · 121 阅读 · 0 评论 -
108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the ...原创 2018-09-25 23:51:05 · 71 阅读 · 0 评论 -
563. Binary Tree Tilt
Given a binary tree, return the tilt of the whole tree.The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all right subtree n...原创 2018-09-25 23:29:18 · 71 阅读 · 0 评论 -
409. Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.This is case sensitive, for example “Aa” is not consid...原创 2018-09-27 22:27:13 · 62 阅读 · 0 评论 -
830. Positions of Large Groups
In a string S of lowercase letters, these letters form consecutive groups of the same character.For example, a string like S = “abbxxxxzyy” has the groups “a”, “bb”, “xxxx”, “z” and “yy”.Call a grou...原创 2018-09-27 22:52:41 · 94 阅读 · 0 评论 -
【Leetcode刷题笔记】437. Path Sum III 寻找树的子路径和中等于sum的路径
You are given a binary tree in which each node contains an integer value.Find the number of paths that sum to a given value.The path does not need to start or end at the root or a leaf, but it must ...原创 2019-02-23 12:37:39 · 154 阅读 · 0 评论 -
【Leetcode刷题】用队列实现栈,用栈实现队列
1.用队列实现栈使用两个队列,进行交替使用。q2作为主要队列,当每一次进栈的时候,先将q2队列中的所有元素先进q1,再将所需要进栈的元素进q2,然后将所有q1元素重新进q2。需要交替使用class MyStack {public: queue<int>q1,q2; /** Initialize your data structure here. */ /...原创 2019-02-23 12:02:42 · 123 阅读 · 0 评论 -
【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 [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \3 4 4 3思路:对...原创 2019-02-23 11:03:47 · 155 阅读 · 0 评论 -
【Leetcode刷题笔记】669. Trim a Binary Search Tree
Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the re...原创 2019-01-30 15:29:21 · 103 阅读 · 0 评论 -
543. Diameter of Binary Tree
/**Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};/class Solution {public:int m...转载 2018-09-28 00:12:57 · 79 阅读 · 0 评论 -
175. Combine Two Tables
Table: Person+-------------+---------+| Column Name | Type |+-------------+---------+| PersonId | int || FirstName | varchar || LastName | varchar |+-------------+---------+Per...原创 2018-09-27 23:15:54 · 88 阅读 · 0 评论 -
268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.Example 1:Input: [3,0,1]Output: 2Example 2:Input: [9,6,4,2,3,5,7,0,1]Output: ...原创 2018-09-27 23:08:54 · 63 阅读 · 0 评论 -
661. Image Smoother
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray scale of each cell becomes the average gray scale (rounding down) of all the 8 surro...原创 2018-09-25 15:49:41 · 90 阅读 · 0 评论