
Array
wutingyehe
Just for fun
展开
-
LintCode Valid Sudoku 判断数独是否合法
请判定一个数独是否有效。该数独可能只填充了部分数字,其中缺少的数字用 . 表示。 样例 下列就是一个合法数独的样例。 Determine whether a Sudoku is valid.The Sudoku board could be partially filled, where empty cells are filled with the character ..原创 2015-06-24 22:21:45 · 1834 阅读 · 0 评论 -
[LintCode] 跳跃游戏
给出一个非负整数数组,你最初定位在数组的第一个位置。 数组中的每个元素代表你在那个位置可以跳跃的最大长度。 判断你是否能到达数组的最后一个位置。注意事项 这个问题有两个方法,一个是贪心和 动态规划。 贪心方法时间复杂度为O(N)。 动态规划方法的时间复杂度为为O(n^2)。 我们手动设置小型数据集,使大家阔以通过测试的两种方式。这仅仅是为了让大家学会如何使用动态规划的方式解原创 2016-04-09 16:23:53 · 514 阅读 · 0 评论 -
【LintCode】Insert Intervals 插入区间
给出一个无重叠的按照区间起始端点排序的区间列表。 在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。样例 插入区间[2, 5] 到 [[1,2], [5,9]],我们得到 [[1,9]]。 插入区间[3, 4] 到 [[1,2], [5,9]],我们得到 [[1,2], [3,4], [5,9]]。/** * Definition of Inte原创 2015-07-16 10:56:53 · 1352 阅读 · 0 评论 -
【LintCode】Product of Array Exclude Itself 数组剔除元素后的乘积
给定一个整数数组A。 定义B[i] = A[0] * … * A[i-1] * A[i+1] * … * A[n-1], 计算B的时候请不要使用除法。样例 给出A=[1, 2, 3],返回 B为[6, 3, 2]public class Solution { /** * @param A: Given an integers array A * @return: A原创 2015-07-15 12:04:08 · 846 阅读 · 0 评论 -
【LintCode】 Best Time to Buy and Sell Stock 买卖股票的最佳时机
假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格。如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润。样例 给出一个数组样例 [3,2,3,1,2], 返回 1 public class Solution { /** * @param prices: Given an integer array * @return: Maximu原创 2015-07-15 23:11:06 · 498 阅读 · 0 评论 -
【LintCode】 Find Minimum in Rotated Sorted Array 寻找旋转排序数组中的最小值
假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2)。 你需要找到其中最小的元素。 你可以假设数组中不存在重复的元素。样例 给出[4,5,6,7,0,1,2] 返回 0public class Solution { /** * @param num: a rotated sorted array * @r原创 2015-07-15 22:53:35 · 572 阅读 · 0 评论 -
【LintCode】Search Insert Position 搜索插入位置
给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。 你可以假设在数组中无重复元素。样例 [1,3,5,6],5 → 2 [1,3,5,6],2 → 1 [1,3,5,6], 7 → 4 [1,3,5,6],0 → 0public class Solution { /** * param A : an integer原创 2015-07-15 22:07:17 · 1909 阅读 · 0 评论 -
【LintCode】Recover Rotated Sorted Array 恢复旋转排序数组
给定一个旋转排序数组,在原地恢复其排序。样例 [4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5]挑战 使用O(1)的额外空间和O(n)时间复杂度说明 什么是旋转数组? 比如,原始数组为[1,2,3,4], 则其旋转数组可以是[1,2,3,4], [2,3,4,1], [3,4,1,2], [4,1,2,3]public class Solution { /**原创 2015-07-15 22:01:15 · 1232 阅读 · 0 评论 -
LintCode Merge Sorted ArrayII 合并排序数组 II
合并两个排序的整数数组A和B变成一个新的数组。 Given two sorted integer arrays A and B, merge B into A as one sorted array.样例 给出A = [1, 2, 3, empty, empty] B = [4,5] 合并之后A将变成[1,2,3,4,5]注意 你可以假设A具有足够的空间(A数组的大小大于或等于m+n)去添加原创 2015-07-14 16:20:30 · 1040 阅读 · 0 评论 -
LintCode Merge Sorted Array 合并排序数组
合并两个排序的整数数组A和B变成一个新的数组。 Merge two given sorted integer array A and B into a new sorted integer array.样例 给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6]挑战 你能否优化你的算法,如果其中一个数组很大而另一个数组很小? How can you op原创 2015-07-14 16:00:41 · 1110 阅读 · 0 评论 -
LintCode Search a 2D matrix 搜索二维矩阵
写出一个高效的算法来搜索 m × n矩阵中的值。 这个矩阵具有以下特性: 每行中的整数从左到右是排序的。 每行的第一个数大于上一行的最后一个整数。Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Intege原创 2015-07-14 15:38:30 · 1381 阅读 · 0 评论 -
【LintCode】 Compare Strings 比较字符串
比较两个字符串A和B,确定A中是否包含B中所有的字符。字符串A和B中的字符都是大写字母。样例 给出 A = “ABCD” B = “ACD”,返回 true; 给出 A = “ABCD” B = “AABC”, 返回 false。注意 在 A 中出现的 B 字符串里的字符不需要连续或者有序。English Version:Compare two strings A and B, determi原创 2015-07-15 10:37:19 · 2348 阅读 · 0 评论 -
LinCode Remove Duplicates from Sorted Array 删除排序数组中的重复数字
中文描述:给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度。 不要使用额外的数组空间,必须在原地没有额外空间的条件下完成。样例 给出数组A =[1,1,2],你的函数应该返回长度2,此时A=[1,2]。English Version:Given a sorted array, remove the duplicates in place such t原创 2015-07-11 21:49:07 · 619 阅读 · 0 评论 -
LintCode Remove Duplicates from Sorted Array II 删除排序数组中的重复数字 II
中文描述:跟进“删除重复数字”: 如果可以允许出现两次重复将如何处理?样例 给出数组A =[1,1,1,2,2,3],你的函数应该返回长度5,此时A=[1,1,2,2,3]。English Version:Follow up for “Remove Duplicates”: What if duplicates are allowed at most twice?For example, G原创 2015-07-11 21:57:38 · 615 阅读 · 0 评论 -
LintCode Binary Search 二分查找
给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1。For a given sorted array (ascending order) and a target number, find the first index of this number in O(log n) ti原创 2015-07-10 17:51:37 · 1312 阅读 · 0 评论 -
LintCode Subarray Sum 子数组之和
给定一个整数数组,找到和为零的子数组。你的代码应该返回满足要求的子数组的起始位置和结束位置。样例 给出[-3, 1, 2, -3, 4],返回[0, 2] 或者 [1, 3].Given an integer array, find a subarray where the sum of numbers is zero. Your code should return the index of t原创 2015-06-26 13:24:06 · 1942 阅读 · 0 评论 -
[LintCode] 最小差 The Smallest Difference
给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 A 中取 A[i],数组 B 中取 B[j],A[i] 和 B[j]两者的差越小越好(|A[i] - B[j]|)。返回最小差。 样例 给定数组 A = [3,4,6,7], B = [2,3,8,9],返回 0。 挑战 时间复杂度 O(n log n)Given two array of integers(the first原创 2016-04-11 09:24:39 · 1231 阅读 · 1 评论