package y2019.Algorithm.array;

/**
* @ClassName FindUnsortedSubarray
* @Description TODO 581. Shortest Unsorted Continuous Subarray
*
* Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order,
* then the whole array will be sorted in ascending order, too.
* You need to find the shortest such subarray and output its length.
*
* Input: [2, 6, 4, 8, 10, 9, 15]
* Output: 5
* Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.
*
* 求最短未排序的子串
* 求最短的无序连续子数组
*
* @Author xiaof
* @Date 2019/7/9 19:58
* @Version 1.0
**/
public class FindUnsortedSubarray { public int solution(int[] nums) {
//1.这里应该保障前后都是有序的,中间无序
int begin = -1, end = -2;
int max = nums[0], min = nums[nums.length - 1];
for(int i = 1; i < nums.length; ++i) {
max = Math.max(max, nums[i]);
min = Math.min(min, nums[nums.length - i - 1]);
//每次判断是否获取到最大值
if(nums[i] < max) end = i; //如果当前位置比最大的值小,那么乱序的末尾移动到这里(Math.max(max, nums[i]))前面有比较,难么i肯定在max之后
//判断开始的位置是,比最小的值大的再前面
if(nums[nums.length - i - 1] > min) begin = nums.length - 1 - i; } return end - begin + 1;
} }
package y2019.Algorithm.array;

import java.util.*;

/**
* @ClassName LargeGroupPositions
* @Description TODO 830. Positions of Large Groups
* In a string S of lowercase letters, these letters form consecutive groups of the same character.
* For example, a string like S = "abbxxxxzyy" has the groups "a", "bb", "xxxx", "z" and "yy".
* Call a group large if it has 3 or more characters. We would like the starting and ending positions of every large group.
* The final answer should be in lexicographic order.
*
* Input: "abbxxxxzzy"
* Output: [[3,6]]
* Explanation: "xxxx" is the single large group with starting 3 and ending positions 6.
*
* @Author xiaof
* @Date 2019/7/9 20:37
* @Version 1.0
**/
public class LargeGroupPositions { public List<List<Integer>> solution(String S) {
//1.用list存放分组(字符,长度),然后遍历分组中最长的那个
Map<Character, int[]> group = new IdentityHashMap<>();
int begin = 0, end = 0, maxLen = 3;;
char[] ss = S.toCharArray();
Character curC = ss[0];
for(int i = 1; i < ss.length; ++i) {
if (curC != ss[i]) {
//如果产生了变化
if(group.get(curC) != null) {
int[] temp = group.get(curC);
int len = temp[1] - temp[0];
if(len < (end - begin)) {
group.remove(curC);
group.put(curC, new int[]{begin, end});
}
} else {
group.put(curC, new int[]{begin, end});
}
// maxLen = Math.max(maxLen, end - begin + 1);
curC = ss[i];
begin = i;
end = i;
} else {
//如果不为空,并且是相同的字符,那么递增
end = i; }
} //最后放入最后一个元素
if(group.get(curC) != null) {
int[] temp = group.get(curC);
int len = temp[1] - temp[0];
maxLen = Math.max(maxLen, len);
if(len < (end - begin)) {
group.remove(curC);
group.put(curC, new int[]{begin, end});
}
} else {
group.put(curC, new int[]{begin, end});
} //获取其中最大的,并进行反馈
List<List<Integer>> result = new ArrayList<>(); if(maxLen < 3) {
return result;
} for(Map.Entry entry : group.entrySet()) {
int temp[] = (int[]) entry.getValue();
int curLen = temp[1] - temp[0] + 1;
if(curLen >= maxLen) {
result.add(Arrays.asList(temp[0], temp[1]));
}
} return result;
} public List<List<Integer>> solution2(String S) {
List<List<Integer>> res = new ArrayList<>();
//寻遍比那里所有字符,并把i赋值为j
for (int i = 0, j = 0; i < S.length(); i = j) {
//循环遍历到第一个不相等的位置
while (j < S.length() && S.charAt(j) == S.charAt(i)) ++j;
if (j - i >= 3)
res.add(Arrays.asList(i, j - 1));
}
return res;
} public static void main(String args[]) {
int A1[] = {1,4,3,2};
String s = "abbxxxxzzy";
s = "abc";
s = "abcdddeeeeaabbbcd";
s = "babaaaabbb";
LargeGroupPositions fuc = new LargeGroupPositions();
System.out.println(fuc.solution(s));
}
}
package y2019.Algorithm.array;

