LeetCode
海涛anywn
不忘初心 方得始终
展开
-
1. 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.public class Solution { pu原创 2016-05-14 14:49:32 · 299 阅读 · 0 评论 -
202. 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原创 2016-06-05 18:28:20 · 259 阅读 · 0 评论 -
203. 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/** * Definition for sing原创 2016-06-05 18:29:08 · 254 阅读 · 0 评论 -
217. Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element原创 2016-06-05 18:30:13 · 248 阅读 · 0 评论 -
226. Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1/** * Definition for a binary tree node. * public class TreeNode { *原创 2016-06-05 18:31:17 · 225 阅读 · 0 评论 -
231. Power of Two
Given an integer, write a function to determine if it is a power of two.public class Solution { public boolean isPowerOfTwo(int n) { return n > 0 && (n & (n - 1)) == 0; }}原创 2016-06-05 18:32:23 · 257 阅读 · 0 评论 -
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 value原创 2016-06-05 18:33:33 · 243 阅读 · 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.public class Solutio原创 2016-06-05 18:34:18 · 244 阅读 · 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-06-05 18:35:18 · 270 阅读 · 0 评论 -
292. Nim Game
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the原创 2016-06-05 18:36:06 · 259 阅读 · 0 评论 -
326. Power of Three
Given an integer, write a function to determine if it is a power of three.public class Solution { public boolean isPowerOfThree(int n) { double res = Math.log(n)/Math.log(3); ret原创 2016-06-05 18:36:50 · 252 阅读 · 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-06-05 18:37:28 · 402 阅读 · 0 评论 -
5. 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.class Solution {p原创 2016-07-10 10:40:37 · 686 阅读 · 0 评论 -
二维数组中的查找
在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。public boolean Find(int [][] array,int target) { int len = array.length-1; int i = 0;原创 2016-07-10 13:47:16 · 281 阅读 · 0 评论 -
重建二叉树
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。/** * Definition for binary tree * public class TreeNode { * int val; *原创 2016-07-10 15:47:25 · 404 阅读 · 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-06-05 18:27:07 · 389 阅读 · 0 评论 -
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./** * Definition for a binary tree原创 2016-06-05 18:26:15 · 280 阅读 · 0 评论 -
2. Add Two Numbers
ou are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linke原创 2016-05-14 14:51:43 · 306 阅读 · 0 评论 -
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", which the length is 3.Given "bbbbb", the answer is "原创 2016-05-14 14:52:58 · 220 阅读 · 0 评论 -
7. Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321public class Solution { public int reverse(int x) { int r = 0; while (x!=0) {原创 2016-05-14 15:02:15 · 221 阅读 · 0 评论 -
9. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.class Solution {public: bool isPalindrome(int x) { if(x < 0) return false; int div = 1;原创 2016-05-14 15:03:33 · 291 阅读 · 0 评论 -
13. Roman to Integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.public class Solution { public int romanToInt(String s) { int res = 0; /原创 2016-06-01 18:32:05 · 324 阅读 · 0 评论 -
19. Remove Nth Node From End of List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* re原创 2016-06-01 18:33:15 · 283 阅读 · 0 评论 -
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./** * Definition for singly-linked list. * public cla原创 2016-06-01 18:34:30 · 204 阅读 · 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原创 2016-06-01 18:36:23 · 328 阅读 · 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.原创 2016-06-01 18:37:21 · 232 阅读 · 0 评论 -
28. Implement strStr()
public class Solution { public int strStr(String haystack, String needle) { /** int l1 = haystack.length(); int l2 = needle.length(); char[] haystack1 = haystack.toCharArray();原创 2016-06-01 18:38:26 · 213 阅读 · 0 评论 -
38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as原创 2016-06-01 18:39:34 · 232 阅读 · 0 评论 -
66. 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.import java.math.Big原创 2016-06-01 18:41:01 · 264 阅读 · 0 评论 -
67. Add Binary
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".public class Solution { public String addBinary(String a, String b) {原创 2016-06-01 18:41:49 · 291 阅读 · 0 评论 -
70. Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?public class Solution { pu原创 2016-06-01 18:42:40 · 301 阅读 · 0 评论 -
旋转有序数组中找最小值
O(n)的算法就不说了,这题主要考查的是 O(logn)的算法。有序数组容易想到使用二分查找解决,这题就是在二分基础上做一些调整。数组只有一次翻转,可以知道原有序递增数组被分成两部分,这俩部分都是有序递增的(这题只需要考虑有序数组的递增情况)。假如翻转后的数组以第 x 个结点分为两部分 A[0..x] 和 A[x+1..n]。则 A[0..x] 这一段是有序递增的, A[x+1..原创 2016-07-27 09:37:15 · 1945 阅读 · 0 评论