package y2019.Algorithm.array.medium;

import java.util.Arrays;

/**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array.medium
* @ClassName: ProductExceptSelf
* @Author: xiaof
* @Description: TODo 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 equal to the product of all
* the elements of nums except nums[i].
* Input: [1,2,3,4]
* Output: [24,12,8,6]
*
* 给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积。
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/product-of-array-except-self
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*
* @Date: 2019/7/17 9:31
* @Version: 1.0
*/
public class ProductExceptSelf { public int[] solution(int[] nums) { //直接对所有数据求乘积,然后每个数据遍历的时候,求除数
int[] res = new int[nums.length];
int allX = 1, zeroNum = 0;
for(int i = 0; i < nums.length; ++i) {
if(nums[i] != 0) {
allX *= nums[i];
} else {
++zeroNum;
}
} //求各个位置的值
if(zeroNum <= 1) {
for(int i = 0; i < res.length; ++i) {
if(nums[i] == 0 && zeroNum == 1) { res[i] = allX;
} else if (zeroNum == 0) {
res[i] = allX / nums[i];
}
}
} return res;
} public static void main(String[] args) {
int data[] = {1,0};
ProductExceptSelf fuc = new ProductExceptSelf();
System.out.println(fuc.solution(data));
System.out.println();
} }
package y2019.Algorithm.array.medium;

import java.io.*;
import java.util.*; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array.medium
* @ClassName: Subsets
* @Author: xiaof
* @Description: TODO 78. Subsets
* Given a set of distinct integers, nums, return all possible subsets (the power set).
* Note: The solution set must not contain duplicate subsets.
*
* Input: nums = [1,2,3]
* Output:
* [
* [3],
* [1],
* [2],
* [1,2,3],
* [1,3],
* [2,3],
* [1,2],
* []
* ]
*
*
*
* @Date: 2019/7/17 10:49
* @Version: 1.0
*/
public class Subsets { public List<List<Integer>> solution(int[] nums) {
//输出所有可能组合,因为涉及到长度的变化,这里考虑用递归
List<List<Integer>> res = new ArrayList<>();
res.add(new ArrayList<>());
//每次递归深度加一,相当于去探索,长度不能超过总长,每次递归都是前面一次的集合加上下一次的集合
//那就要对位置做标记,但是长度又是不固定的并且不重复,那么考虑用set做标记
Set mark = new HashSet();
Arrays.sort(nums);
//这里还涉及一个问题,那就是可能有重复的组合,顺序不一样而已,那么为了排除掉乱序的,我们对数组拍个顺,然后每次只看后面的数据
allZhuHe(new ArrayList<>(), mark, res, 1, nums, 0); return res;
} public void allZhuHe(List<Integer> curList, Set marks, List<List<Integer>> res, int len, int[] nums, int startIndex) {
if(len > nums.length) {
return;
}
//如果再合理范围内,那么我们取不在集合中的数据
// Set tempSet = new HashSet(marks);
for(int i = startIndex; i < nums.length; ++i) {
if(!marks.contains(nums[i])) {
//如果不包含
List<Integer> tempList = new ArrayList<>(curList);
tempList.add(nums[i]);
res.add(tempList);
marks.add(nums[i]);
allZhuHe(tempList, marks, res, len + 1, nums, i);
marks.remove(nums[i]);
}
}
} // public List deepClone(List<Integer> curList) throws IOException, ClassNotFoundException {
// //直接拷贝对象
// ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
// objectOutputStream.writeObject(curList);
//
// ByteArrayInputStream byteIn = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
// ObjectInputStream in = new ObjectInputStream(byteIn);
// @SuppressWarnings("unchecked")
// List dest = (List) in.readObject();
// return dest;
//
// } public static void main(String[] args) {
int data[] = {1,2,3};
Subsets fuc = new Subsets();
System.out.println(fuc.solution(data));
System.out.println();
}
}

