數組類,簡單級別完結。。。。java
不容易啊,基本都是靠百度答案。。。。數組
但願作過以後後面能夠本身複習,本身學會這個解法網絡
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * @ClassName: CanPlaceFlowers * @Author: xiaof * @Description: TODO 605. Can Place Flowers * Suppose you have a long flowerbed in which some of the plots are planted and some are not. * However, flowers cannot be planted in adjacent plots - they would compete for water and both would die. * Given a flowerbed (represented as an array containing 0 and 1, where 0 means empty and 1 means not empty), * and a number n, return if n new flowers can be planted in it without violating the no-adjacent-flowers rule. * * 假設你有一個很長的花壇,一部分地塊種植了花,另外一部分卻沒有。但是,花卉不能種植在相鄰的地塊上,它們會爭奪水源,二者都會死去。 * 給定一個花壇(表示爲一個數組包含0和1,其中0表示沒種植花,1表示種植了花),和一個數 n 。可否在不打破種植規則的狀況下種入 n 朵花?能則返回True,不能則返回False。 * 來源:力扣(LeetCode) * 連接:https://leetcode-cn.com/problems/can-place-flowers * 著做權歸領釦網絡全部。商業轉載請聯繫官方受權,非商業轉載請註明出處。 * * Input: flowerbed = [1,0,0,0,1], n = 1 * Output: True * @Date: 2019/7/12 8:55 * @Version: 1.0 */ public class CanPlaceFlowers { public boolean solution(int[] flowerbed, int n) { //判斷每一個花之間要有空格 int count = 0; for(int i = 0; i < flowerbed.length && count < n; ++i) { //判斷是不是空的 if(flowerbed[i] == 0) { //判斷左右是否間隔1一個空位 int pre = i == 0 ? 0 : flowerbed[i - 1]; int next = i < flowerbed.length - 1 ? flowerbed[i + 1] : 0; if(pre == 0 && next == 0) { ++count; flowerbed[i] = 1; } } } if(count == n) { return true; } else { return false; } } public static void main(String args[]) { String as[] = {"bella","label","roller"}; int[] A = {1,0,0,0,1}; int k = 1; CanPlaceFlowers fuc = new CanPlaceFlowers(); System.out.println(fuc.solution(A, k)); } }
package y2019.Algorithm.array; import java.util.HashMap; import java.util.Map; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * @ClassName: FindPairs * @Author: xiaof * @Description: 532. K-diff Pairs in an Array * Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. * Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k. * * Input: [3, 1, 4, 1, 5], k = 2 * Output: 2 * Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5). * Although we have two 1s in the input, we should only return the number of unique pairs. * * 給定一個整數數組和一個整數 k, 你須要在數組裏找到不一樣的 k-diff 數對。這裏將 k-diff 數對定義爲一個整數對 (i, j), * 其中 i 和 j 都是數組中的數字,且兩數之差的絕對值是 k. * * 來源:力扣(LeetCode) * 連接:https://leetcode-cn.com/problems/k-diff-pairs-in-an-array * 著做權歸領釦網絡全部。商業轉載請聯繫官方受權,非商業轉載請註明出處。 * * @Date: 2019/7/12 9:33 * @Version: 1.0 */ public class FindPairs { public int solution(int[] nums, int k) { if(k < 0) { return 0; } //這題用相似hash的方式 Map<Integer, Integer> numMap = new HashMap(); for(int t : nums) { if(!numMap.containsKey(t)) { numMap.put(t, 0); } //出現次數 numMap.put(t, numMap.get(t) + 1); } //遍歷查詢缺失的通常 int count = 0; //根據map中的數據進行判斷,而且進行了去重 for(Map.Entry entry : numMap.entrySet()) { //判斷map中是否包含差值的數據,若是包含,還要避免是同同一對數據,剩餘的個數要不能爲0 if(k == 0) { //特殊處理K爲0的狀況 if((int) entry.getValue() >= 2) { ++count; } } else if(numMap.containsKey((int) entry.getKey() + k)) {//由於是遞增的,因此能夠吧相同對排除掉 ++count; } } return count; } public static void main(String args[]) { int[] A = {3,1,4,1,5}; int[] B = {1,2,3,4,5}; int k = -1; FindPairs fuc = new FindPairs(); System.out.println(fuc.solution(A, k)); } }