摘要: 题目 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。 必须原地修改,只允许使用额外常数空间。 以下是一些例子,输入位于左侧列,其相应输出位于右侧列。 1,2,3 → 1,3,2 3,2,1 阅读全文
posted @ 2020-08-09 21:10 阳离子 阅读(97) 评论(0) 推荐(0) 编辑
摘要: 前言 String是Object的子类,因此下面的代码是合法的。 Object obj = new String(); 但是,下面这段代码是不合法的。 ArrayList<Object> list = new ArrayList<String>(); 原因就是,泛型是不变的(invariant)。泛 阅读全文
posted @ 2020-08-07 23:02 阳离子 阅读(179) 评论(0) 推荐(0) 编辑
摘要: 题目 给你一个整数数组 nums ,请你找出数组中乘积最大的连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。 思路 动态规划问题,但是无法像最长连续子序列那样来处理。 在 问题中,我们维护了 ,那么我们在这个问题中,也维护一个 。 因为 ,简单来说, 。 因此还要维护 ,当更新 阅读全文
posted @ 2020-05-17 22:41 阳离子 阅读(138) 评论(0) 推荐(0) 编辑
摘要: 题目描述 已有方法 rand7 可生成 1 到 7 范围内的均匀随机整数,试写一个方法 rand10 生成 1 到 10 范围内的均匀随机整数。 不要使用系统的 Math.random() 方法。 题解 (rand_Y 1) X + rand_X = 可以生成[1, X Y]的等概率随机数。 在本题 阅读全文
posted @ 2020-05-17 22:18 阳离子 阅读(322) 评论(0) 推荐(0) 编辑
摘要: The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, meaning that when you use this coupon with a produc 阅读全文
posted @ 2020-03-12 19:24 阳离子 阅读(146) 评论(0) 推荐(0) 编辑
摘要: With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas s 阅读全文
posted @ 2020-03-12 11:18 阳离子 阅读(157) 评论(0) 推荐(0) 编辑
摘要: One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B , we say that A and B is r 阅读全文
posted @ 2020-03-11 22:52 阳离子 阅读(132) 评论(0) 推荐(0) 编辑
摘要: 非线性时间比较类 1. 交换排序 1.冒泡排序 思想:从前往后扫描,如果相邻两个元素的大小不满足要求,则进行交换。因此,每一轮可以将最大的元素放到最后一位,下一轮扫描时,就无需进行到最后一位了。 时间复杂度:进行两重循环,因此是O(n^2) 空间复杂度:原地排序,无需其他额外的空间,因此是O(1) 阅读全文
posted @ 2020-03-11 14:40 阳离子 阅读(275) 评论(0) 推荐(0) 编辑
摘要: A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a progra 阅读全文
posted @ 2020-03-09 11:06 阳离子 阅读(182) 评论(0) 推荐(0) 编辑
摘要: Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1 = { 11, 12, 13, 14 } is 1 阅读全文
posted @ 2020-03-08 22:55 阳离子 阅读(135) 评论(0) 推荐(0) 编辑