第287题,这个题联系到了快慢指针,惊艳到我了

package array.medium;

/**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array.medium
* @ClassName: FindDuplicate
* @Author: xiaof
* @Description: 287. Find the Duplicate Number
* Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive),
* prove that at least one duplicate number must exist. Assume that there is only one duplicate number,
* find the duplicate one.
*
* Input: [1,3,4,2,2]
* Output: 2
*
* 不能更改原数组(假设数组是只读的)。
* 只能使用额外的 O(1) 的空间。
* 时间复杂度小于 O(n2) 。
* 数组中只有一个重复的数字,但它可能不止重复出现一次。
*
* @Date: 2019/7/17 11:29 2019年9月5日10:04:13 2020年4月21日10:01:47
* @Version: 1.0
*/
public class FindDuplicate { public int solution(int[] nums) {
//明显就是hash了,但是不能修改原数组,因为数组再1~n之间,对于这种有范围的数组,直接hash不用想了
int[] hashNum = new int[nums.length];
for(int i = 0; i < nums.length; ++i) {
if(hashNum[nums[i]] == 0) {
hashNum[nums[i]]++;
} else {
return nums[i];
}
} return -1; } //因为数据范围是1~n,所以肯定是hash,但是又因为只能使用O(1)的空间
//参考:https://leetcode.com/problems/find-the-duplicate-number/discuss/72846/My-easy-understood-solution-with-O(n)-time-and-O(1)-space-without-modifying-the-array.-With-clear-explanation.
//这里用链表来做,吧数组中的每一个位置的数据当初指向下一个链表的对象
//比如:1,3,4,2,2转换成链表就是没个位置数据指向下一个链表对象
//(0,1)->(1,3)->(3,2)->(2,4)<->(4,2)
public int solution2(int[] nums) {
if (nums.length > 1) {
//大于1个数据才可以有可能重复
int slow = nums[0];
int fast = nums[nums[0]]; //指向第二个节点位置
while (slow != fast) {
//因为存在一个指向重复的节点,所以必定有环
slow = nums[slow]; //指向下一个位置
fast = nums[nums[fast]]; //移动2次
} //当我们slow和fast相遇之后,参考142. Linked List Cycle II的结论可知,从开头的位置到slow走向成环节点,和meet位置走向这个位置的距离是一样的,或者环的n倍
//这里需要计算的是-------参考 y2019.Algorithm.LinkedList.medium.DetectCycle
//2*s(走了s步) = s + n*r (环链表总长)
//s = n*r =>
//a(链表开始到环链开始节点距离) + x(链表开始循环到相遇节点距离) = n*r
//a(链表开始到环链开始节点距离) + x(链表开始循环到相遇节点距离) = (n - 1)*r + r
//a(链表开始到环链开始节点距离) = (n - 1)*r + r - x(链表开始循环到相遇节点距离)
//a(链表开始到环链开始节点距离) = (r - x) + (n - 1) * r
//a = h(相遇的节点到链表开始的节点) + (n-1) * r
//因为r是环形链表的长度,所以只要走a步,环形链表的位置不论循环多久,最后回到相遇的节点,然后会向前走h步,也就是到相遇的节点位置
fast = 0;
while (slow != fast) {
slow = nums[slow];
fast = nums[fast];
} return slow;
} return -1; } }

