Leetcode 相关知识点 day3
/用于作者每日有需所作笔记/
1.有序矩阵中第K小的元素
给你一个 n x n 矩阵 matrix ,其中每行和每列元素均按升序排序,找到矩阵中第 k 小的元素。
改进的搜索法
class Solution {
public int kthSmallest(int[][] matrix, int k) {
int l = sqrt(k);
PriorityQueue<Integer> que = new PriorityQueue<Integer>((a, b) -> (a - b));
for(int i = 1; i <= k && i<=matrix.length;i++){
for(int j = 1; j <k/i+1 && j<=matrix[0].length;j++ ){
que.offer(matrix[i-1][j-1]);
}
}
for(int i = 1; i < k; i++){
que.poll();
}
return que.peek();
}
public int sqrt(int k){
for(int i = 1; i<=k; i++){
if(i*i>=k){
return i;
}
}
return k;
}
}
利用归并排序原理,
核心在于pq.offer(new int[]{matrix[now[1]][now[2] + 1], now[1], now[2] + 1});
首先取第一列入堆栈,入栈的数据自动从大到小排序,之后出栈的第k个就是要找的值。
注意的是,每次出栈一位,需要将它右侧数字入栈。
class Solution {
public int kthSmallest(int[][] matrix, int k) {
PriorityQueue<int[]> pq = new PriorityQueue<int[]>(new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return a[0] - b[0];
}
});
int n = matrix.length;
for (int i = 0; i < n; i++) {
pq.offer(new int[]{matrix[i][0], i, 0});
}
for (int i = 0; i < k - 1; i++) {
int[] now = pq.poll();
if (now[2] != n - 1) {
pq.offer(new int[]{matrix[now[1]][now[2] + 1], now[1], now[2] + 1});
}
}
return pq.poll()[0];
}
}
2.如何从 Map 中获取键和值?
Map<String, String> map = new HashMap<>();
// 获取键和值
for (Map.Entry<String, String> entry : map.entrySet()) {
String k = entry.getKey();
String v = entry.getValue();
System.out.println("键: " + k + ", 值: " + v);
}
// Java 8 Lambda
map.forEach((k, v) -> {
System.out.println("键: " + k + ", 值: " + v);
});
Map<String, String> map = new HashMap<>();
map.put("db", "MySQL");
map.put("username", "cmower");
map.put("password", "123456");
// 获取键和值
for (Map.Entry<String, String> entry : map.entrySet()) {
String k = entry.getKey();
String v = entry.getValue();
System.out.println("键: " + k + ", 值: " + v);
}
// 获取所有键
Set<String> keys = map.keySet();
for (String k : keys) {
System.out.println("键: " + k);
}
// 获取所有值
Collection<String> values = map.values();
for (String v : values) {
System.out.println("值: " + v);
}
// Java 8 Lambda
map.forEach((k, v) -> {
System.out.println("键: " + k + ", 值: " + v);
});
键: password, 值: 123456
键: db, 值: MySQL
键: username, 值: cmower
键: password
键: db
键: username
值: 123456
值: MySQL
值: cmower
键: password, 值: 123456
键: db, 值: MySQL
键: username, 值: cmower
Map<Integer, Integer> map2 = new HashMap<>();
map2.put(1, 2);
map2.put(5, 4);
map2.put(8, 0);
Set gg = map2.keySet();
Collection ff = map2.values();
System.out.println(gg);
System.out.println(ff);
System.out.println(gg.contains(2));
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
PriorityQueue<Map.Entry<Integer, Integer>> que = new PriorityQueue<>((a, b) -> (b.getValue() - a.getValue()));
将map整个存入 que队列并自动排序。按map值从小到大排序。
3.queue补充
自带排序队列
PriorityQueue<int[]> que = new PriorityQueue<>((a, b) -> (b[0] - a[0]));
que.add(new int[]{1, 2});
que.add(new int[]{3, 10});
que.add(new int[]{2, 1});
System.out.println(que.peek()[1]);
普通队列
Queue<String> queue = new LinkedList();
注意 队列queue 是先进先出!!!!!!!!!!!
4.Stack
堆栈是先进后出
初始化
Stack stack=new Stack();
判断Stack是否为空
isEmpty()
添加元素
push(E item)
获取栈顶值,元素不出栈(栈为空时抛异常)
peek()
是否存在obj
search(Object obj)
移除栈顶
pop()
stack 作为 list,具备 list 常用方法,如:
//获取stack长度
size()
//下标处添加
add(int index, E element)
//添加集合
addAll(Collection<? extends E> c)
//移除对象
remove(Object obj)
//根据下标移除对象
remove(int index)
//清空
clear()
5.计算机
class Solution {
public int calculate(String s) {
Stack<Integer> queue = new Stack<Integer>();
int num = 0;
int res = 0;
char preSign = '+';
for(int i = 0;i < s.length();i++){
if(Character.isDigit(s.charAt(i))){
num = num*10 + s.charAt(i)- '0';
}
if(!Character.isDigit(s.charAt(i)) && s.charAt(i) != ' ' || i == s.length() - 1) {
switch(preSign){
case '+':
queue.push(num);
break;
case '-':
queue.push(-num);
break;
case '*':
queue.push(queue.pop()*num);
break;
default:
queue.push(queue.pop()/num);
}
preSign = s.charAt(i);
num = 0;
}
}
while (!queue.isEmpty()) {
res += queue.pop();
}
return res;
}
}
注意: 必须用堆栈来存储,或者双向列表。
禁用 queue<Integer> que = new queue<Integer>();
》他属于先进先出,会计算错误。
6.迭代器遍历
public class NestedIterator implements Iterator<Integer> {
private List<Integer> vals;
private Iterator<Integer> cur;
public NestedIterator(List<NestedInteger> nestedList) {
vals = new ArrayList<Integer>();
dfs(nestedList);
cur = vals.iterator();
}
@Override
public Integer next() {
return cur.next();
}
@Override
public boolean hasNext() {
return cur.hasNext();
}
private void dfs(List<NestedInteger> nestedList) {
for (NestedInteger nest : nestedList) {
if (nest.isInteger()) {
vals.add(nest.getInteger());
} else {
dfs(nest.getList());
}
}
}
}
补充:“123”转整数123 Integer.valueOf(“123”);