Java 【 LeetCode】79. Word Search 问题描述 https://leetcode.com/problems/word-search/#/description Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell,
Java 【LeetCode】78. Subsets 问题描述 Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not contain duplicate subsets. For example, If nums = [1,2,3], a solution is: [ [3], [1]
Java 【LeetCode】77. Combinations 问题描述 https://leetcode.com/problems/combinations/#/description Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example, If n = 4 and k = 2, a
Java 【LeetCode】75. Sort Colors 问题描述 https://leetcode.com/problems/sort-colors/#/description Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in
Java 【LeetCode】74. Search a 2D Matrix 问题描述 https://leetcode.com/problems/search-a-2d-matrix/#/description Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: * Integers in each row are
Java 【LeetCode】 73. Set Matrix Zeroes 问题描述 https://leetcode.com/problems/set-matrix-zeroes/#/description Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 给定一个矩阵,如果一个元素为0,则其所在行和所在列都置为0.
Java 【LeetCode】72. Edit Distance 问题描述 https://leetcode.com/problems/edit-distance/#/description Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You
Java 【LeetCode】71. Simplify Path 问题描述 https://leetcode.com/problems/simplify-path/#/description Given an absolute path for a file (Unix-style), simplify it. For example path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" Corner Cases: * Did you consider the
Java 【LeetCode】69. Sqrt(x) 问题描述 https://leetcode.com/problems/sqrtx/#/description Implement int sqrt(int x). Compute and return the square root of x. 算法 使用折半查找即可,但要注意整型溢出 代码 public int mySqrt(int x) { int left = 1, right
Java 【LeetCode】68. Text Justification 问题描述 https://leetcode.com/problems/text-justification/#/description Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right)
Java 【LeetCode】64. Minimum Path Sum 问题描述 https://leetcode.com/problems/minimum-path-sum/#/description Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers
Java 【LeetCode】63. Unique Paths II 问题描述 https://leetcode.com/problems/unique-paths-ii/#/description Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty
Java 【LeetCode】62. Unique Paths 问题描述 https://leetcode.com/problems/unique-paths/#/description A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either
Java 【LeetCode】60. Permutation Sequence 问题描述 https://leetcode.com/problems/permutation-sequence/#/description he set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the
Java 【LeetCode】59. Spiral Matrix II 问题描述 https://leetcode.com/problems/spiral-matrix-ii/#/description Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For example, Given n = 3, You should
Java 【LeetCode】57. Insert Interval 问题描述 https://leetcode.com/problems/insert-interval/#/description 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
Java 【LeetCode】56. Merge Intervals 问题描述 https://leetcode.com/problems/merge-intervals/#/description 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,
Java 【LeetCode】55. Jump Game 问题描述 https://leetcode.com/problems/jump-game/#/description 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
Java 【LeetCode】54. Spiral Matrix 问题描述 https://leetcode.com/problems/spiral-matrix/#/description 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
Java 【LeetCode】53. Maximum Subarray 问题描述 https://leetcode.com/problems/maximum-subarray/#/description 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,
算法 八皇后问题 八皇后问题是一个经典的回溯算法案例。 国际西洋棋棋手马克斯·贝瑟尔于1848年提出: 在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上,问有多少种摆法。 解法1 因为皇后不能在同一行中,所以可以排除掉行这一因素,所以可以使用一个数组c[n]来表示皇后的放法,如c[i]=j,表示第i行的皇后在j列 则判断皇后位置是否冲突的方法就只需要判断是否在同一列或同一斜线,如果c[a]=c[b],则第a行的皇后与第b行的皇后在同一列中; 如果|a-b|=|c[a]-c[b]|,则**a和b
Java 【LeetCode】52. N-Queens II 问题描述 https://leetcode.com/problems/n-queens-ii/#/description Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. 比N-Queens [http://www.zgljl2012.com/leetcode-51-n-queens/] 反倒容易一点了,因为不需要存储解法,只需要计算数目。
Java 【LeetCode】51. N-Queens 问题描述 https://leetcode.com/problems/n-queens/#/description 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
Java 【LeetCode】50. Pow(x, n) 问题描述 https://leetcode.com/problems/powx-n/#/description Implement pow(x, n). 实现pow(x,n),及实现幂运算。 算法分析 这个题目的难度是中等,所以如果用显而易见的那个O(n)算法,即直接循环n次,将n个x乘起来的话是肯定会超时的,所以,只能用递归分治算法,即一次只乘一半,设值为A ,即当n为偶数时,结果是A*A;当n为奇数时,结果为A*A*x;
Java 【LeetCode】49. Group Anagrams 问题描述 https://leetcode.com/problems/anagrams/#/description Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", "eat","tea"], ["nat","tan"], ["bat"] ] Note: All