【LEETCODE】58、数组分类,适中级别,题目:238、78、287的更多相关文章

  1. LeetCode:颜色分类【75】

    LeetCode:颜色分类[75] 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 ...

  2. LeetCode.961-2N数组中N次重复的元素(N-Repeated Element in Size 2N Array)

    这是悦乐书的第365次更新,第393篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第227题(顺位题号是961).在大小为2N的数组A中,存在N+1个唯一元素,并且这些元 ...

  3. [LeetCode] All questions numbers conclusion 所有题目题号

    Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...

  4. LeetCode:数组中的第K个最大元素【215】

    LeetCode:数组中的第K个最大元素[215] 题目描述 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: ...

  5. leetcode(58)-Range Sum Query - Immutable

    题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...

  6. LeetCode: 58. Length of Last Word(Easy)

    1. 原题链接 https://leetcode.com/problems/length-of-last-word/description/ 2. 题目要求 给定一个String类型的字符串,字符串中 ...

  7. LeetCode一维数组的动态和

    一维数组的动态和 题目描述 给你一个数组 nums.数组「动态和」的计算公式为:runningSum[i] = sum(nums[0]...nums[i]). 请返回 nums 的动态和. 示例 1: ...

  8. 【LEETCODE】61、对leetcode的想法&数组分类,适中级别,题目:162、73

    这几天一直再想这样刷题真的有必要么,这种单纯的刷题刷得到尽头么??? 这种出题的的题目是无限的随便百度,要多少题有多少题,那么我这一直刷的意义在哪里??? 最近一直苦苦思考,不明所以,刷题刷得更多的感 ...

  9. 【LEETCODE】60、数组分类,适中级别,题目:75、560、105

    package y2019.Algorithm.array.medium; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.a ...

随机推荐

  1. 在windbg调试.net时遇到的问题

    调试.net应用程序时,有时会在windbg中收到错误消息.以下是我最常遇到的几个问题. Failed to start stack walk---启动堆栈遍历失败 如果你运行sos命令!clrsta ...

  2. 关于异常System.ArgumentException

    什么是System.ArgumentException 当向方法提供的参数之一无效时引发的异常. 继承 Object Exception SystemException ArgumentExcepti ...

  3. CF1237C2 【Balanced Removals (Harder)】

    这么妙的题怎么没人发题解啊 首先这是三维的,我们可以对其进行降维打击 先考虑一维怎么做? 我们可以对其该维坐标进行排序,按照顺序输出,可能会多余一个 那拓展到二维呢? 我们可以把它转化成一维,分成很多 ...

  4. 【NOIP2015】真题回顾

    题目链接 神奇的幻方 按照题意模拟 信息传递 不难想到这是一个基环树的森林,找一个最小环就可以了 斗地主 毒瘤搜索题,时限不紧,但是要考虑全所有情况 需要注意的一些地方: 先枚举顺子.再枚举四带二.三 ...

  5. 解决WordPress访问中文标签出现404的几个方法

    最近很多主题用户提到安装完WordPress后中文标签出现404的情况,出现这种情况一般修改固定链接设置是没有效果的,多数是windows主机带来的麻烦.网上多数人说要修改核心文件class-wp.p ...

  6. (五)Cisco dhcp snooping实例3-多交换机环境(DHCP服务器和DHCP客户端位于同VLAN)

    试验拓扑 环境:dhcp server和dhcp客户端属于同vlan,但是客户端属于不同的交换机,在L2和L3交换机开启dhcp snooping后得出如下结论 L3交换机的配置 ip dhcp po ...

  7. 大数据应用期末总评——Hadoop综合大作业

    作业要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3339 Hadoop综合大作业 要求: 1.将爬虫大作业产生的csv文件 ...

  8. vue+elementui搭建后台管理界面(7 vuex和mockjs的使用)

    将权限管理应用到系统,首先做好登录, 点击登录按钮后,触发以下动作 vuex 中的 login 动作,设置 cookie vuex 中的 getuserinfo , 获取权限.用户名.头像等 由于目前 ...

  9. Java-Maven(十二):idea多项目:common module进行compiler和install正常,运行domain-perf module提示:Could not resolve dependencies for project

    前提: product项目下有三个module,分别是: driver module domain-perf module common module 问题: driver 和 domain-perf ...

  10. RPC接口测试(一)什么是 RPC 框架

    什么是 RPC 框架 RPC 框架----- 远程过程调用协议RPC(Remote Procedure Call Protocol)-----允许像调用本地服务一样调用远程服务. RPC是指远程过程调 ...