leetcode 238 & leetcode 152 & leetcode 228
lc238 Product of Array Except Self
遍历两次数组 用一个res[] 记录答案
1) 第一次,从左往右遍历 res[i] 记录0~i-1的乘积
2) 第二次,从右往左遍历 res[i] *= right right *= nums[i]
注意两者顺序,right初值为1,更新步骤一定要在res[i]乘完之后,因为res *= right的目的是补上i+1~j这一部分的乘积,而不是i~j。
class Solution {
public int[] productExceptSelf(int[] nums) {
if(nums == null || nums.length < 2)
return nums; int len = nums.length;
int[] res = new int[len];
res[0] = 1; for(int i=1; i<len; i++){
res[i] = res[i-1] * nums[i-1];
}
int right = 1; for(int i=len-1; i>=0; i--){
res[i] *= right;
right *= nums[i];
} return res;
}
}
lc152 Maximum Product Subarray
本质上还是一个dp,dp[i] = Math.max(dp[i-1]*i, i),然后用一个res记录所有dp里的最大值。
不过因为是乘法所以要考虑正负性的问题,可能负负得正,min乘完i反而是max
有两种解决方案
1) 记录每次i的max和min乘积,若nums[i] 为负,则交换max和min,因为max*nums[i] 肯定为负,不如试一试min*nums[i]。
intuitive是min其实记录的yes负数里的“最大值“ (例如-982 < -32 --> 982 > 32),此时我们只要乘以一个负数,min*i有可能是最大值
class Solution {
public int maxProduct(int[] nums) {
if(nums == null || nums.length == 0)
return 0;
int len = nums.length;
int res = nums[0]; int max = res, min = res; for(int i=1; i<len; ++i){
if(nums[i] < 0){
int tmp = max;
max = min;
min = tmp;
} max = Math.max(nums[i], max*nums[i]);
min = Math.min(nums[i], min*nums[i]); res = Math.max(res, max);
} return res; }
2) 在写max和min更新方程的时候,考虑正负性,1)中分析了,min*i可能是最大值,同理max*i也可能是最小值,所以我们的更新方程应该写成:
max = Math.max(nums[i], Math.max(max*nums[i]), min*nums[i]);
min = Math.min(nums[i], Math.min(min*nums[i], man*nums[i]));
lc228 Summary Ranges
1) 观察这个数组,由于是有序且不重复的,这些差值为1的元素,他们与index的差值也是相同的 index: 0 1 2 3 4 5 6 nums: 2 4 5 7 8 9 12 差值: 2 3 3 4 4 4 6 我们可以通过这个规律来解决这个问题
2) 另一种更直观的想法就是:检查nums[i+1] – nums[i]是否 == 1 一个for循环,里面套一个while, while里面判断i+1和i的差值是否为1,为1就i++,否则停止
若更新了,那么原来的nums[i]肯定与现在的不同,所以我们for里第一句用一个tmp存储原来的nums[i] while之后,
若tmp != nums[i],说明tmp ~ nums[i]之间有多个连续的元素,为什么能肯定是多个?因为while的判断条件,没有多个,i不变,tmp == nums[i],然后for进行下一次循环。
若tmp == nums[i], 说明tmp这个元素单独成一组
class Solution {
public List<String> summaryRanges(int[] nums) {
if(nums == null || nums.length == 0){
List<String> res = new ArrayList<>();
return res;
}
int len = nums.length;
List<String> res = new ArrayList<>(); if(len == 1){
res.add(nums[0] + "");
return res;
} for(int i=0; i<len; i++){
int tmp = nums[i];
while(i+1 < len && nums[i+1] - nums[i] == 1){
i++;
}
if(tmp != nums[i]){
res.add(tmp+"->"+nums[i]);
}else
res.add(tmp+""); } return res; }
}
leetcode 238 & leetcode 152 & leetcode 228的更多相关文章
- LeetCode:汇总区间【228】
LeetCode:汇总区间[228] 题目描述 给定一个无重复元素的有序整数数组,返回数组区间范围的汇总. 示例 1: 输入: [0,1,2,4,5,7] 输出: ["0->2&quo ...
- 剑指offer 66. 构建乘积数组(Leetcode 238. Product of Array Except Self)
剑指offer 66. 构建乘积数组 题目: 给定一个数组A[0, 1, ..., n-1],请构建一个数组B[0, 1, ..., n-1],其中B中的元素B[i] = A[0] * A[1] * ...
- LeetCode 238. Product of Array Except Self (去除自己的数组之积)
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- LeetCode 刷题 App / LeetCode 题解 App
LeetCode 刷题 APP / LeetCode 题解 App 全端支持 http://leetcode-app.xgqfrms.xyz/ http://leetcode-desktop.xgqf ...
- LeetCode 238. 除自身以外数组的乘积(Product of Array Except Self)
238. 除自身以外数组的乘积 238. Product of Array Except Self 题目描述 LeetCode LeetCode238. Product of Array Except ...
- [LeetCode] 238. Product of Array Except Self 除本身之外的数组之积
Given an array nums of n integers where n > 1, return an array output such that output[i] is equ ...
- Leetcode Weekly Contest 152
退役老人现在连leetcode都不会做了 = = 今天早上做了leetcode第三题题目看错了,加上比赛中间还在调投稿的实验,一心二用直接gg 总结下教训就是 本渣现在做题连题目都看不清就开始做.开始 ...
- LN : leetcode 238 Product of Array Except Self
lc 238 Product of Array Except Self 238 Product of Array Except Self Given an array of n integers wh ...
- LeetCode No.151,152,153
No.151 ReverseWords 翻转字符串里的单词 题目 给定一个字符串,逐个翻转字符串中的每个单词. 示例 输入: "the sky is blue" 输出: " ...
随机推荐
- 校园商铺-4店铺注册功能模块-5店铺注册之Service层的实现
1. 创建接口 ShopService.java package com.csj2018.o2o.service; import java.io.File; import com.csj2018.o2 ...
- SpringBoot使用拦截器/ Servlet/ Filter
一.SpringBoot中使用拦截器 使用SpringMVC的拦截器,需要定义好拦截器,然后通过配置文件文件,对其进行注册 而在SpringBoot项目中,之前在配置文件中配置的内容,现在体现在一个类 ...
- VS开发工具的常用插件
转 http://www.spersky.com/post/vsPlugins.html 我目前主要用的是Hide Main Page——公司配给的电脑屏幕分辨率好小,还是1366*768的,去掉头可 ...
- css3 鼠标悬浮动画效果
CSS3案例 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...
- PAT甲级——A1132 Cut Integer
Cutting an integer means to cut a K digits lone integer Z into two integers of (K/2) digits long int ...
- 20.multi_case02
# 多进程,使用Process对象 from multiprocessing import Process def f(name): print('hello', name) if __name__ ...
- 论文阅读-(ECCV 2018) Second-order Democratic Aggregation
本文是Tsung-Yu Lin大神所作(B-CNN一作),主要是探究了一种无序的池化方法\(\gamma\) -democratic aggregators,可以最小化干扰信息或者对二阶特征的内容均等 ...
- Jumpserver-1.5.2 安装步骤
Jumpsever 是飞致云旗下的一块开源的堡垒机.在如今都在上云的趋势下,一款堡垒机非常重要. 官网:http://jumpserver.org/ GitHub:https://github.com ...
- Java开发系列-文件上传
概述 Java开发中文件上传的方式有很多,常见的有servlet3.0.common-fileUpload.框架.不管哪种方式,对于文件上传的本质是不变的. 文件上传的准备 文件上传需要客户端跟服务都 ...
- readv 和 writev
Unix 系统已经长时间支持名为 readv 和 writev 的 2 个系统调用. 这些 read 和 write 的"矢量"版本使用一个结构数组, 每个包含一个缓存的指针和一个 ...