- 浏览: 88830 次
最新评论
-
hcx2013:
mircle 写道Given nums = [1, 2, 1, ...
Single Number III -
mircle:
Given nums = [1, 2, 1, 3, 2, 5, ...
Single Number III -
hcx2013:
恩,可以,代码更简洁
空格替换 -
QuarterLifeForJava:
这样可以吗?
public static void main( ...
空格替换 -
hcx2013:
这个2是指多出2位,按你的理解可以这么改:int len = ...
空格替换
文章列表
Spiral Matrix II
- 博客分类:
- leetcode
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,Given n = 3,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
public class Solution {
public int[][] generateMatrix(int n) {
int[][] res = ...
Length of Last Word
- 博客分类:
- leetcode
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.
Note: A word is defined as a character sequence consists of non-space characters only.
For example, Given s = "Hell ...
Insert Interval
- 博客分类:
- leetcode
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
You may assume that the intervals were initially sorted according to their start times.
Example 1:Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].
Example 2:Given [1,2],[ ...
Merge Intervals
- 博客分类:
- leetcode
Given a collection of intervals, merge all overlapping intervals.
For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18].
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Int ...
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], ret ...
Spiral Matrix
- 博客分类:
- leetcode
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
For example,Given the following matrix:
public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<>();
...
Maximum Subarray
- 博客分类:
- leetcode
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.
click to show more practice.
public class Solution {
public int maxS ...
N-Queens II
- 博客分类:
- leetcode
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
public class Solution {
private int num = 0;
public int totalNQueens(int n) {
solve(0, n, new int[n]);
return num;
}
private void solve(int i, ...
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
Given an integer n, return all distinct solutions to the n-queens puzzle.
Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' bot ...
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
public class Solution {
public List<String> anagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<String, List<String>>( ...
Rotate Image
- 博客分类:
- leetcode
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:Could you do this in-place?
public class Solution {
public void rotate(int[][] matrix) {
int tR = 0;
int tC = 0;
int dR = matrix.length-1;
int ...
Permutations II
- 博客分类:
- leetcode
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1].
public class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
...
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab",Return
[
["aa","b"],
["a","a","b"]
]
public class ...
Single Number II
- 博客分类:
- leetcode
Given an array of integers, every element appears three times except for one. Find that single one.
Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
public class Solution {
public int singleNumber(int[] nums) {
int[] a ...
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 the top element.
getMin() -- Retrieve the minimum element in the stack.
class MinStack {
...