/**
* @ClassName NumPairsDivisibleBy60
* @Description TODO 1010. Pairs of Songs With Total Durations Divisible by 60
*
* In a list of songs, the i-th song has a duration of time[i] seconds.
* Return the number of pairs of songs for which their total duration in seconds is divisible by 60.
* Formally, we want the number of indices i < j with (time[i] + time[j]) % 60 == 0.
*
* Input: [30,20,150,100,40]
* Output: 3
* Explanation: Three pairs have a total duration divisible by 60:
* (time[0] = 30, time[2] = 150): total duration 180
* (time[1] = 20, time[3] = 100): total duration 120
* (time[1] = 20, time[4] = 40): total duration 60
*
* 题目的意思是找出数组中两数之和能被60整除的数对,数组最大的长度是60000,如果使用暴力求解需要两层循环,
* 需要进行59999+5998+…+2+1=1799910001次计算和判断的,很显然会超时。
*
* @Author xiaof
* @Date 2019/7/9 21:39
* @Version 1.0
**/
public class NumPairsDivisibleBy60 { public int solution(int[] time) { //由于和可以被60整除,也就是说,这些数的余数和为60
int[] occu = new int[60];
int result = 0;
for(int t : time) {
//这里组合的时候,注意我们按照后加入的和前面的数据进行配对,这样就可以出现不同的位置相同的数据可以被多次匹配
int index = t % 60 == 0 ? 0 : 60 - t % 60;
result += occu[index];
occu[t % 60]++;
} return result; } public int solution2(int[] time) {
int result = 0;
int[] occured = new int[60]; for(int i = 0;i < time.length;i++){
time[i] %= 60;
int index = time[i] == 0 ? 0 : 60 - time[i];
result += occured[index];
occured[time[i]]++;
} return result;
} public static void main(String args[]) {
int A1[] = {30,20,150,100,40};
NumPairsDivisibleBy60 fuc = new NumPairsDivisibleBy60();
System.out.println(fuc.solution2(A1));
} }
package y2019.Algorithm.array;

/**
* @ClassName CheckPossibility
* @Description TODO 665. Non-decreasing Array
* Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.
* We define an array is non-decreasing if array[i] <= array[i + 1] holds for every i (1 <= i < n).
*
* Input: [4,2,3]
* Output: True
* Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
*
* 题目给了我们一个nums array, 只允许我们一次机会去改动一个数字,使得数组成为不递减数组。可以实现的话,return true;不行的话,return false。
*   这个题目关键在于,当遇见一个 nums[i] > nums[i+1] 的情况,我们是把 nums[i]降为nums[i+1] 还是 把nums[i+1]升为nums[i]。
*   如果可行的话,当然是选择优先把 nums[i]降为nums[i+1],这样可以减少 nums[i+1] > nums[i+2] 的风险。
*
* @Author xiaof
* @Date 2019/7/9 22:08
* @Version 1.0
**/
public class CheckPossibility { public boolean solution(int[] nums) {
//也就是说这个数组要瞒住该表一个数据之后变成非递减数组
//这里需要对数组边遍历边修改
int needChangeTimes = 0;
for(int i = 0; i < nums.length - 1 && needChangeTimes <= 1; ++i) {
if(nums[i] > nums[i + 1]) {
++needChangeTimes;
if(i - 1 < 0 || nums[i - 1] <= nums[i + 1]) {
nums[i] = nums[i + 1];
} else {
nums[i + 1] = nums[i];
} }
} return needChangeTimes <= 1;
} public static void main(String args[]) {
int A1[] = {3,4,2,3};
CheckPossibility fuc = new CheckPossibility();
System.out.println(fuc.solution(A1));
} }

