算法-散列
一、散列
散列是一种用于以常数平均时间执行插入、删除和查找的技术。但是任何排序的信息都不会得到有效的支持。所以FindMax(),FindMin(),以及以线性时间打印的操作都是散列所不支持的。
理想的散列表数据结构值不过是一个包含有关键字的具有固定大小的数组。
二、子数组和等于K
class Solution {
public int subarraySum(int[] nums, int k) {
if (nums == null || nums.length == 0) {
return 0;
}
Map<Integer, Integer> map = new HashMap<>();
for (int i = 1; i < nums.length; i++) {
nums[i] += nums[i - 1];
}
int ans = 0;
int temp = 0;
map.put(0, 1);
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i] - k)) {
ans += map.get(nums[i] - k);
}
temp = map.containsKey(nums[i]) ? map.get(nums[i]) + 1 : 1;
map.put(nums[i], temp);
}
return ans;
}
}
三、图的克隆
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> neighbors;
public Node() {
val = 0;
neighbors = new ArrayList<Node>();
}
public Node(int _val) {
val = _val;
neighbors = new ArrayList<Node>();
}
public Node(int _val, ArrayList<Node> _neighbors) {
val = _val;
neighbors = _neighbors;
}
}
*/
class Solution {
public Node cloneGraph(Node node) {
if (node == null) {
return null;
}
ArrayList<Node> nodes = getNodes(node);
Map<Node, Node> mapping = new HashMap<>();
// copy node
for (Node n: nodes) {
mapping.put(n, new Node(n.val));
}
//copy neighbors
for (Node n: nodes) {
Node newNode = mapping.get(n);
for (Node neighbor: n.neighbors) {
Node newNeighbor = mapping.get(neighbor);
newNode.neighbors.add(newNeighbor);
}
}
return mapping.get(node);
}
public ArrayList<Node> getNodes(Node node) {
Queue<Node> queue = new LinkedList<Node>();
HashSet<Node> set = new HashSet<Node>();
// -> 1
queue.offer(node);
// -> 1
set.add(node);
while (!queue.isEmpty()) {
//round 1: head = 1, queue is empty, set - > 1
//round 2: head = 2, queue -> 4, set -> 1,2,4
// round 3 head = 4 queue -> 3 , set -> 1,2,3,4
//round 4 head = 3 queue is empty, set -> 1,2,3,4
Node head = queue.poll();
// round 1 neighbors = 2, 4
// after round 1 set 1,2,4 queue 2,4
// round 2 neighbors 1,3
// after loop set -> 1,2,3,4 queue 4, 3
//round 3 neighbors-> 1,3 queue 3
// round 4 neighbots -> 2,4 , queue is
for (Node neighbor: head.neighbors) {
if (!set.contains(neighbor)) {
set.add(neighbor);
queue.offer(neighbor);
}
}
}
return new ArrayList<Node>(set);
}
}
四、最长无重复子串
public class Solution {
/**
* @param s: a string
* @return: an integer
*/
public int lengthOfLongestSubstring(String s) {
// write your code here
Set<Character> set = new HashSet<>();
if (s == null || s.length() == 0) {
return 0;
}
int max = 0;
int right = 0;
for (int i = 0; i < s.length(); i++) {
while (right < s.length() && !set.contains(s.charAt(right))) {
set.add(s.charAt(right++));
max = Math.max(max, right - i);
}
set.remove(s.charAt(i));
}
return max;
}
}
public class Solution {
/**
* @param s: a string
* @return: an integer
*/
public int lengthOfLongestSubstring(String s) {
// write your code here
if (s == null || s.length() == 0) {
return 0;
}
int i = 0;
int j = 0;
int ans = 0;
int[] map = new int[256];
for (i = 0; i < s.length(); i++) {
while (j < s.length() && map[s.charAt(j)] == 0) {
map[s.charAt(j)] = 1;
ans = Math.max(ans, j - i + 1);
j++;
}
map[s.charAt(i)] = 0;
}
return ans;
}
}