![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode solutions
文章平均质量分 72
wo111180611
这个作者很懒,什么都没留下…
展开
-
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 is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the f原创 2016-02-06 10:39:34 · 181 阅读 · 0 评论 -
100. 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. Solution 1原创 2016-02-06 10:46:06 · 155 阅读 · 0 评论 -
67. Add Binary
Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". Solution 1 3ms 90.69% public class Solution { public String addBinary(Str原创 2016-02-06 11:42:35 · 178 阅读 · 0 评论 -
19. 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-02-07 04:29:35 · 138 阅读 · 0 评论 -
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原创 2016-02-07 06:25:01 · 142 阅读 · 0 评论 -
1. Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, whe原创 2016-02-07 07:07:31 · 172 阅读 · 0 评论 -
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 convertin原创 2016-02-09 02:52:15 · 164 阅读 · 0 评论 -
234. Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time and O(1) space? Solution1 3ms 21.69 and this is O(1)space public class Solution { pu原创 2016-02-03 10:51:52 · 152 阅读 · 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: Elements in a triplet (a,b,c原创 2016-02-09 01:06:28 · 213 阅读 · 0 评论 -
53. 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-02-10 02:33:28 · 210 阅读 · 0 评论 -
12. Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Solution 1 11ms 25.74% public class Solution { public String intToRoman(in原创 2016-02-10 04:36:43 · 167 阅读 · 0 评论 -
23. Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Solution 1 PriorityQueue 10ms 51.15% public class Solution { public ListNode mergeKLists(Lis原创 2016-02-10 09:40:18 · 250 阅读 · 0 评论 -
46. Permutations
Given a collection of distinct numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. Solu原创 2016-02-10 11:31:21 · 145 阅读 · 0 评论 -
103. Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary原创 2016-02-10 12:02:13 · 127 阅读 · 0 评论 -
270: Closest Binary Search Tree Value
Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target. Solution 1 public class Solution { public int closestValue(TreeNode root, double原创 2016-02-10 12:23:37 · 211 阅读 · 0 评论 -
328. Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. You should try to do it in原创 2016-02-01 09:15:06 · 172 阅读 · 0 评论 -
79. Word Search
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原创 2016-02-14 12:36:52 · 193 阅读 · 0 评论 -
326. Power of Three
Given an integer, write a function to determine if it is a power of three. Follow up: Could you do it without using any loop / recursion? Recursivepublic class Solution { public boolean isPow原创 2016-02-01 09:32:24 · 193 阅读 · 0 评论 -
303. Range Sum Query - Immutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.public class NumArray { int sum []; public NumArray(int[] nums) { sum = new int [nu原创 2016-02-01 13:55:43 · 176 阅读 · 0 评论 -
299. Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint t原创 2016-02-01 14:02:08 · 136 阅读 · 0 评论 -
290. Word Pattern
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.原创 2016-02-02 11:25:53 · 186 阅读 · 0 评论 -
278. First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the原创 2016-02-02 13:45:07 · 173 阅读 · 0 评论 -
263. Ugly Number
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly sinc原创 2016-02-02 13:54:48 · 143 阅读 · 0 评论 -
258 Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has on原创 2016-02-02 14:24:41 · 166 阅读 · 0 评论 -
257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: ["1->2->5", "1->3"] Solutio原创 2016-02-02 14:57:39 · 167 阅读 · 0 评论 -
242. Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return false. Note: You may原创 2016-02-02 15:25:47 · 192 阅读 · 0 评论 -
236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two node原创 2016-02-03 04:31:21 · 195 阅读 · 0 评论 -
228. Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. Solution 1 straightforward way 1ms 3.27% public c原创 2016-02-03 14:29:02 · 149 阅读 · 0 评论 -
226. Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Solution 1 Recusive 1ms 20.55% public TreeNode invertTree(TreeNode root原创 2016-02-03 14:43:26 · 130 阅读 · 0 评论 -
206. Reverse Linked List
Solution 1 0ms 38.79 public class Solution { public ListNode reverseList(ListNode head) { if(head == null || head.next == null){ return head; } ListNode cur;原创 2016-02-03 15:36:05 · 119 阅读 · 0 评论 -
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 anot原创 2016-02-04 02:09:18 · 174 阅读 · 0 评论 -
168. Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB Solution原创 2016-02-04 04:37:39 · 177 阅读 · 0 评论 -
165. Compare Version Numbers
Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and co原创 2016-02-04 04:50:18 · 142 阅读 · 0 评论 -
160. 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-02-04 10:21:02 · 174 阅读 · 0 评论 -
155. Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get原创 2016-02-04 12:19:19 · 145 阅读 · 0 评论 -
125. Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a原创 2016-02-04 12:39:49 · 165 阅读 · 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] ] Solution 1 public class So原创 2016-02-04 15:20:32 · 131 阅读 · 0 评论 -
112. 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-02-04 15:55:50 · 146 阅读 · 0 评论 -
111. 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. Solution 1 DFS 1ms 12.55 pub原创 2016-02-05 02:16:11 · 171 阅读 · 0 评论 -
110. 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-02-05 02:47:49 · 172 阅读 · 0 评论