【LEETCODE】51、数组分类,简单级别,题目:581,830,1010,665的更多相关文章

  1. 【LEETCODE】54、数组分类,简单级别,题目:605、532

    数组类,简单级别完结.... 不容易啊,基本都是靠百度答案.... 希望做过之后后面可以自己复习,自己学会这个解法 package y2019.Algorithm.array; /** * @Proj ...

  2. 【LEETCODE】53、数组分类,简单级别,题目:989、674、1018、724、840、747

    真的感觉有点难... 这还是简单级别... 我也是醉了 package y2019.Algorithm.array; import java.math.BigDecimal; import java. ...

  3. LeetCode 面试题51. 数组中的逆序对

    面试题51. 数组中的逆序对 题目来源:https://leetcode-cn.com/problems/shu-zu-zhong-de-ni-xu-dui-lcof/ 题目 在数组中的两个数字,如果 ...

  4. 归并排序(归并排序求逆序对数)--16--归并排序--Leetcode面试题51.数组中的逆序对

    面试题51. 数组中的逆序对 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数. 示例 1: 输入: [7,5,6,4] 输出 ...

  5. 【LeetCode】数组-1(643)-返回规定长度k的最大子数组的平均数

    好久没有刷LeetCode了,准备重拾并坚持下去,每天刷个两小时.今天算是开始的第一天,不过出师不利,在一道很简单的题目上墨迹半天.不过还好,现在踩过的坑,应该都不会白踩,这些可能都是以后程序员路上稳 ...

  6. LeetCode:颜色分类【75】

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

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

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

  8. LeetCode~移除元素(简单)

    移除元素(简单) 1. 题目描述 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使 ...

  9. 面阿里P7,竟问这么简单的题目?

    关于作者:程序猿石头(ID: tangleithu),来自十八县贫困农村(查看我的逆袭之路),BAT某厂P7,是前大疆(无人机)技术主管,曾经也在创业公司待过,有着丰富的经验. 本文首发于微信公众号, ...

随机推荐

  1. Dart和JavaScript对比小结

    作为一名web前端来入门dart,新语言和我们熟悉的js有所差异,写dart的过程中容易受到原有思维的影响,这里把dart和js做一个对比总结,方便查找和熟悉. 变量声明 var 关键字 dart和j ...

  2. (浙江金华)Day 1 组合数计数

    目录 Day 1 组合计数 1.组合数 (1).C(n,m) 读作n选m,二项式系数 : (2).n个东西里选m个的方案数 不关心选的顺序: (3).二项式系数--->多项式系数: 2.组合数计 ...

  3. cyyz : Day 1 数论整理

    声明:感谢修改这篇博客的dsr Day 1 先说一下上午的听课吧,哎~,简直了,简直(⊙o⊙)…咋说呢,引人入胜???No! 是昏昏欲睡好吧...一点听课欲都没有(强撑....),一上午停下来简直怀疑 ...

  4. 安卓入门教程(十三)-Activity

    已经发表个人公众号 什么是Activity? Android是由Activity,Service,Content,Provider等组件组成,其中要讲的就是Activity组件,这是最基本,且常用的组 ...

  5. GDOI2018 小学生图论题 [NTT]

    并没有传送门qwq 思路 首先要知道一个结论(或者说是一个套路):一个竞赛图缩点之后必定是一条链. 那么强联通分量的个数,就是这条链的边数+1. 考虑一条边什么时候会出现:当且仅当点集可以被分成\(S ...

  6. MAKEFILE编写学习--1

    makefile是在编译中大型程序中使用的自动化编译工具make依赖的指令文件.这样可以使得程序的编译更加便捷快速. makefile的一般规则如下: target ... : prerequisit ...

  7. 【BIRT】修改主题背景颜色

    下图是BIRT默认的颜色配置,为了跟系统颜色格局相一致,此处需要对颜色进行修改; 下面简单介绍了如何修改不同位置的背景颜色 对应文件地址均在目录:../webcontent/birt/styles下 ...

  8. 前端VScode推荐插件

    Auto Close Tag 自动添加HTML / XML关闭标签 Auto Rename Tag 自动重命名配对的HTML / XML标签 Beautify 格式化代码 [必须]Bracket Pa ...

  9. 刷题之给定一个整数数组 nums 和一个目标值 taget,请你在该数组中找出和为目标值的那 两个 整数

    今天下午,看了一会github,想刷个题呢,就翻出来了刷点题提高自己的实际中的解决问题的能力,在面试的过程中,我们发现,其实很多时候,面试官 给我们的题,其实也是有一定的随机性的,所以我们要多刷更多的 ...

  10. mysql事务回滚机制概述

    应用场景:   银行取钱,从ATM机取钱,分为以下几个步骤       1 登陆ATM机,输入密码:    2 连接数据库,验证密码:    3 验证成功,获得用户信息,比如存款余额等:    4 用 ...