![](https://img-blog.csdnimg.cn/20201014180756926.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
文章平均质量分 61
candy_crush
胡编乱写
展开
-
Valid Palindrome
题目:判断字符串是否是回文字符串,只考虑字符串中的数字和字符,不考虑标点符号例如:"A man, a plan, a canal: Panama"是回文字符串"race a car" 不是回文字符串方法1:使用额外的内存空间 注意:测试时输入的字符串是带""引号的public class Solution { public boolean isPalindrome(S原创 2016-10-06 14:55:49 · 247 阅读 · 0 评论 -
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.求二叉树的最短深度解法一:深度优先遍历DFS/**原创 2016-09-17 22:34:43 · 177 阅读 · 0 评论 -
Binary Tree Level Order Traversal I和II 层次遍历二叉树
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 2原创 2016-09-17 21:13:19 · 308 阅读 · 0 评论 -
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原创 2016-09-17 18:07:50 · 237 阅读 · 0 评论 -
Same Tree
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.判断两棵二叉树是否相原创 2016-09-17 16:34:06 · 171 阅读 · 0 评论 -
Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5题意:去除链表中的目标元素。链表中的值可能有重复原创 2016-09-05 10:11:22 · 183 阅读 · 0 评论 -
Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ↘原创 2016-09-05 09:47:17 · 134 阅读 · 0 评论 -
Linked List Cycle
Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?题意:判断链表中是否有环思路:采用快慢指针的方法,快指针走两步,慢指针走一步,如果有环,则总有相遇的时候即快慢指针相等的时候/** * Definitio原创 2016-09-04 23:36:19 · 146 阅读 · 0 评论 -
Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. Y原创 2016-09-04 22:14:10 · 234 阅读 · 0 评论 -
Merge Two Sorted Lists
Merge 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.合并两个有序链表,返回合并后的有序链表头由于两个数去链表可能为空,所以增加一个dummy节点,时dummy原创 2016-09-04 16:59:47 · 162 阅读 · 0 评论 -
Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the原创 2016-09-04 13:46:42 · 182 阅读 · 0 评论 -
Triangle
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], [原创 2016-09-03 10:57:16 · 207 阅读 · 0 评论 -
Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.找出数组中元素个数超过n/3的元素。解题思路: 忘了在哪看见过这个的数学证明,数组中超原创 2016-09-03 09:50:36 · 186 阅读 · 0 评论 -
Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example,Given nums = [0, 1, 3] return 2.数组包含n个不同的数,这些数来自0, 1, 2, ..原创 2016-09-02 23:15:23 · 246 阅读 · 0 评论 -
Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.求二叉树的最大深度,采用深度优先遍历原则/** * Def原创 2016-09-17 21:52:08 · 314 阅读 · 0 评论 -
Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value原创 2016-09-05 17:00:07 · 192 阅读 · 0 评论 -
Balanced Binary Tree
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 diffe原创 2016-09-18 10:43:31 · 273 阅读 · 0 评论 -
Palindrome Number
题目:判断一个数是否是回文数字 回文数字就是正着读和反着读一样,比如12321.要求:不能使用额外的内存空间思路:反转数字,然后和反转前的数字对比,相等即返回truepublic class Solution { public boolean isPalindrome(int x) { //负数肯定不是回文数字 if (x < 0) {原创 2016-10-06 13:32:42 · 248 阅读 · 0 评论 -
Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321要求:不使用额外的内存空间。反转之后考虑越界问题。public class Solution { public int reverse(int x) { long res原创 2016-10-05 23:45:22 · 235 阅读 · 0 评论 -
Sort List
题意:对链表进行排序,要求的时间复杂度为O(n log n)。解题思路:O(nlogn)的排序有快速排序、归并排序、堆排序。这里使用归并排序,归并排序的基本思想是:找到链表的中间节点,然后递归对前半部分和后半部分分别进行归并排序,最后对两个以排好序的链表进行Merge。/** * Definition for singly-linked list. * public class L原创 2016-09-08 13:45:53 · 185 阅读 · 0 评论 -
Insertion Sort List
题意:实现链表的插入排序 插入排序是一种O(n^2)复杂度的算法,就是每次循环找到一个元素在当前排好的结果中相对应的位置,然后插进去,经过n次迭代之后就得到排好序的结果了。/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next;原创 2016-09-08 10:40:04 · 163 阅读 · 0 评论 -
Reorder List
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reorder it to原创 2016-09-07 23:51:57 · 282 阅读 · 0 评论 -
Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome.题意: 判断一个链表是否是回文链表,回文链表就是无论从左读还是从右读都一样例如 1->2->3->2->1 需要注意的是空链表和只有一个节点的链表都是回文链表解题思路:反转链表,将链表后半段原地翻转,再将前半段、后半段依次比较,判断是否相等该方法时间复杂度为原创 2016-09-05 16:42:15 · 188 阅读 · 0 评论 -
Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Note: Do not modify the linked list.Follow up:Can you solve it without using extra space? 题原创 2016-09-07 22:33:04 · 190 阅读 · 0 评论 -
Reverse Linked List I,II
Reverse a singly linked list.题意:反转链表方法一:遍历思路:使用辅助节点dummy。初始化时dummy..next指向head,并从第二个节点开始遍历,把遍历过的节点一次插入在dummy节点之后,最后返回dummy.next即可/** * Definition for singly-linked list. * public class Lis原创 2016-09-05 12:40:29 · 189 阅读 · 0 评论 -
Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of原创 2016-09-07 09:55:40 · 233 阅读 · 0 评论 -
Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.题意:给定一个链表和一个k值,将链表向右旋转K个位置/原创 2016-09-06 18:02:57 · 181 阅读 · 0 评论 -
Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1题意:反转一颗二叉树解题思路:直接获取根节点的左右节点,然后进行交换即可。方法一使用二叉树层次遍历。/** * De原创 2016-09-18 22:56:45 · 184 阅读 · 0 评论 -
Remove Duplicates from Sorted List
题意:输入一个有序链表,删除重复元素,原创 2016-09-04 23:15:17 · 160 阅读 · 0 评论 -
Path Sum
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原创 2016-09-18 11:26:05 · 195 阅读 · 0 评论 -
Plus One
Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.题意:一个非负整数按位存储于一个数组中,原创 2016-09-01 15:35:33 · 176 阅读 · 0 评论 -
Find Peak Element
A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain multiple peaks, in原创 2016-09-02 22:38:39 · 165 阅读 · 0 评论 -
Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element原创 2016-09-02 21:54:41 · 169 阅读 · 0 评论 -
3Sum
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原创 2016-08-18 16:48:07 · 175 阅读 · 0 评论 -
Combination Sum I,II,III
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited numb原创 2016-08-20 20:22:33 · 215 阅读 · 0 评论 -
Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1] ha原创 2016-08-20 21:16:12 · 158 阅读 · 0 评论 -
Unique Paths和Unique Paths II 路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the原创 2016-08-22 17:03:55 · 230 阅读 · 0 评论 -
Spiral Matrix 螺旋矩阵
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:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]原创 2016-08-20 22:51:35 · 267 阅读 · 0 评论 -
Two Sum and Two Sum II
给定一个整形的数组,找出其中的两个数使其和为某个指定的值,并返回这两个数的下标,假设数组元素的值各不相同。原创 2016-08-16 23:38:28 · 244 阅读 · 0 评论 -
Next Permutation下一个全排列序列
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible原创 2016-08-19 16:38:28 · 414 阅读 · 0 评论