Algorithm:523. 連續的子數組和
Review: 鴻蒙OS——安卓殺手
Tip/Tech:並查集
Share:印度會禁止使用部分一次性塑料
Algorithm
523. 連續的子數組和
https://leetcode-cn.com/problems/continuous-subarray-sum/
暴力法
原來以爲這樣不會通過的,但是沒想到還是過了。
class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
if (nums == null || nums.length < 2) {
return false;
}
for (int i = 0, len = nums.length; i < len - 1; ++i) {
int sumTemp = nums[i];
for (int j = i + 1; j < len; ++j) {
sumTemp += nums[j];
if (k != 0 && sumTemp % k == 0 ) {
return true;
} else if (k == 0 && sumTemp == 0) {
return true;
}
}
}
return false;
}
}
時間複雜度爲。這都能過了,簡直了。。哈哈
哈希表
這裏引用網友的解題思路:https://leetcode-cn.com/problems/continuous-subarray-sum/solution/lian-xu-de-zi-shu-zu-qiu-he-by-lenn123/
在方法一中我們利用前綴和數組來求解問題,對於子數組 (不包含下標 j ),其區間和爲 (其中 sum 爲預處理得到的前綴和數組),
我們要判斷的是 是否等於 0。
根據 mod 運算的性質,我們知道
故若想 ,則必有 。
但是思路是思路,要用代碼實現還是有難度的,主要是判斷0的情況。一般是實現倒是問題不大。
class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
if (nums == null || nums.length < 2) {
return false;
}
if (k == 0) {
for (int i = 0, len = nums.length; i < len - 1; ++i) {
if (nums[i] == 0 && nums[i + 1] == 0) {
return true;
}
}
return false;
}
HashMap<Integer, Integer> map = new HashMap<>();
map.put(0, -1);
int sum = 0;
for (int i = 0, len = nums.length; i < len; ++i) {
sum += nums[i];
sum = sum % k;
if (map.containsKey(sum)) {
if (i - map.get(sum) > 1) {
return true;
}
} else {
map.put(sum, i);
}
}
return false;
}
}
Review
https://raccoon.onyxbits.de/blog/harmony-os-the-android-killer/
Harmony OS - the Android Killer?
鴻蒙OS——安卓殺手?
文章分析了一些目前安卓和鴻蒙OS的情況。
其中分析到,其實以上的圖中,其實只有第一層谷歌的服務是谷歌自己獨有的,下面的兩層都是開源的。
文章也建議,鴻蒙OS要取代其實要花最大的力氣也是取代第一層。
至於剩下兩個其實不直接用就行了。
但是偏偏是第一層是最難以取代的,因爲第一層其實是谷歌的生態,你無法想象你在國外不能是使用谷歌的play,那是國外安卓系統上的最多的應用的下載起始點;gmail郵箱就不用說了,這幾乎是外國人的標配;youtube要是視頻都看不了,那麼玩手機還玩啥;map,谷歌地圖,這個就更不用說了,谷歌在國外這方面幾乎無敵。
所以以上這些服務纔是android系統的重中之重。
Tip/Tech
通用的並查集的實現是通過map來搞定的,幾乎成了萬能的並查集實現。
並查集的實現
public class UnionFindSet<V> {
class Element<V> {
public V value;
public Element(V value) {
this.value = value;
}
}
public HashMap<V, Element<V>> elementHashMap;
public HashMap<Element<V>, Element<V>> fatherMap;
public HashMap<Element<V>, Integer> rankMap;
/**
* @Description: 初始化操作
* @Param: [list]
* @return:
* @Author: James
* @Date:
*/
public UnionFindSet(List<V> list) {
elementHashMap = new HashMap<>();
fatherMap = new HashMap<>();
rankMap = new HashMap<>();
for (V value : list) {
Element<V> element = new Element<>(value);
elementHashMap.put(value, element);
fatherMap.put(element, element);
rankMap.put(element, 1);
}
}
private Element<V> findHead(Element<V> element) {
Stack<Element<V>> path = new Stack<>();
// 找父節點
while (element != fatherMap.get(element)) {
path.push(element);
element = fatherMap.get(element);
}
// 路徑壓縮
while (!path.isEmpty()) {
fatherMap.put(path.pop(), element);
}
return element;
}
public boolean isSameSet(V a, V b) {
if (elementHashMap.containsKey(a) && elementHashMap.containsKey(b)) {
return findHead(elementHashMap.get(a)) == findHead(elementHashMap.get(b));
}
return false;
}
/**
* @Description: 合併兩個兩個數值所在的結合
* @Param: [a, b]
* @return: void
* @Author: James
* @Date: 2019/5/19
*/
public void union(V a, V b) {
if (elementHashMap.containsKey(a) && elementHashMap.containsKey(b)) {
Element<V> aF = findHead(elementHashMap.get(a));
Element<V> bF = findHead(elementHashMap.get(b));
// 若相同就表示已經在一個集合中了
if (aF != bF) {
// Element<V> big = rankMap.get(aF) >= rankMap.get(bF) ? aF : bF;
// Element<V> small = big == aF ? bF : aF;
Element<V> big = aF;
Element<V> small = bF;
if (rankMap.get(aF) < rankMap.get(bF)) {
big = bF;
small = aF;
}
fatherMap.put(small, big);
rankMap.put(big, rankMap.get(aF) + rankMap.get(bF));
rankMap.remove(small);
}
}
}
}
Share
https://www.ndtv.com/india-news/pm-narendra-modi-says-at-un-climate-meet-that-like-india-even-world-should-say-goodbye-to-single-use-2097878
India Will Ban Single-Use Plastic, World Should Too: PM Modi At UN Meet
莫迪總理在聯合國會議上:印度將禁止一次性塑料,世界也應該禁止
印度即將在聖雄甘地誕辰10月2日之前在全國範圍內禁止使用六種塑料製品。這六個物品是塑料袋,杯子,盤子,小瓶,吸管和某些類型的香囊。
環境問題已經成爲了不管是發達國家還是發展中國家不得不面對的一個問題。
包括今年的上海的垃圾分類。還是印度今年的禁止使用塑料產品都表現了各國對改良環境的努力。
其實垃圾分類是肯定會在全國範圍內施行的。