第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. CLR 调试概述

    利用公共语言运行时 (CLR) 调试 API,工具供应商可以编写调试器来调试运行于 CLR 环境中的应用程序. 要调试的代码可为 CLR 支持的任何代码种类.CLR 调试 API 主要是使用非托管代码 ...

  2. Windows用户模式调试内部组件

    简介 允许用户模式调试工作的内部机制很少得到充分的解释.更糟糕的是,这些机制在Windows XP中已经发生了根本性的变化,当许多支持被重新编写时,还通过将ntdll中的大多数例程作为本地API的一部 ...

  3. bg/fg

    将一个在后台暂停的命令,变成继续执行 (在后台执行). 一般ctrl+z就把前台命令调到了后台 将后台中的命令调至前台继续运行

  4. Windows 系统PowerShell或cmd设置添加静态路由方式

    电脑上添加静态路由,PowerShell或cmd设置路由 方法/步骤1.首先以管理员身份在“运行”窗口输入cmd或PowerShell(按WIN+R打开运行窗口),然后回车进入命令行,输入 route ...

  5. 【区间DP】加分二叉树

    https://www.luogu.org/problemnew/show/P1040#sub 题目描述 设一个n个节点的二叉树tree的中序遍历为(1,2,3,…,n),其中数字1,2,3,…,n为 ...

  6. 打造VIM成为IDE - nerdtree

    nerdtree 自动缩进 :set paste :set nopaste set tabstop=4 set softtabstop=4 set shiftwidth=4 set noautoind ...

  7. vuex基础入门

    Vuex简介 vuex的安装和组成介绍 [外链图片转存失败(img-nWQUUuyh-1565273314232)(https://upload-images.jianshu.io/upload_im ...

  8. CDH 6.0.1 版本 默认配置下 HUE | happybase 无法访问 Hbase 的问题

    第一个问题 HUE 无法直接连接到 HBase 在默认配置下 CDH 6.0.1 版本下的 HBase2.0 使用了默认配置 hbase.regionserver.thrift.compact = T ...

  9. nRF51822 配对之device_manager_init 调用,以及保证 用户数据存储 的Flash 操作不与device manager 模块冲突

    昨天 遇到了一个烦心的问题,被老外客户怼了两句,恼火,很想发火,发现英文不够用,算了,就不跟直肠的鬼佬一般见识.说正事. 最近的一个nRF51822+MT2503 钱包防丢项目,准备接近量产了.昨天做 ...

  10. Java 12 骚操作, switch居然还能这样玩!

    Java 13 都快要来了,12必须跟栈长学起! Java 13 即将发布,新特性必须抢先看! Java 12 中对 switch 的语法更友好了,建议大家看下栈长在Java技术栈微信公众号分享的&l ...