第950题,这题我是真的没想到居然会说使用队列去做,大神的答案,拿过来瞻仰一下

package y2019.Algorithm.array;

import java.util.HashMap;
import java.util.Map; /**
* @ClassName Exist
* @Description TODO 79. Word Search
*
* Given a 2D board and a word, find if the word exists in the grid.
* The word can be constructed from letters of sequentially adjacent cell,
* where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
*
* board =
* [
* ['A','B','C','E'],
* ['S','F','C','S'],
* ['A','D','E','E']
* ]
* Given word = "ABCCED", return true.
* Given word = "SEE", return true.
* Given word = "ABCB", return false.
*
* 给定一个二维网格和一个单词,找出该单词是否存在于网格中。
* 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/word-search
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*
* @Author xiaof
* @Date 2019/7/14 22:50
* @Version 1.0
**/
public class Exist { public boolean solution(char[][] board, String word) {
//1.遍遍历Word,对字符进行遍历,并对字符位置进行比较
//遍历word
char[] w = word.toCharArray();
for (int y=0; y<board.length; y++) {
for (int x=0; x<board[y].length; x++) {
if (exist1(board, y, x, w, 0)) return true;
}
} return false;
} //i当前查看位置,然后把之前比遍历过的位置去掉,然后这里是对每个字符位置进行按位置异或操作,对256异或,其实就是取反
private boolean exist1(char[][] board, int y, int x, char[] word, int i) {
if(i == word.length) return true;
//如果深度满足,然后,x,y不能
if(y < 0 || x < 0 || y == board.length || x == board[y].length) return false;
if(board[y][x] != word[i]) return false;
//匹配成功,修改当前位置标记
board[y][x] = (char) ~board[y][x]; //继续向其他四个方向探索
boolean exist = exist1(board, y, x - 1, word, i + 1)
|| exist1(board, y, x + 1, word, i + 1)
|| exist1(board, y - 1, x, word, i + 1)
|| exist1(board, y + 1, x, word, i + 1);
//不断向四个方向探索,只要有一个方向探索成功,继续往下递归
board[y][x] = (char) ~board[y][x]; //还原
return exist; } }
package y2019.Algorithm.array;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map; /**
* @ClassName TriangleNumber
* @Description TODO 611. Valid Triangle Number
* Given an array consists of non-negative integers,
* your task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle.
*
* Input: [2,2,3,4]
* Output: 3
* Explanation:
* Valid combinations are:
* 2,3,4 (using the first 2)
* 2,3,4 (using the second 2)
* 2,2,3
*
* 给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数。
*
* @Author xiaof
* @Date 2019/7/14 23:38
* @Version 1.0
**/
public class TriangleNumber { public int solution(int[] nums) {
//1.首先对数组排序
quikSort(nums, 0, nums.length);
// Arrays.sort(nums);
//2.首先取三边的最大边
int count = 0;
for(int i = nums.length - 1; i >= 2; --i) {
int b1 = 0, b2 = i - 1, b3 = nums[i];
//3.然后排除最大边和右边的数据,在最大边左边取2条边比较,两边之和大于第三边
while(b1 < b2) {
//避免第一,第二个边取值错位
if(nums[b1] + nums[b2] > b3) {
//两边之和大于第三边
count += b2 - b1; //可以获取的个数是第二个和第一个边的位置距离,因为左边最小的加上也比第三条边大,那么说明中间所有数据和都比三大
//缓缓减少一遍大小
b2--;
} else {
//如果不满住两边之和大于第三边,那么就增加大小
b1++;
}
}
}
return count;
} private void quikSort(int[] nums, int begin, int end) {
if(begin < end) {
int midSite = midSite(nums, begin, end);
quikSort(nums, begin, midSite);
quikSort(nums, midSite + 1, end);
}
} private int midSite(int[] nums, int left, int right) {
if(left >= right) {
return nums[left];
} //这个位置是为了取出中位数的位置,并把两边的顺序分开
int midValue = nums[left], l1 = left, r1 = right; do { //遍历左边比他小的,因为这里是do-while,那么会先++,排除掉第0位
do { ++ l1; } while(l1 < right && nums[l1] < midValue); do { --r1; } while (left < r1 && nums[r1] > midValue); if(l1 < r1) {
//交换
int temp = nums[l1];
nums[l1] = nums[r1];
nums[r1] = temp;
} } while(l1 < r1); //最后把第一个位置的数据,交换到中间位置
nums[left] = nums[r1];
nums[r1] = midValue; return r1;
} public static void main(String[] args) {
int data[] = {10,20,11,30,5,40};
int data1[] ={2,2,3,4};
int data2[] = {82,15,23,82,67,0,3,92,11};
TriangleNumber fuc = new TriangleNumber();
fuc.quikSort(data2, 0, data2.length);
// System.out.println(fuc.solution(data2));
System.out.println(data);
} }
package y2019.Algorithm.array.medium;

