198. 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 have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Read More

53. Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

Read More

121. Best Time to Buy and Sell Stock

Say you have an array for which the ith element is the price of a given stock on day i.

Read More

70. Climbing Stairs

You are climbing a stair case. It takes n steps to reach to the top.

Read More

九章 - 动态规划笔记

什么情况下使用动态规划

满足下面三个条件之一,则极有可能使用动态规划 90% - 95%:

Read More

136. Single Number

位运算奇妙无穷啊。这个题目巧妙的运用了xor位运算。异或具有以下性质

Read More

151. Reverse Words in a String

Given an input string, reverse the string word by word.

Read More

167. Two Sum II - Input array is sorted

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.

Read More

1. Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

Read More

233. Number of Digit One

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.

Read More

302. Smallest Rectangle Enclosing Black Pixels

An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally and vertically. Given the location (x, y) of one of the black pixels, return the area of the smallest (axis-aligned) rectangle that encloses all black pixels.

Read More

320. Generalized Abbreviation

Write a function to generate the generalized abbreviations of a word.

Read More

413. Arithmetic Slices

A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.

Read More

412. Fizz Buzz

Write a program that outputs the string representation of numbers from 1 to n.

Read More

241. Different Ways to Add Parentheses

Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *.

Read More

284. Peeking Iterator

Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation – it essentially peek() at the element that will be returned by the next call to next().

Read More

392. Is Subsequence

Given a string s and a string t, check if s is subsequence of t.

Read More

348. Design Tic-Tac-Toe

Design a Tic-tac-toe game that is played between two players on a n x n grid.

Read More

245. Shortest Word Distance III

This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as word2.

Read More

364. Nested List Weight Sum II

Given a nested list of integers, return the sum of all integers in the list weighted by their depth.

Read More

319. Bulb Switcher

There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it’s off or turning off if it’s on). For the ith round, you toggle every i bulb. For the nth round, you only toggle the last bulb. Find how many bulbs are on after n rounds.

Read More

415. Add Strings

Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2.

Read More

296. Best Meeting Point

A group of two or more people wants to meet and minimize the total travel distance. You are given a 2D grid of values 0 or 1, where each 1 marks the home of someone in the group. The distance is calculated using Manhattan Distance,
where distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|.

Read More

311. Sparse Matrix Multiplication

Given two sparse matrices A and B, return the result of AB.

Read More

357. Count Numbers with Unique Digits

Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.

Read More

362. Design Hit Counter

Design a hit counter which counts the number of hits received in the past 5 minutes.

Read More

369. Plus One Linked List

Given a non-negative number represented as a singly linked list of digits, plus one to the number.

Read More

370. Range Addition

Assume you have an array of length n initialized with all 0’s and are given k update operations.

Read More

281. Zigzag Iterator

Given two 1d vectors, implement an iterator to return their elements alternately.

Read More

249. Group Shifted Strings

Given a string, we can “shift” each of its letter to its successive letter, for example: “abc” -> “bcd”. We can keep “shifting” which forms the sequence:

Read More

295. Find Median from Data Stream

Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.

Read More

397. Integer Replacement

Given a positive integer n and you can do operations as follow:

Read More

396. Rotate Function

Given an array of integers A and let n to be its length.

Read More

406. Queue Reconstruction by Height

Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this person who have a height greater than or equal to h. Write an algorithm to reconstruct the queue.

Read More

382. Linked List Random Node

Given a singly linked list, return a random node’s value from the linked list. Each node must have the same probability of being chosen.

Read More

398. Random Pick Index

Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array.

Read More

393. UTF-8 Validation

A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:

Read More

201. Bitwise AND of Numbers Range

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.

Read More

403. Frog Jump

A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.

Read More

356. Line Reflection

Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the given points.

Read More

408. Valid Word Abbreviation

Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation.

Read More

405. Convert a Number to Hexadecimal

Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.

Read More

366. Find Leaves of Binary Tree

Given a binary tree, collect a tree’s nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty.

Read More

Java Tips

对Java还不是太熟悉,有些用法,用过了,过后就记不太清了。把用过的小技巧总结一下。

Read More

307. Range Sum Query - Mutable

Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.

Read More

Binary Indexed Tree

我们有时候会遇到这样的问题:
有一个数组arr[0 … n - 1]. 我们需要做下面的事情:

Read More

402. Remove K Digits

Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.

Read More

404. Sum of Left Leaves

Find the sum of all left leaves in a given binary tree.

Read More

410. Split Array Largest Sum

Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays.

Read More

401. Binary Watch

A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).

Read More