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. 如何防止CSRF攻击?

    CSRF攻击 CSRF漏洞的发生 相比XSS,CSRF的名气似乎并不是那么大,很多人都认为CSRF“不那么有破坏性”.真的是这样吗? 接下来有请小明出场~~ 小明的悲惨遭遇 这一天,小明同学百无聊赖地 ...

  2. CSS3之碰撞反弹动画无限运动

    示例代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  3. vuex如何实现数据持久化,刷新页面存储的值还存在

    1.安装: npm install vuex-persistedstate --save 2.找到store/index.js import Vue from 'vue' import Vuex fr ...

  4. css文字两行或者几行显示省略号

    做这个省略的问题,突然发现显示省略号是有中英文区分的 我做两行的时候用的是以下代码,在是中文的情况下是么得问题,到了英文下发现不起作用了 width: 250px; overflow: hidden; ...

  5. chown与chmod的区别

    chown 修改文件和文件夹的用户和用户组属性 1.要修改文件hh.c的所有者.修改为sakia的这个用户所有 chown sakia hh.c 这样就把hh.c的用户访问权限应用到sakia作为所有 ...

  6. 通过Zabbix监控Tomcat单机多实例

    前面已经介绍过Tomcat单机多实例部署,接下来就在他的基础上进行下一步操作:Tomcat多实例监控! Tomcat多实例监控过程和之前的redis多实例原理一样,分为以下4步: 1.获取多实例 2. ...

  7. Healthcare in Azure

  8. IOC注解详解

    @Component 修改一个类,将这个类交给Spring管理 相当于在配置文件当中配置<bean id="" class=""> @Compone ...

  9. uniapp - 关于ios调试

    [ios调试] 1.一台windows电脑.一根apple数据线(一旦连接以后,apple设备就会自动识别itunes软件,如果没有安装会提示) 2.安装itunes (爱思助手) 3.官方教程:ht ...

  10. 使用ItextSharop合并pdf文件,体积变大的解决

    通用的合并方式导致输出的pdf 文件中嵌入了大量的重复字体.导致文件体积膨胀. 使用基于内存流的方式,读取文件字节,可以解决重复字体的嵌入问题: public static string MergeF ...