import y2019.Algorithm.array.TriangleNumber;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array.medium
* @ClassName: DeckRevealedIncreasing
* @Author: xiaof
* @Description: TODO 950. Reveal Cards In Increasing Order
* In a deck of cards, every card has a unique integer. You can order the deck in any order you want.
* Initially, all the cards start face down (unrevealed) in one deck.
* Now, you do the following steps repeatedly, until all cards are revealed:
* Take the top card of the deck, reveal it, and take it out of the deck.
* If there are still cards in the deck, put the next top card of the deck at the bottom of the deck.
* If there are still unrevealed cards, go back to step 1. Otherwise, stop.
* Return an ordering of the deck that would reveal the cards in increasing order.
* The first entry in the answer is considered to be the top of the deck.
*
* Input: [17,13,11,2,3,5,7]
* Output: [2,13,3,11,5,17,7]
* Explanation:
* We get the deck in the order [17,13,11,2,3,5,7] (this order doesn't matter), and reorder it.
* After reordering, the deck starts as [2,13,3,11,5,17,7], where 2 is the top of the deck.
* We reveal 2, and move 13 to the bottom. The deck is now [3,11,5,17,7,13].
* We reveal 3, and move 11 to the bottom. The deck is now [5,17,7,13,11].
* We reveal 5, and move 17 to the bottom. The deck is now [7,13,11,17].
* We reveal 7, and move 13 to the bottom. The deck is now [11,17,13].
* We reveal 11, and move 17 to the bottom. The deck is now [13,17].
* We reveal 13, and move 17 to the bottom. The deck is now [17].
* We reveal 17.
* Since all the cards revealed are in increasing order, the answer is correct.
*
* 牌组中的每张卡牌都对应有一个唯一的整数。你可以按你想要的顺序对这套卡片进行排序。
* 最初,这些卡牌在牌组里是正面朝下的(即,未显示状态)。
* 现在,重复执行以下步骤,直到显示所有卡牌为止:
* 从牌组顶部抽一张牌,显示它,然后将其从牌组中移出。
* 如果牌组中仍有牌,则将下一张处于牌组顶部的牌放在牌组的底部。
* 如果仍有未显示的牌,那么返回步骤 1。否则,停止行动。
* 返回能以递增顺序显示卡牌的牌组顺序。
* 答案中的第一张牌被认为处于牌堆顶部。
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/reveal-cards-in-increasing-order
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*
* @Date: 2019/7/15 10:27
* @Version: 1.0
*/
public class DeckRevealedIncreasing { public int[] solution(int[] deck) {
//也就是要把数组中的数据间隔按照依次增大的顺序排列,然后把剩余间隔的按照递减的顺序,最后一个放最大值
Arrays.sort(deck); //如果长度是奇数,那么倒数第二位放最大,前面按照递减,如果是偶数,那么从最后往前奇数位递减
//偶数位放递增,奇数位放递减
int[] res = new int[deck.length];
if(deck.length % 2 == 0) {
//如果是偶数
for(int i = 0, j = 0, k = deck.length - 1; i < deck.length; i += 2) {
res[i] = deck[j++];
res[deck.length - i - 1] = deck[k--];
}
} else {
//如果是奇数
if(deck.length > 2)
res[deck.length - 2] = deck[deck.length - 1];
for(int i = 0, j = 0, k = deck.length - 2; i < deck.length; i += 2) {
res[i] = deck[j++];
if(i + 1 < deck.length && i != deck.length - 3)
res[i + 1] = deck[k--];
}
} return res;
} /**
*
* @program: y2019.Algorithm.array.medium.DeckRevealedIncreasing
* @description: 答案来源:https://leetcode.com/problems/reveal-cards-in-increasing-order/discuss/200526/Java-Queue-Simulation-Step-by-Step-Explanation
*/
public int[] solution2(int[] deck) {
int n= deck.length;
Arrays.sort(deck);
Queue<Integer> q= new LinkedList<>();
//排序号入队
for (int i=0; i<n; i++) q.add(i);
int[] res= new int[n];
for (int i=0; i<n; i++){
//先进后出的队列数据,从小取到大为对应的值
res[q.poll()]=deck[i];
//连续出队,这样间隔的数据放到队列尾部,后面取出来的时候,重新是间隔的从小到大的位置
q.add(q.poll());
}
return res;
} public static void main(String[] args) {
int data[] = {17,13,11,2,3,5,7};
int data1[] = {1,2,3,4,5,6};
DeckRevealedIncreasing fuc = new DeckRevealedIncreasing();
System.out.println(fuc.solution2(data1));
System.out.println();
} }

【LEETCODE】55、数组分类,适中级别,题目:79、611、950的更多相关文章

  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: 55. Jump Game(Medium)

    1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...

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

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

  6. LeetCode一维数组的动态和

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

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

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

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

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

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

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

随机推荐

  1. circus 架构

    转自官方文档:https://circus.readthedocs.io/en/latest/design/architecture/ Overall architecture Circus is c ...

  2. vue-cli 中的 eslint 规则说明

    "no-alert": 0,//禁止使用alert confirm prompt "no-array-constructor": 2,//禁止使用数组构造器 & ...

  3. java.lang.IllegalAccessException: void #####.MyBroadcastReceiver.() is not accessible from jav

    java.lang.IllegalAccessException: void #####.MyBroadcastReceiver.<init>() is not accessible fr ...

  4. GoCN每日新闻(2019-10-22)

    GoCN每日新闻(2019-10-22) GoCN每日新闻(2019-10-22) 1. Go 集成测试:https://www.ardanlabs.com/blog/2019/10/integrat ...

  5. Jmeter(四十三)_性能测试分配堆内存

    内存泄漏.内存溢出是什么? 内存泄露是指你的应用使用资源之后没有及时释放,导致应用内存中持有了不需要的资源,这是一种状态描述: 内存溢出是指你应用的内存已经不能满足正常使用了,堆栈已经达到系统设置的最 ...

  6. ubuntu之路——day11.7 end-to-end deep learning

    在传统的数据处理系统或学习系统中,有一些工作需要多个步骤进行,但是端到端的学习就是用一个神经网络来代替中间所有的过程. 举个例子,在语音识别中: X(Audio)----------MFCC----- ...

  7. 团队作业-Beta冲刺(2/4)

    队名:软工9组 组长博客:https://www.cnblogs.com/cmlei/ 作业博客:https://edu.cnblogs.com/campus/fzu/SoftwareEngineer ...

  8. EF join

    两张表: var query = db.Categories // 第一张表 .Join(db.CategoryMaps, // 第二张表 c => c.CategoryId, // 主键 cm ...

  9. vs2015编译OBS-Studio

    编译之前的准备: 系统win10 QT5.7.0 VS2015 CMake 3.13.4 obs vs2015环境依赖包:dependencies2015 obs-studio 24.0 ====== ...

  10. python简单的游戏场景代码

    模拟英雄联盟游戏场景的简单场景 最后计算出英雄的战斗力 class Hero: def __init__(self, na, gen, age, fig): self.name = na self.g ...