ACM
zgljl2012
这个作者很懒,什么都没留下…
展开
-
【剑指Offer】连续子数组的最大和
问题描述HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决。但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢?例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。你会不会被他忽悠住?算法分析有一个te原创 2015-09-21 00:35:15 · 1867 阅读 · 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 · 1018 阅读 · 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 · 882 阅读 · 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 · 1456 阅读 · 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 · 1003 阅读 · 5 评论 -
【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 · 2316 阅读 · 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 · 1972 阅读 · 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 · 737 阅读 · 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 · 2169 阅读 · 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 · 779 阅读 · 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 · 823 阅读 · 0 评论 -
【剑指Offer】把数组排成最小的数
题目描述输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。将数字转化为字符串,然后对字符串进行快速排序class Solution {public: string PrintMinNumber(vector<int> numbers) { string r原创 2015-09-21 00:39:23 · 2222 阅读 · 0 评论 -
【剑指Offer】文章索引(未完)
下面是牛客网剑指Offer编程题的一些解题报告,目前还没刷完,会一篇篇加上来。跳台阶变态跳台阶矩形覆盖重建二叉树替换空格用两个栈代替队列斐波那契数列二进制中 1 的个数原创 2015-09-18 09:23:59 · 1758 阅读 · 2 评论 -
【剑指Offer】第一个只出现一次的字符位置
问题描述在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符的位置。若为空串,返回-1。位置索引从0开始.示例: 输入:sabcdsdf 输出:1算法描述定义一个52个元素的整型数组aCount,初始化为0,每个字母(大小写)依次对应一个,记录字母出现的次数; 定义一个52个元素的整型数组aPos,初始化为-1,每个字母(大小写)对应一个,记录字母第一次出原创 2015-10-25 23:12:09 · 2639 阅读 · 0 评论 -
【ACM】K尾相等数
问题描述: 从键盘输入一个自然数K(K>1),若存在自然数M和N(M>N),使得K^M 和K^N均大于或等于1000,且它们的末尾三位数相等,则称M和N是一对”K尾相等数”。请编写程序,输出M+N最小的K尾相等数。样例: 输入 输出 2 120问题分析: 末尾三位数只有1000个,从1到无穷大增大幂次时,肯定会出现同样的末尾三位数,比如n=10时(n^M大于1000时才有末尾三位原创 2016-05-18 16:03:49 · 2078 阅读 · 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 · 1790 阅读 · 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 · 1004 阅读 · 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 · 738 阅读 · 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 · 765 阅读 · 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 · 977 阅读 · 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 · 1000 阅读 · 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 · 2058 阅读 · 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 · 696 阅读 · 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 · 1041 阅读 · 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 · 821 阅读 · 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 · 902 阅读 · 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 · 1939 阅读 · 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 · 1032 阅读 · 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 · 1146 阅读 · 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 · 966 阅读 · 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 · 800 阅读 · 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 · 1326 阅读 · 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 · 742 阅读 · 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 · 719 阅读 · 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 · 1023 阅读 · 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 · 1328 阅读 · 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 · 834 阅读 · 0 评论 -
【LeetCode】68. Text Justification
问题描述https://leetcode.com/problems/text-justification/#/descriptionGiven 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) just原创 2017-05-28 10:31:08 · 996 阅读 · 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 · 1403 阅读 · 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 · 975 阅读 · 0 评论