第950題,這題我是真的沒想到竟然會說使用隊列去作,大神的答案,拿過來瞻仰一下java
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(); } }