第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();
} }
05-13 20:43