leetcode演算法題解(Java版)-3-廣搜+HashMap
一、運算子——異或”^”
題目描述
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
思路
- 題目很簡單,考到了一個知識點——異或:比較兩個運算元的二進位制的各個位置,相同則為0不同則為1。所以,1.與0異或是本身2.與和自己一樣的異或是0.
程式碼
public class Solution {
public int singleNumber(int[] A) {
int res=0;
for(int i=0;i<A.length;i++){
res^=A[i];
}
return res;
}
}
二、動態規劃
題目描述
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
What is the minimum candies you must give?
思路:
- 一開始看錯題了,孩子們本來站好隊了,我按權重從大到小排了一下。。。看了別人的程式碼,思路並不難:先給每人一個糖果,兩個迴圈解決問題,一,從前往後掃一遍,如果下一個比上一個權重大就在上一個基礎上加一;再從後往前掃一遍,如果前面的比後面的權重大且糖果比他少就再後面的基礎上加一重新整理原有的值。
- 語法點:
Arrays.fill(array,val);Arrays.sort(array);
程式碼
import java.util.Arrays;
public class Solution {
public int candy(int[] ratings) {
if(ratings==null||ratings.length==0){
return 0;
}
int len=ratings.length;
int [] cnt=new int [len];
Arrays.fill(cnt,1);
for(int i=1;i<len;i++){
if(ratings[i]>ratings[i-1]){
cnt[i]=cnt[i-1]+1;
}
}
int sum=0;
for(int i=len-1;i>0;i--){
if(ratings[i-1]>ratings[i]&&cnt[i-1]<=cnt[i]){
cnt[i-1]=cnt[i]+1;
}
sum+=cnt[i];
}
return sum+cnt[0];
}
}
三、模擬題(環狀)
題目描述
There are N gas stations along a circular route, where the amount of gas at station i isgas[i].
You have a car with an unlimited gas tank and it costscost[i]of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station`s index if you can travel around the circuit once, otherwise return -1.
Note:
The solution is guaranteed to be unique.
思路
- 設定start和end,分別放在首尾。如果能繼續走就讓end++,不能則讓start退一個。結束while迴圈有兩種可能結果,一個是sum>=0,則最後相會的點就是出發點,另一個則不可能。
程式碼
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int len=gas.length;
int start=len-1;
int end=0;
int sum=0;
sum=gas[start]-cost[start];
while(end<start){
if(sum>=0){
sum+=gas[end]-cost[end];
end++;
}
else{
start--;
sum+=gas[start]-cost[start];
}
}
return sum>=0?start:-1;
}
}
四、BFS+HashMap
題目描述
Clone an undirected graph. Each node in the graph contains alabeland a list of itsneighbors.
思路
- 廣搜,然後用map儲存原來的和克隆的一一對映
- HashMap中通過get()來獲取value,通過put()來插入value,containsKey()則用來檢驗物件是否已經存在
- Stack:
st.push(node);st.pop();st.empty();
程式碼
/**
* Definition for undirected graph.
* class UndirectedGraphNode {
* int label;
* ArrayList<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/
import java.util.Stack;
import java.util.HashMap;
public class Solution {
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if(node==null){
return null;
}
HashMap<UndirectedGraphNode,UndirectedGraphNode> map=new HashMap<>();
Stack<UndirectedGraphNode> stackNode=new Stack<>();
stackNode.push(node);
while(!stackNode.empty()){
UndirectedGraphNode tempNode=stackNode.pop();
if(map.containsKey(tempNode)){
continue;
}
UndirectedGraphNode copyNode=new UndirectedGraphNode(tempNode.label);
for(UndirectedGraphNode uNode:tempNode.neighbors){
copyNode.neighbors.add(uNode);
if(map.containsKey(uNode)){
continue;
}
stackNode.push(uNode);
}
map.put(tempNode,copyNode);
}
return map.get(node);
}
}
相關文章
- leetcode演算法題解(Java版)-9-N皇后問題LeetCode演算法Java
- leetcode演算法題解(Java版)-12-中序遍歷LeetCode演算法Java
- leetcode演算法題解(Java版)-14-第k小數問題LeetCode演算法Java
- leetcode演算法題解(Java版)-7-迴圈連結串列LeetCode演算法Java
- leetcode演算法題解(Java版)-16-動態規劃(單詞包含問題)LeetCode演算法Java動態規劃
- LeetCode 連結串列解題彙總 Java版LeetCodeJava
- LeetCode 熱題 HOT 100 Java題解——33. 搜尋旋轉排序陣列LeetCodeJava排序陣列
- [LeetCode題解]79. 單詞搜尋LeetCode
- LeetCode 熱題 HOT 100 Java題解——96. 不同的二叉搜尋樹LeetCodeJava
- 演算法(三):圖解廣度優先搜尋演算法演算法圖解
- LeetCode演算法題-Number of Boomerangs(Java實現)LeetCode演算法OOMJava
- Leetcode 演算法題解系列 - 最小棧LeetCode演算法
- leetcode 831題解【C++/Java/Python】LeetCodeC++JavaPython
- 【Java貓說】每日演算法:#3-列舉求就業率問題Java演算法就業
- 演算法之搜尋(Java版)-持續更新補充演算法Java
- java之HashMap用法講解JavaHashMap
- Leetcode 演算法題解系列 - 二維陣列快速查詢元素(二叉搜尋樹)LeetCode演算法陣列
- LeetCode演算法題LeetCode演算法
- LeetCode1160.拼寫單詞(Java+暴力+HashMap)LeetCodeJavaHashMap
- Java解決LeetCode72題 Edit DistanceJavaLeetCode
- 《劍指offer》JAVA題解,LeetCode評測JavaLeetCode
- LeetCode演算法題-Next Greater Element I(Java實現)LeetCode演算法Java
- JAVA系列:HashMap常見問題JavaHashMap
- Leetcode 題解演算法之動態規劃LeetCode演算法動態規劃
- LeetCode解題記錄(貪心演算法)(二)LeetCode演算法
- LeetCode解題記錄(貪心演算法)(一)LeetCode演算法
- LeetCode 資料庫解題彙總 MySql版LeetCode資料庫MySql
- 【LeetCode】初級演算法:排序和搜尋LeetCode演算法排序
- leetcode 455題 javaLeetCodeJava
- LeetCode 316. 去除重複字母 java題解LeetCodeJava
- Java實現 LeetCode 35 搜尋插入位置JavaLeetCode
- java面試題-HashMap的工作原理Java面試題HashMap
- #leetcode刷題之路3-無重複字元的最長子串LeetCode字元
- leetcode 刷題之深度優先搜尋LeetCode
- java解一些演算法題Java演算法
- leetcode排序專題演算法刷題LeetCode排序演算法
- 深度和廣度優先搜尋演算法演算法
- 演算法筆記(廣度優先搜尋)演算法筆記