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的更多相关文章

  1. LeetCode:汇总区间【228】

    LeetCode:汇总区间[228] 题目描述 给定一个无重复元素的有序整数数组,返回数组区间范围的汇总. 示例 1: 输入: [0,1,2,4,5,7] 输出: ["0->2&quo ...

  2. 剑指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] * ...

  3. 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 ...

  4. LeetCode 刷题 App / LeetCode 题解 App

    LeetCode 刷题 APP / LeetCode 题解 App 全端支持 http://leetcode-app.xgqfrms.xyz/ http://leetcode-desktop.xgqf ...

  5. LeetCode 238. 除自身以外数组的乘积(Product of Array Except Self)

    238. 除自身以外数组的乘积 238. Product of Array Except Self 题目描述 LeetCode LeetCode238. Product of Array Except ...

  6. [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 ...

  7. Leetcode Weekly Contest 152

    退役老人现在连leetcode都不会做了 = = 今天早上做了leetcode第三题题目看错了,加上比赛中间还在调投稿的实验,一心二用直接gg 总结下教训就是 本渣现在做题连题目都看不清就开始做.开始 ...

  8. 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 ...

  9. LeetCode No.151,152,153

    No.151 ReverseWords 翻转字符串里的单词 题目 给定一个字符串,逐个翻转字符串中的每个单词. 示例 输入: "the sky is blue" 输出: " ...

随机推荐

  1. 代码执行批量Excel数据导入Oracle数据库

    由于基于Oracle数据库上做开发,因此常常会需要把大量的Excel数据导入到Oracle数据库中,其实如果从事SqlServer数据库的开发,那么思路也是一样的,本文主要介绍如何导入Excel数据进 ...

  2. thinkphp 操作绑定到类

    定义 ThinkPHP3.2版本提供了把每个操作方法定位到一个类的功能,可以让你的开发工作更细化,可以设置参数ACTION_BIND_CLASS,例如: 'ACTION_BIND_CLASS' =&g ...

  3. Python 函数与内置函数

    1.函数的基本定义 def 函数名称(参数) 执行语句 return 返回值 def : 定义函数的关键字: 函数名称:顾名思义,就是函数的名字,可以用来调用函数,不能使用关键字来命名,做好是用这个函 ...

  4. hdu-1394(线段树求最小逆序数)

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意: 给定一个n,然后又n个数字,首先,这些数字的大小是从0开始到n-1,比如样例n=10,则这十个数就 ...

  5. python collections模块 之 defaultdict

    defaultdict 是 dict 的子类,因此 defaultdict 也可被当成 dict 来使用,dict 支持的功能,defaultdict 基本都支持.但它与 dict 最大的区别在于,如 ...

  6. 如何快速合并多个TXT文本内容

    工作中有时候需要合并很多文本内容,例如一些推送清单之类,一个一个打开去复制粘贴的话,少量还行,如果txt文本数据量大(10+M以上)且文件数量多(成百上千),这种方式就显得很低效了.具体要求如下:   ...

  7. 【左偏树】 [JLOI2015]城池攻占

    原来左偏树还可以打tag,get了 和线段树打tag一样,时不时Push_Down就好了 然后这里显然也是要先乘法后加法的 tag打上了之后还是其他一般左偏树差不多,有些细节注意一下 然后开 long ...

  8. SPOJ694 New Distinct Substrings

    New Distinct Substrings 题目大意 给定一个字符串,求本质不同的子串个数 题解 SA常见思想:每一个子串都是某个后缀的前缀 考虑每一个后缀的贡献,首先他拥有n - sa[i]个( ...

  9. Aria2配置文件-aria2.conf

    ##此部分主要分为几部分###1.文件保存#2.下载链接#3.进度保存#4.RPC相关#5.BT\PT下载相关 ##===================================#### 文件 ...

  10. JAVA-第一课 环境的配置

    首先 我们需要 下载java的开发工具包 jdk  jdk 的下载地址::http://www.oracle.com/technetwork/java/javase/downloads/index.h ...