[Leetcode] Jump Game 跳跃游戏

2015-08-25
阅读 2 分钟
7.5k
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example: A = [2,3,1,1,4], return true. A = [3,2,1,0,4], retu...

[Leetcode] Missing Number and Missing First Positive 找缺失数

2015-08-25
阅读 3 分钟
7.7k
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. Note: Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?

[Leetcode] Find Minimum in Rotated Sorted Array 找旋转有序数组的最小值

2015-08-25
阅读 4 分钟
4.7k
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in the array.

[Lintcode] Find Peak Element 找峰值

2015-08-25
阅读 5 分钟
8.8k
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 that case return the index to any one of the peaks is fine. You may imagine that num[-1] = num[n] = -∞....

[Leetcode] Reverse Bits 反转位

2015-08-24
阅读 2 分钟
10.5k
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 00111001011110000010100101000000). Follow up: If this function is called many times, how would you optimize it?

[Leetcode] Best Time to Buy and Sell Stock 买卖股票的最佳时机

2015-08-24
阅读 7 分钟
12.5k
Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

[Leetcode] Happy Number 快乐数

2015-08-23
阅读 2 分钟
7.5k
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 its digits, and repeat the process until the number equals 1 (where it will stay), or it loops e...

[Leetcode] Invert Binary Tree 翻转二叉树

2015-08-23
阅读 2 分钟
5.6k
Invert a binary tree. {代码...} to {代码...} Trivia: This problem was inspired by this original tweet by MaxHowell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.

[Leetcode] Distinct Subsequences 不同顺序字串

2015-08-23
阅读 2 分钟
5.7k
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characterswithout disturbing the relative positions of the remaining characters. (ie, "ACE...

[Leetcode] Valid Parentheses 验证有效括号对

2015-08-23
阅读 2 分钟
6.6k
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 valid but "(]" and "([)]" are not.

[Leetcode] Maximum Subarray 子序列最大和

2015-08-23
阅读 2 分钟
7k
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] has the largest sum = 6.

[Leetcode] Longest Valid Parentheses 最长有效括号对

2015-08-23
阅读 3 分钟
9.9k
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is ")()())", where the longest valid parentheses substring is "()(...

[Leetcode] Power of Two and Power of Four 二之幂四之幂

2015-08-23
阅读 2 分钟
5.3k
Given an integer, write a function to determine if it is a power of two.

[Leetcode] Ugly Number 丑陋数

2015-08-23
阅读 2 分钟
6.1k
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 since it includes another prime factor 7. Note that 1 is typically treated as an ugly number.

[Leetcode] Surrounded Regions 找出被包围的区域

2015-08-22
阅读 4 分钟
4k
Given a 2D board containing 'X' and 'O', capture all regionssurrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surroundedregion. For example, {代码...} After running your function, the board should be: {代码...}

[Leetcode] Populating Next Right Pointers in Each Node 二叉树Next指针

2015-08-22
阅读 7 分钟
4.9k
Given a binary tree {代码...} Populate each next pointer to point to its next right node. If thereis no next right node, the next pointer should be set to NULL. Initially, all next pointers are set to NULL.For example, Given the following perfect binary tree, {代码...} After calling your function...

[Leetcode] Sum Root to Leaf Numbers 累加叶子节点

2015-08-22
阅读 1 分钟
3.1k
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the total sum of all root-to-leaf numbers.

[Leetcode] Binary Tree Paths 二叉树路径

2015-08-22
阅读 3 分钟
15.2k
Root To Leaf Binary Tree Paths Given a binary tree, return all root-to-leaf paths. 递归法 复杂度 时间 O(b^(h+1)-1) 空间 O(h) 递归栈空间 对于二叉树b=2 思路 简单的二叉树遍历,遍历的过程中记录之前的路径,一旦遍历到叶子节点便将该路径加入结果中。 代码 {代码...} 2018/2 {代码...} Node to Node Binary Tre...

[Leetcode] Number of 1 Bits 一的位数

2015-08-22
阅读 1 分钟
5.2k
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 00000000000000000000000000001011, so the function should return 3.

[Leetcode] Palindrome Number 回文数

2015-07-18
阅读 2 分钟
4.8k
Determine whether an integer is a palindrome. Do this without extra space.

[Leetcode] String to Integer (atoi) 字符串转整数

2015-07-16
阅读 2 分钟
5.7k
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You ...

[Leetcode] Reverse Integer 反转整数

2015-07-16
阅读 2 分钟
10k
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321

[Leetcode] Longest Palindromic Substring 最长回文子字符串

2015-07-15
阅读 6 分钟
15.4k
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.

[Leetcode] Median of Two Sorted Arrays 有序数组中位数

2015-07-14
阅读 4 分钟
13.1k
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).有两个有序数组nums1和nums2,他们的大小各是m和n,请找出这两个数组所有数的中位数,总得时间复杂度不超过O(log(m+n))

[Leetcode] Longest Substring Without Repeating Characters 最长唯一子串

2015-07-14
阅读 2 分钟
4.1k
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

[Leetcode] Add Two Numbers 链表数相加

2015-07-14
阅读 4 分钟
13.9k
You 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 linked list.Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)Output: 7 -> 0 -> 8

[Leetcode] Two Sum 两数和

2015-07-14
阅读 4 分钟
10k
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 index1 must be less than index2. Please note that your returned answers (both index1 and index2) a...