ACM算法专栏
专栏中会有CCF、LeetCode、牛客网在线编程题等等的ACM算法题,还会有一些常见算法的讨论与分析,这是一个属于算法的专栏!最终目的是:通过刷题,锻炼逻辑思维能力,更深入地了解算法,了解计算机。
zgljl2012
这个作者很懒,什么都没留下…
展开
-
【 LeetCode】79. Word Search
问题描述https://leetcode.com/problems/word-search/#/descriptionGiven 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, where原创 2017-06-09 09:20:55 · 1207 阅读 · 0 评论 -
【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], [2],原创 2017-06-08 08:48:26 · 1113 阅读 · 0 评论 -
【LeetCode】77. Combinations
问题描述https://leetcode.com/problems/combinations/#/descriptionGiven 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 solution is:[ [2原创 2017-06-07 11:51:52 · 1007 阅读 · 0 评论 -
【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 the or原创 2017-06-05 10:24:22 · 773 阅读 · 0 评论 -
【LeetCode】74. Search a 2D Matrix
问题描述https://leetcode.com/problems/search-a-2d-matrix/#/descriptionWrite an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each r原创 2017-06-04 14:06:07 · 941 阅读 · 0 评论 -
【LeetCode】 73. Set Matrix Zeroes
问题描述https://leetcode.com/problems/set-matrix-zeroes/#/descriptionGiven a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.给定一个矩阵,如果一个元素为0,则其所在行和所在列都置为0.注意:乍一看很好解决——碰原创 2017-06-01 10:34:32 · 762 阅读 · 0 评论 -
【LeetCode】72. Edit Distance
问题描述https://leetcode.com/problems/edit-distance/#/descriptionGiven two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)原创 2017-05-31 20:07:22 · 673 阅读 · 0 评论 -
【LeetCode】71. Simplify Path
问题描述https://leetcode.com/problems/simplify-path/#/descriptionGiven an absolute path for a file (Unix-style), simplify it.For example path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c"Corne原创 2017-05-30 12:19:54 · 949 阅读 · 0 评论 -
【LeetCode】69. Sqrt(x)
问题描述https://leetcode.com/problems/sqrtx/#/descriptionImplement int sqrt(int x).Compute and return the square root of x.算法使用折半查找即可,但要注意整型溢出代码public int mySqrt(int x) { int left = 1, right = x;原创 2017-05-29 08:22:13 · 1375 阅读 · 0 评论 -
【LeetCode】64. Minimum Path Sum
问题描述https://leetcode.com/problems/minimum-path-sum/#/descriptionGiven 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 alo原创 2017-05-26 08:51:08 · 692 阅读 · 0 评论 -
【LeetCode】63. Unique Paths II
问题描述https://leetcode.com/problems/unique-paths-ii/#/descriptionFollow up for “Unique Paths”:Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and e原创 2017-05-25 12:17:31 · 718 阅读 · 0 评论 -
【LeetCode】62. Unique Paths
问题描述https://leetcode.com/problems/unique-paths/#/descriptionA 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 down or right原创 2017-05-24 08:24:36 · 709 阅读 · 0 评论 -
【LeetCode】60. Permutation Sequence
问题描述https://leetcode.com/problems/permutation-sequence/#/descriptionhe 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 f原创 2017-05-22 12:53:39 · 1911 阅读 · 0 评论 -
【LeetCode】59. Spiral Matrix II
问题描述https://leetcode.com/problems/spiral-matrix-ii/#/descriptionGiven 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 retu原创 2017-05-21 21:59:16 · 875 阅读 · 0 评论 -
【LeetCode】57. Insert Interval
问题描述https://leetcode.com/problems/insert-interval/#/descriptionGiven a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals原创 2017-05-20 12:57:56 · 1018 阅读 · 0 评论 -
【LeetCode】56. Merge Intervals
问题描述https://leetcode.com/problems/merge-intervals/#/descriptionGiven 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,原创 2017-05-19 13:14:35 · 1307 阅读 · 0 评论 -
【LeetCode】55. Jump Game
问题描述https://leetcode.com/problems/jump-game/#/descriptionGiven an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your原创 2017-05-17 21:26:11 · 807 阅读 · 0 评论 -
【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原创 2017-05-16 09:57:21 · 990 阅读 · 0 评论 -
【LeetCode】53. Maximum Subarray
问题描述https://leetcode.com/problems/maximum-subarray/#/descriptionFind the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,原创 2017-05-15 08:37:34 · 2033 阅读 · 0 评论 -
【LeetCode】52. N-Queens II
问题描述https://leetcode.com/problems/n-queens-ii/#/descriptionFollow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.比N-Queens反倒容易一点了,因为原创 2017-05-14 11:30:12 · 966 阅读 · 0 评论 -
【LeetCode】51. N-Queens
问题描述https://leetcode.com/problems/n-queens/#/descriptionThe 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原创 2017-05-13 11:39:41 · 2141 阅读 · 0 评论 -
【LeetCode】50. Pow(x, n)
问题描述https://leetcode.com/problems/powx-n/#/descriptionImplement pow(x, n).实现pow(x,n),及实现幂运算。算法分析这个题目的难度是中等,所以如果用显而易见的那个O(n)算法,即直接循环n次,将n个x乘起来的话是肯定会超时的,所以,只能用递归分治算法,即一次只乘一半,设值为A,即当n为偶数时,结果是A*A;当n为奇数时,结果原创 2017-05-12 17:49:40 · 1946 阅读 · 0 评论 -
【LeetCode】49. Group Anagrams
问题描述https://leetcode.com/problems/anagrams/#/descriptionGiven an array of strings, group anagrams together.For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return:[ ["ate", "eat","te原创 2017-05-11 15:43:07 · 2290 阅读 · 0 评论 -
【LeetCode】48. Rotate Image
问题描述https://leetcode.com/problems/rotate-image/#/descriptionYou 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?将一个n原创 2017-05-09 09:39:08 · 1304 阅读 · 0 评论 -
【LeetCode】46. Permutations
问题链接:https://leetcode.com/problems/permutations/#/description求数组的全排列算法一个个的将每一个数插入数组,如数组[1,2,3],设结果二重数组为r,初始化r中只有一个数组r=[[1]], 然后要插入数据2,有两种插法,[1,2]和[2,1],即插在1前面和后面,从而r里有了两个数组r=[[1,2],[2,1]],接下来插入数据3,对于数原创 2017-05-05 12:47:21 · 947 阅读 · 0 评论 -
【LeetCode】41. First Missing Positive
问题描述https://leetcode.com/problems/first-missing-positive/#/descriptionGiven an unsorted integer array, find the first missing positive integer.For example, Given [1,2,0] return 3, and [3,4,-1,1] retu原创 2017-03-22 10:29:25 · 968 阅读 · 5 评论 -
【LeetCode】204. Count Primes
问题描述Description:Count the number of prime numbers less than a non-negative number, n.点击查看问题问题分析筛选法求素数,参考这篇文章【算法】普通方法和筛选法求素数,这道题用普通方法求素数会超时,只能用筛选法。代码class Solution {public: int countPrimes(int n) {原创 2017-01-17 14:58:11 · 753 阅读 · 0 评论 -
【LeetCode】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.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock),原创 2017-01-11 22:43:30 · 858 阅读 · 0 评论 -
【LeetCode】11. Container With Most Water
问题描述Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two原创 2017-01-11 22:42:58 · 986 阅读 · 0 评论 -
【LeetCode】125. Valid Palindrome
问题描述Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a pal原创 2017-01-11 22:44:56 · 1422 阅读 · 0 评论 -
【LeetCode】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 differ原创 2017-01-09 10:13:20 · 794 阅读 · 0 评论 -
【LeetCode】111. Minimum Depth of Binary Tree
问题描述Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.二叉树最短路径问题。问题分析两种解法:递归遍历,求每棵子树的最小路径宽度优原创 2017-01-09 10:13:54 · 742 阅读 · 0 评论 -
【LeetCode】88. Merge Sorted Array
问题描述Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additi原创 2017-01-05 14:44:20 · 741 阅读 · 0 评论 -
【LeetCode】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.大数进位问题,给一个由数组表示的数加上1,返回同样原创 2017-01-05 11:47:00 · 712 阅读 · 0 评论 -
【LeetCode】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.给定一个字符串S,找到最长的回文子串(S的长度原创 2016-09-28 15:37:17 · 874 阅读 · 0 评论 -
【LeetCode】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 "b", with th原创 2016-09-23 09:55:17 · 1032 阅读 · 0 评论 -
【LeetCode】Add Two Numbers
问题描述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 l原创 2016-09-22 09:55:03 · 1074 阅读 · 0 评论 -
【Leetcode】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.Example:Given nums = [2, 7, 1原创 2016-09-21 09:32:46 · 1073 阅读 · 0 评论 -
【leetcode】Insert Delete GetRandom O(1)
Insert Delete GetRandom O(1)问题描述Design a data structure that supports all following operations in average O(1) time. insert(val): Inserts an item val to the set if not already present. remove(val): R原创 2016-08-05 15:28:09 · 1763 阅读 · 1 评论 -
【leetcode】Remove Duplicates from sorted array
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原创 2016-08-04 14:18:42 · 978 阅读 · 0 评论