package y2019.Algorithm.array.medium;

/**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array.medium
* @ClassName: SortColors
* @Author: xiaof
* @Description: TODO 75. Sort Colors
* Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent,
* with the colors in the order red, white and blue.
* Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
*
* Input: [2,0,2,1,1,0]
* Output: [0,0,1,1,2,2]
*
* 给定一个包含红色、白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。
* 此题中,我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/sort-colors
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*
* 大神答案参考:https://leetcode.com/problems/sort-colors/discuss/26472/Share-my-at-most-two-pass-constant-space-10-line-solution
*
* @Date: 2019/7/19 9:03
* @Version: 1.0
*/
public class SortColors { public void solution(int[] nums) {
//这里进行hash放置的话,可能会产生冲突,那么就需要把hash值依次往后排,然后和对应的值交换位置
//针对这题,因为只有三类数据,那么0再最开头,2再最末尾,其余的就会被放到中间
int index0 = 0, index2 = nums.length - 1;
for(int i = 0; i <= index2; ++i) {
while(nums[i] == 2 && i < index2) {
//当指定位置是2,那么我们吧他交换到指定的2的位置
//并且这里赋值字后,如果值改变了,那么就会跳出while循环,NB
nums[i] = nums[index2];
nums[index2--] = 2;
}
//然后交换0的位置
while(nums[i] == 0 && i > index0) {
nums[i] = nums[index0];
nums[index0++] = 0;
}
} } public static void main(String[] args) {
int data[] = {2,0,2,1,1,0};
SortColors fuc = new SortColors();
fuc.solution(data);
System.out.println();
}
}
package y2019.Algorithm.array.medium;

import java.util.HashMap;
import java.util.Map; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array.medium
* @ClassName: SubarraySum
* @Author: xiaof
* @Description: TODO 560. Subarray Sum Equals K
*
* Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.
* Input:nums = [1,1,1], k = 2
* Output: 2
*
* @Date: 2019/7/19 9:50
* @Version: 1.0
*/
public class SubarraySum { public int solution(int[] nums, int k) {
//查子串和,那么我们遍历这个数组,然后求子串值,如果和超了,那么就把最前面的数剔除掉,然后再比较,小了就往后
int sum = 0, count = 0;
Map<Integer, Integer> sumMap = new HashMap();
sumMap.put(0, 1); //和为0的情况那么就是单独一个元素
//由于是求子串,子串是连续的,那么我们只要求出i->j连续的和是k即可
//而i->j = 0->j - 0->i 的差值,转而言之,我们需要把k + {0->i} = {0->j}求出来个数即可
for(int i = 0; i < nums.length; ++i) {
sum += nums[i];
//这里不需要考虑后面没有put进去的sum,因为我们要求i->j的和,只要判断前面的数据就可以了,后面的和放在后面比较
if(sumMap.containsKey(sum - k)) {
//那么就是存在
count += sumMap.get(sum - k);
}
//如果不包含,那么就把值放入,进行下一个子串的遍历
sumMap.put(sum, sumMap.getOrDefault(sum, 0) + 1);
} return count;
}
}
package y2019.Algorithm.array.medium;

/**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array.medium
* @ClassName: BuildTree
* @Author: xiaof
* @Description: TODO 105. Construct Binary Tree from Preorder and Inorder Traversal
* Given preorder and inorder traversal of a tree, construct the binary tree.
*
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
*
* preorder = [3,9,20,15,7]
* inorder = [9,3,15,20,7]
*
* 3
* / \
* 9 20
* / \
* 15 7
*
* @Date: 2019/7/19 10:31
* @Version: 1.0
*/
public class BuildTree { public class TreeNode {
private int value;
private TreeNode left;
private TreeNode right; public TreeNode(int x) {
this.value = x;
} public int getValue() {
return value;
} public void setValue(int value) {
this.value = value;
} public TreeNode getLeft() {
return left;
} public void setLeft(TreeNode left) {
this.left = left;
} public TreeNode getRight() {
return right;
} public void setRight(TreeNode right) {
this.right = right;
}
} public TreeNode solution(int[] preorder, int[] inorder) {
//根据前序遍历和中序组建一颗树
//探索类,考虑递归
return backTrack(preorder, 0, inorder, 0, inorder.length - 1); } public TreeNode backTrack(int[] preorder, int preLeft, int[] inorder, int inLeft, int inRight) {
if(preLeft > preorder.length - 1 || inLeft > inRight) {
return null;
}
//如果没有,我们可以吧当前树的前序遍历第一个当成根
TreeNode curRoot = new TreeNode(preorder[preLeft]);
int midIndex = 0;
//然后我们去中序寻找中间点
for(int i = inLeft; i <= inRight; ++i) {
if(preorder[preLeft] == inorder[i]) {
//找到位置
midIndex = i;
}
} //中分两边中序
//左子树
curRoot.setLeft(backTrack(preorder, preLeft + 1, inorder, inLeft, midIndex - 1));
curRoot.setRight(backTrack(preorder, preLeft + midIndex + 1 - inLeft, inorder, midIndex + 1, inRight));
return curRoot;
} public static void main(String[] args) {
int preorder1[] = {3,9,20,15,7};
int inorder1[] = {9,3,15,20,7};
BuildTree fuc = new BuildTree();
fuc.solution(preorder1, inorder1);
System.out.println();
}
}

