
LeetCode
CheeRok
这个作者很懒,什么都没留下…
展开
-
LeetCode题解: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题意:删除链表中某个节点解决思路:……代码:public class Solution原创 2015-08-26 20:04:02 · 447 阅读 · 0 评论 -
LeetCode题解:Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example:1 -> A2 -> B3 -> C...26 -> Z27 -> AA28 -> AB 题意:给定一个整数,按照给定的对应关系返回列标题解决思路:用一个大小为26的数组代表每个数原创 2015-08-26 19:40:26 · 500 阅读 · 0 评论 -
LeetCode题解:Excel Sheet Column Number
Related to question Excel Sheet Column TitleGiven a column title as appear in an Excel sheet, return its corresponding column number.For example:A -> 1B -> 2C -> 3...Z -> 26AA -> 27AB -> 28 题意:与原创 2015-08-26 19:42:46 · 484 阅读 · 0 评论 -
LeetCode题解:Count Primes
Count the number of prime numbers less than a non-negative number, n.题意:计算质数数量解决思路:按照质数性质求代码:public class Solution { public int countPrimes(int n) { boolean notPrime[] = new boolean[n + 2]; n原创 2015-08-26 20:19:18 · 527 阅读 · 0 评论 -
LeetCode题解: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 chara原创 2015-08-26 19:53:46 · 958 阅读 · 0 评论 -
LeetCode题解:Reverse Linked List
Reverse a singly linked list.题意:逆转单向链表解决思路:依次取得后一个节点,再改变指针指向就可以了代码:public class Solution { public ListNode reverseList(ListNode head) { return reverseListInt(head,null); } public Lis原创 2015-08-26 19:48:27 · 404 阅读 · 0 评论 -
LeetCode题解:Happy Number
Write an algorithm to determine if a number is “happy”.A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of i原创 2015-08-26 20:17:58 · 1258 阅读 · 0 评论 -
LeetCode题解:Symmetric Tree(有4种解法)
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 3But the f原创 2014-11-17 19:01:27 · 878 阅读 · 1 评论 -
LeetCode题解:Reverse Integer (3种解法)
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321题解:设计一个栈,能够实现出入栈,获得栈顶元素,而且能不断获得出入栈操作后栈内最小的元素。原创 2014-11-19 20:28:33 · 1594 阅读 · 0 评论 -
LeetCode题解:MinStack(三种解法)
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原创 2014-11-18 10:30:24 · 1269 阅读 · 0 评论 -
LeetCode题解: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 3, t原创 2015-08-12 10:16:56 · 549 阅读 · 0 评论 -
LeetCode题解: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?题意:给定一个单向链表,判断是否为回文解决思路:假设一个链表是回文,那么把链表分割为1…n/2,n/2+1…n两个部分,这两个部分肯定是相同的(把第二部分顺序逆转过来或者原创 2015-08-12 10:39:25 · 490 阅读 · 0 评论 -
LeetCode题解: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,#,#,15,7}, 3 / \ 9 20原创 2014-11-20 23:43:03 · 567 阅读 · 0 评论 -
LeetCode题解:Single Number最优解法
LeetCode解题笔记原创 2014-11-16 19:39:44 · 875 阅读 · 0 评论 -
LeetCode题解: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].Note: Could you optimize your algorithm to use only O(k) extra space?题意:返回给定k对应第k行的 Pascal 三角形原创 2015-08-12 10:13:35 · 522 阅读 · 0 评论 -
LeetCode题解:House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses原创 2015-08-26 20:25:47 · 609 阅读 · 0 评论 -
LeetCode题解: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 0000000000000原创 2015-08-26 20:27:20 · 450 阅读 · 0 评论 -
LeetCode题解: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 00111001011110原创 2015-08-26 20:35:16 · 453 阅读 · 0 评论 -
LeetCode题解: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 ↘原创 2015-08-26 20:44:58 · 646 阅读 · 0 评论 -
LeetCode题解: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 cons原创 2015-08-26 20:46:20 · 453 阅读 · 0 评论 -
LeetCode题解: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.题意:给定数组和整数k原创 2015-08-12 11:11:17 · 620 阅读 · 0 评论 -
LeetCode题解: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 assume the s原创 2015-08-12 10:03:34 · 631 阅读 · 0 评论 -
LeetCode题解:Implement Queue using Stacks
Implement the following operations of a queue using stacks.push(x) – Push element x to the back of queue.pop() – Removes the element from in front of queue.peek() – Get the front element.empty() –原创 2015-08-12 10:42:37 · 496 阅读 · 0 评论 -
LeetCode题解:Power of Two
Given an integer, write a function to determine if it is a power of two.题意:给定一个整数,判断是否为2的幂解决思路:如果一个整数n是2的幂,那么让n和n-1进行与运算必然为0(位运算)代码:public boolean isPowerOfTwo(int n) { return ((n & (n-1))==0 &原创 2015-08-12 10:47:17 · 433 阅读 · 0 评论 -
LeetCode题解: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”].题意:给定一个有序数组,数组内没有重复元素,返回一个代表数组内数据范围的数组。解决思路:从左往右遍历,当后一个元素与原创 2015-08-12 10:50:50 · 609 阅读 · 0 评论 -
LeetCode题解:Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 题意:将二叉树的左右子树互换解决思路:利用队列先进先出的特性改变左右子树代码:public TreeNode invertTree(原创 2015-08-12 10:55:58 · 481 阅读 · 0 评论 -
LeetCode题解:Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane.Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.Assume that the total area is nev原创 2015-08-12 11:06:02 · 668 阅读 · 0 评论 -
LeetCode题解:Count and Say解法
奇葩的读数方法~原创 2014-11-16 22:07:25 · 1775 阅读 · 0 评论 -
LeetCode题解: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] ]题意是:给定一个正整数numR原创 2015-08-12 10:08:25 · 594 阅读 · 0 评论 -
LeetCode题解:Implement Stack using Queues
Implement the following operations of a stack using queues.push(x) – Push element x onto stack.pop() – Removes the element on top of the stack.top() – Get the top element.empty() – Return whether t原创 2015-08-12 11:02:13 · 484 阅读 · 0 评论 -
LeetCode题解: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 differ by原创 2015-08-26 20:50:47 · 421 阅读 · 0 评论 -
LeetCode题解: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 between two原创 2015-08-12 10:33:29 · 455 阅读 · 0 评论 -
LeetCode题解: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 conta原创 2015-08-26 20:43:16 · 549 阅读 · 0 评论 -
LeetCode题解: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.题意:求二叉树的最小深度解决思路:找出最短路径,BFS代码:public class原创 2015-08-26 20:49:20 · 450 阅读 · 0 评论 -
LeetCode题解: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 = 2原创 2015-08-26 20:47:44 · 422 阅读 · 0 评论 -
LeetCode题解: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, where in原创 2015-08-28 10:40:30 · 1403 阅读 · 1 评论 -
LeetCode题解:Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.题意:给定一个字符串,找出最长的回文子串解题思路:Ma原创 2015-08-28 10:50:56 · 546 阅读 · 0 评论 -
LeetCode题解:Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).For example: Given binary tree {3,9,20,#,#,15,7},原创 2015-08-28 10:21:58 · 439 阅读 · 0 评论 -
LeetCode题解: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.题意:求二叉树最长路径题解:DFS代码:public class Solution {原创 2015-08-28 10:23:17 · 501 阅读 · 0 评论 -
LeetCode题解:Integer to Roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.题意:给定一个整数,将其转换为罗马数解题思路:按照转换关系换代码:import java.util.LinkedHashMap;public class Solution { pri原创 2015-08-28 10:54:26 · 456 阅读 · 0 评论