
leetcode
文章平均质量分 50
云胡同学
GitHub 地址:https://github.com/stevenling
微信公众号:yunhu_123
展开
-
6. ZigZag Conversion
题目描述The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H N A P L S I原创 2017-01-10 10:02:16 · 298 阅读 · 0 评论 -
LeetCode 153. Find Minimum in Rotated Sorted Array
题目描述Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.You may assume no du...原创 2018-08-31 20:01:56 · 164 阅读 · 0 评论 -
LeetCode 237. 删除链表中的节点
题目描述请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。输入: head = [4,5,1,9], node = 5输出: [4,1,9]解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.思路给我们的只有待删除的节点,不能遍历。比如 1->3-&am原创 2018-08-21 10:28:21 · 106 阅读 · 0 评论 -
LeetCode 18. 4Sum
题目描述Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note: The soluti...原创 2018-08-31 20:04:24 · 120 阅读 · 0 评论 -
LeetCode 232. 用栈实现队列
题目描述使用栈实现队列的下列操作:push(x) – 将一个元素放入队列的尾部。pop() – 从队列首部移除元素。peek() – 返回队列首部的元素。empty() – 返回队列是否为空。思路创建两个栈 s1, s2入队将元素放入 s1 中出队若 s2 为空,将 s1 的元素 放入 s2 中, 此时 s2 的栈顶就是队首元素,s2 栈顶出栈。若 ...原创 2018-08-31 20:23:34 · 524 阅读 · 0 评论 -
LeetCode 83. 删除排序链表中的重复元素
题目描述给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。示例 1:输入: 1->1->2输出: 1->2示例 2:输入: 1->1->2->3->3输出: 1->2->3思路方法 1新建一个链表 newhead,第一个节点的值是 head 的值。用两个节点 p 与 qp 用来循...原创 2018-08-31 20:46:17 · 132 阅读 · 0 评论 -
LeetCode 20. 有效的括号
题目描述给定一个只包括 ‘(‘,’)’,’{‘,’}’,’[‘,’]’ 的字符串,判断字符串是否有效。有效字符串需满足:左括号必须用相同类型的右括号闭合。左括号必须以正确的顺序闭合。 注意空字符串可被认为是有效字符串。示例 1:输入: "()"输出: true示例 2:输入: "()[]{}"输出: true示例 3:输入: "(]"输出: f...原创 2018-08-29 08:41:28 · 226 阅读 · 0 评论 -
LeetCode 155. 最小栈
题目描述设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。push(x) – 将元素 x 推入栈中。pop() – 删除栈顶的元素。top() – 获取栈顶元素。getMin() – 检索栈中的最小元素。示例:MinStack minStack = new MinStack();minStack.push(-2);minStack....原创 2018-08-29 08:42:30 · 469 阅读 · 0 评论 -
LeetCode 21. 合并两个有序链表
题目描述将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例:输入:1->2->4, 1->3->4输出:1->1->2->3->4->4思路新建一个链表,将 l1,l2 中的值,相互比较,新的链表指针指向小的那个节点。当其中一个为空,另一个不为空的时候,直接指向那...原创 2018-08-29 08:43:25 · 171 阅读 · 0 评论 -
LeetCode 617. 合并二叉树
题目描述给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠。你需要将他们合并为一个新的二叉树。合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 NULL 的节点将直接作为新二叉树的节点。示例 1:输入: Tree 1 Tree 2 ...原创 2018-09-21 10:16:37 · 317 阅读 · 0 评论 -
LeetCode 104. 二叉树的最大深度
题目描述给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。说明: 叶子节点是指没有子节点的节点。示例:给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7返回它的最大深度 3 。思路递归判断左右子树哪个深度最大。class Solution {pub...原创 2018-09-21 10:22:50 · 146 阅读 · 0 评论 -
LeetCode 226. 翻转二叉树
题目描述翻转一棵二叉树示例:输入: 4 / \ 2 7 / \ / \1 3 6 9输出: 4 / \ 7 2 / \ / \9 6 3 1思路翻转二叉树的左右子树,然后递归class Solution {public: TreeNode* invertTree(Tre...原创 2018-09-21 10:38:21 · 242 阅读 · 0 评论 -
LeetCode 203. 删除链表中的元素
题目描述删除链表中等于给定值 val 的所有节点。示例:输入: 1->2->6->3->4->5->6, val = 6输出: 1->2->3->4-&a原创 2018-10-14 05:15:30 · 212 阅读 · 0 评论 -
Leetode 590. N叉树的后序遍历
题目描述给定一个 N 叉树,返回其节点值的后序遍历。例如,给定一个 3叉树 : !](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/12/narytreeexample.png) 返回其后序遍历: [5,6,3,2,4,1].代码class Solution {public: v...原创 2018-11-19 20:33:55 · 200 阅读 · 0 评论 -
LeetCode 589. N叉树的前序遍历
题目描述给定一个 N 叉树,返回其节点值的前序遍历。例如,给定一个 3叉树 :返回其前序遍历: [1,3,5,6,2,4]。代码class Solution {public: vector<int> preorder(Node* root) { vector<int> res;//存储后序遍历的结果 stack&am原创 2018-11-19 20:45:39 · 249 阅读 · 0 评论 -
LeetCode 559. N叉树的最大深度
题目描述给定一个 N 叉树,找到其最大深度。最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。例如,给定一个 3叉树 :我们应返回其最大深度,3。代码class Solution {public: int maxDepth(Node* root) { if(root == NULL) return 0; int deep = 0; fo...原创 2018-11-19 20:57:31 · 213 阅读 · 0 评论 -
LeetCode 84. Largest Rectangle in Histogram
题目描述Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where width of ...原创 2018-08-31 20:00:48 · 172 阅读 · 0 评论 -
LeetCode 74. Search a 2D Matrix
题目描述Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right. The first integer of ea...原创 2018-08-31 19:59:33 · 204 阅读 · 0 评论 -
7. Reverse Integer
题目描述Reverse digits of an integer.Example1: x = 123, return 321 Example2: x = -123, return -321思路一开始想的比较简单,直接循环取尾数*10。#include<iostream>using namespace std;int main(){ int n, count = 0; int原创 2017-01-10 10:38:56 · 266 阅读 · 0 评论 -
LeetCode 1. Two Sum 两数之和
题目描述Two Sum Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution.Example: Given n原创 2017-01-07 22:44:08 · 278 阅读 · 0 评论 -
LeetCode 9. Palindrome Number 回文数
题目描述Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints: Could negative integers be palindromes? (ie, -1)If you are thinking of converting the i原创 2017-05-26 23:33:10 · 223 阅读 · 0 评论 -
27. Remove Element
题目描述Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.The o原创 2017-06-22 17:17:32 · 219 阅读 · 0 评论 -
26. Remove Duplicates from Sorted Array
题目描述Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with原创 2017-06-22 18:49:56 · 234 阅读 · 0 评论 -
80. Remove Duplicates from Sorted Array II
题目描述Follow up for “Remove Duplicates”: What if duplicates are allowed at most twice?For example, Given sorted array nums = [1,1,1,2,2,3],Your function should return length = 5, with the first five el原创 2017-06-23 14:37:41 · 235 阅读 · 0 评论 -
66. Plus One
题目描述Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself.The digits ar原创 2017-06-25 17:56:03 · 257 阅读 · 0 评论 -
16. 3Sum Closest
题目描述Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exac原创 2017-07-31 22:42:22 · 167 阅读 · 0 评论 -
15. 3Sum
题目描述Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note: The solution set must not contain du原创 2017-07-31 22:52:55 · 182 阅读 · 0 评论 -
118. Pascal‘s Triangle
题目描述Given numRows, generate the first numRows of Pascal’s triangle.For example, given numRows = 5, Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]思路很明显,杨辉三角,在欧洲称为帕斯卡三角形。找规律: 1. 每原创 2017-07-26 11:51:30 · 286 阅读 · 0 评论 -
119. Pascal's Triangle II
题目描述Given an index k, return the kth row of the Pascal’s triangle.For example, given k = 3, Return [1,3,3,1].思路得到第k+1行的一整列的值。代码class Solution {public: vector<int> getRow(int numRows) { v原创 2017-07-26 15:32:02 · 255 阅读 · 0 评论 -
88. Merge Sorted Array
题目描述Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additi原创 2017-07-28 07:28:39 · 169 阅读 · 0 评论 -
LeetCode 189. 轮转数组 题解
题目描述给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。输入: [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]思路第一种方法将后 k 个.........原创 2018-08-14 15:38:09 · 174 阅读 · 0 评论 -
LeetCode 206. 反转链表
题目描述反转一个单链表。示例:输入: 1->2->3->4->5->NULL输出: 5->4->3->2->1->NULL思路使用三个指针遍历单链表,对每一个链接点的指针进行反转。p 和 q 指针进行反向,用 r 记录下一个节点,然后从前往后一直进行。原来的 head 变为尾指针,要令 head 的 ...原创 2018-08-21 11:00:32 · 177 阅读 · 0 评论 -
LeetCode 225. 用队列实现栈
题目描述使用队列实现栈的下列操作:push(x) – 元素 x 入栈pop() – 移除栈顶元素top() – 获取栈顶元素empty() – 返回栈是否为空思路创建两个队列 q1,q2入栈将元素 x 直接放入 q1 队列中。出栈也就是把 q1 的队尾元素出队列,由于队列只能从队头出队,因此先把 q1 中除了队尾元素的其他值存到 q2 中再把队尾元素......原创 2018-08-31 19:46:57 · 1174 阅读 · 0 评论 -
LeetCode 905. 按奇偶排序数组
题目描述给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素。你可以返回满足此条件的任何数组作为答案。示例:输入:[3,1,2,4]输出:[2,4,3,1]输出 [4,2,3,1],[2,4,1,3] 和 [4,2,1,3] 也会被接受。提示:1 &lt;= A.length &lt;= 50000 &lt;= A[i] &lt;= 50...原创 2018-12-27 08:13:36 · 131 阅读 · 0 评论