【LEETCODE】60、数组分类,适中级别,题目:75、560、105的更多相关文章

  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:60. Permutation Sequence,n全排列的第k个子列

    LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...

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

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

  5. LeetCode:数组专题

    数组专题 有关数组的一些 leetcode 题,在此做一些记录,不然没几天就忘光光了 二分查找 双指针 滑动窗口 前缀和/差分数组 二分查找 本文内容摘录自公众号labuladong中有关二分查找的文 ...

  6. LeetCode: 60. Permutation Sequence(Medium)

    1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...

  7. LeetCode一维数组的动态和

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

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

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

  9. 【LEETCODE】58、数组分类,适中级别,题目:238、78、287

    package y2019.Algorithm.array.medium; import java.util.Arrays; /** * @ProjectName: cutter-point * @P ...

随机推荐

  1. js传对象处理

    JSON.stringify(carlist); 需要先将对象进行处理:如果服务端解析异常,可以先将这个值单独解析一次

  2. 【Beta】Phylab 测试报告

    PhyLab Beta 测试报告 测试中发现的bug Beta阶段新Bug Bug 可能原因 markdown生成的报告可能溢出显示框 表格过长,显示框未设置横向溢出 移动端实验区无法评论 移动端社区 ...

  3. php 面试必备:各种缓存技术详解

    这门课程以电商网站为例,通过具体场景模块实战,让你更系统的掌握缓存原理.使用场景等相关知识,帮助你构建完整的缓存知识体系,胜任实际开发中缓存的处理,提升代码性能!    从原理到场景 系统讲解PHP缓 ...

  4. jeecg数据库切换至mysql8.0方式

    1.修改pom.xml   mysql版本 <mysql.version>8.0.11</mysql.version> 2.修改dbconfig.properties文件 hi ...

  5. Spring Web Flux 相关概念

    Reactive Streams.Reactor 和 Web Flux 上面介绍了反应式编程的一些概念,以及 Reactor 和 Web Flux.可能读者看到这里有些乱.这里介绍一下三者的关系.其实 ...

  6. odoo开发笔记 -- 借助模块queue_job实现异步方法调用

    场景描述: 对比了几个定时调度的框架,发现各有优缺点: celery 很强,异步定时调度,异步周期调度,也有延时调度的功能,但是延时调度的案例比较少,遂暂时不使用. queue_job,一个odoo第 ...

  7. 解决bootstrap-table表头filter-control select控件被遮挡显示不全的问题

    [本文出自天外归云的博客园] 在使用bootstrap-table的extension——filter-control时(对应表格的data-filter-control="true&quo ...

  8. docker入门-基本概念(一)

    Docker是什么 Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源. Docker的应用场景 Web 应用的自动化打包和发布. 自动化测试和持续集成.发 ...

  9. ubuntu解决网络连接的优先级

    ubuntu下无线连接网络,有线连接板卡,解决网络连接的优先级 1. 查看网关  ip route show dev wlp2s0 proto kernel scope link src dev wl ...

  10. python初级(302) 6 对象(三)

    一.复习 1.什么是魔法方法? 2.什么是类的初始化函数? 二.什么是self 使用一个类可以创建多个对象实例,例如: ball1 = Ball("red", "smal ...