LeetCode
文章平均质量分 54
Carl_Hugo
Knowledge is Power
展开
-
[LeetCode]226. Invert Binary Tree
题目描述:Invert a binary tree. 4 / \ 2 7/ \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1解题思路1:将一个二叉树的左右子树逆转。通过层序遍历将二叉树的节点放入队列中,并通过出队来交换该节点的左右子树public TreeNode invertTree(Tre原创 2017-10-17 15:39:03 · 145 阅读 · 0 评论 -
[LeetCode]219. Contains Duplicate II
题目描述: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 difference between i and j is at most k. 分原创 2017-10-17 17:15:11 · 145 阅读 · 0 评论 -
[LeetCode]203. Remove Linked List Elements
题目描述:Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6 Return: 1 –> 2 –> 3 –> 4 –> 5 分析:删除链表中特定值的链表节点原创 2017-10-18 10:58:19 · 138 阅读 · 0 评论 -
[LeetCode]206. Reverse Linked List
题目描述:Reverse a singly linked list. 分析:将一个链表原地逆转解题思路:将头结点与之后的节点断开,将之后的节点使用头插法连接到头结点后面。public ListNode reverseList(ListNode head) { if(head==null)return null; //考虑到head在LeetCode中为实际存原创 2017-10-18 11:10:16 · 158 阅读 · 0 评论 -
[LeetCode]204. Count Primes
Description: Count the number of prime numbers less than a non-negative number, n. 分析:返回n范围内素数的个数解题思路:使用打表法,找出1~n中的素数,之后遍历统计。以空间换时间。打表思路:判断当前数是否为素数,从2开始。如果当前数为素数,则count++。内存循环当前素数乘之后必定为非素数,循环结原创 2017-10-18 13:01:06 · 167 阅读 · 0 评论 -
[LeetCode]189. Rotate Array
题目描述:Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 分析:将数组元素循环右移k个单位解题思路:题意是将数组元素循环右移k个单元,可以原创 2017-10-18 14:04:13 · 139 阅读 · 0 评论 -
[LeetCode]172. Factorial Trailing Zeroes
题目描述:Given an integer n, return the number of trailing zeroes in n!. 分析:返回n!后面的尾0个数解题思路:将n除以5后的结果累加public int trailingZeroes(int n) { int result = 0; while(n>0){ n=n/5;原创 2017-10-18 15:42:46 · 153 阅读 · 0 评论 -
[LeetCode]191. Number of 1 Bits
题目描述:Write a function that takes an unsigned integer and returns the number of ’1’ bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11’ has binary representation 000原创 2017-10-18 15:52:47 · 146 阅读 · 0 评论 -
[LeetCode]190. Reverse Bits
题目描述:Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111原创 2017-10-18 18:53:07 · 146 阅读 · 0 评论 -
[LeetCode]168. Excel Sheet Column Title
题目描述:Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 分析:根据数字返回在excel表格中的表示 1 -> A 2 -> B 3 -> C … 26 -> Z原创 2017-10-18 19:33:33 · 143 阅读 · 0 评论 -
[LeetCode]171. Excel Sheet Column Number
题目描述:Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: 分析:返回excel中对应的列表数的大小解题思路:类似于将26进制的数原创 2017-10-18 19:04:01 · 178 阅读 · 0 评论 -
[LeetCode]169. 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 al原创 2017-10-18 21:05:43 · 202 阅读 · 0 评论 -
[LeetCode]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原创 2017-10-19 09:40:50 · 169 阅读 · 0 评论 -
[LeetCode]136. Single Number
题目描述: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without原创 2017-10-19 10:19:20 · 140 阅读 · 0 评论 -
[LeetCode]141. 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? 分析:判断在一个链表中是否存在环解题思路:设置两个指针, 其中一个指针每次走一步,另外一个指针每次走两步。如果两个指针相遇,则表示有环。如果当其原创 2017-10-19 14:03:37 · 145 阅读 · 0 评论 -
[LeetCode]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原创 2017-10-19 14:46:22 · 168 阅读 · 0 评论 -
[LeetCode]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原创 2017-10-27 10:31:11 · 166 阅读 · 0 评论 -
[LeetCode]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. 分析:查找一棵二叉树从根节点到叶子节点的最短距离解题思原创 2017-10-19 15:55:55 · 177 阅读 · 0 评论 -
[LeetCode]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原创 2017-10-27 10:34:15 · 186 阅读 · 0 评论 -
[LeetCode]21. 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.解题思路:已知两个链表有序,可以利用两个指针,对两个链表依次遍历。将较小的指针所指向的节点添加到新的链表中。原创 2017-10-27 12:25:20 · 180 阅读 · 0 评论 -
[LeetCode]20. Valid Parentheses
题目描述:Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid. The brackets must close in the correct order, “()” and “()[]{}” are all va原创 2017-10-27 12:27:43 · 192 阅读 · 0 评论 -
[LeetCode]24. 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原创 2017-10-27 12:49:11 · 209 阅读 · 0 评论 -
[LeetCode]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 bi原创 2017-10-19 16:11:40 · 167 阅读 · 0 评论 -
[LeetCode]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原创 2017-10-20 10:24:58 · 179 阅读 · 0 评论 -
[LeetCode]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]. 解题思路:先求出pascal矩阵,在返回第i行的结果。public List<Integer> getRow(int rowIndex){原创 2017-10-20 12:55:15 · 192 阅读 · 0 评论 -
[LeetCode]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. 分析:判原创 2017-10-20 13:55:38 · 167 阅读 · 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 / \ /原创 2017-10-20 17:01:27 · 145 阅读 · 0 评论 -
[LeetCode]104. 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. 分析:查找一个二叉树的最大深度解题思路:求二叉树的深度原创 2017-10-20 17:04:14 · 171 阅读 · 0 评论 -
[LeetCode]102. Binary Tree Level Order Traversal
题目描述: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],原创 2017-10-20 20:04:42 · 189 阅读 · 0 评论 -
[LeetCode]88. Merge Sorted Array
题目描述:Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. 分析:将两个有序的数组合并到一个有序的数组中解题思路1:已知两个数组有序,利用两个指针将两个数组从头到尾遍历,将两个数组中较小的那个元素优先给第三个数组 ,完成合并。再将第三个数组赋给nums1[原创 2017-10-20 20:39:23 · 150 阅读 · 0 评论 -
[LeetCode]83. Remove Duplicates from Sorted List
题目描述: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, r原创 2017-10-20 20:55:49 · 154 阅读 · 0 评论 -
[LeetCode]66. Plus One
题目描述:Given a non-negative number represented as an array of digits, plus one to the number. You may assume the integer do not contain any leading zero, except the number 0 itself. The digits ar原创 2017-10-20 21:36:10 · 188 阅读 · 0 评论 -
[LeetCode]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.原创 2017-10-20 21:52:05 · 156 阅读 · 0 评论 -
[LeetCode]28. Implement strStr()
题目描述:Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 分析:返回子串在字符串中第一次出现的位置解题思路:题目要求返回子串在父串第一次出现的位置。设置两个指针,一个指向父字符串一个原创 2017-10-23 09:38:10 · 182 阅读 · 0 评论 -
[LeetCode]237. 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 v原创 2017-10-13 18:32:15 · 357 阅读 · 0 评论 -
[LeetCode]231. Power of Two-判断一个整数是否是2的幂次方
题目描述:Given an integer, write a function to determine if it is a power of two.思路一: 位运算与1相与,如果结果为1则为2的幂次方 public boolean isPowerOfTwo(int n) { if(n<=0)return false; int count = 0;原创 2017-10-13 18:37:44 · 343 阅读 · 0 评论 -
[LeetCode]58. Length of Last Word
题目描述: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. 分析:返回一个长传字符串原创 2017-10-23 14:22:29 · 184 阅读 · 0 评论 -
[LeetCode]235. Lowest Common Ancestor of a Binary Search Tree
题目描述:Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined b原创 2017-10-13 22:28:57 · 150 阅读 · 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 wit原创 2017-10-14 14:25:14 · 225 阅读 · 0 评论 -
[LeetCode]441. Arranging Coins
题目描述: You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full staircase rows that can be for原创 2017-10-14 17:09:16 · 174 阅读 · 0 评论