本博客文章(学习笔记)导航 (点击这里访问)
![在这里插入图片描述](https://img-blog.csdnimg.cn/fe463387c4ab4c1c85662c7f78ceff23.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBATuKCgg==,size_20,color_FFFFFF,t_70,g_se,x_16)
BM97 旋转数组
描述
一个数组A中存有 n 个整数,在不允许使用另外数组的前提下,将每个整数循环向右移 M( M >=0)个位置,即将A中的数据由(A0 A1 ……AN-1 )变换为(AN-M …… AN-1 A0 A1 ……AN-M-1 )(最后 M 个数循环移至最前面的 M 个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?
数据范围:0<n≤100,0≤m≤1000
进阶:空间复杂度 O(1),时间复杂度 O(n)
示例1
输入:6,2,[1,2,3,4,5,6]
返回值:[5,6,1,2,3,4]
示例2
输入:4,0,[1,2,3,4]
返回值:[1,2,3,4]
思路1:
旋转整体
旋转两个局部
思路2:
新建数组,每个元素移动m个位置
import java.util. ;
public class Solution {
public int[] solve (int n, int m, int[] a) {
m=m%a.length;
reverse(0,a.length-1,a);
reverse(0,m-1,a);
reverse(m,a.length-1,a);
return a;
}
public void reverse(int n,int m,int[] a){
if(n>=m||n<0||m>a.length-1) return;
int l=n,r=m;
while(l<r){
int temp=a[l];
a[l]=a[r];
a[r]=temp;
r--;
l++;
}
}
}
BM98 螺旋矩阵
描述
给定一个m x n大小的矩阵(m行,n列),按螺旋的顺序返回矩阵中的所有元素。
数据范围:0≤n,m≤10,矩阵中任意元素都满足∣val∣≤100
要求:空间复杂度 O(nm) ,时间复杂度 O(nm)
示例1
输入:[[1,2,3],[4,5,6],[7,8,9]]
返回值:[1,2,3,6,9,8,7,4,5]
示例2
输入:[]
返回值:[]
思路:
定义四个顶点,逐渐缩小范围
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> spiralOrder(int[][] matrix) {
ArrayList<Integer> res=new ArrayList<Integer>();
if(matrix==null||matrix.length==0) return res;
int row=matrix.length,collum=matrix[0].length;
int top=0,left=0,down=row-1,right=collum-1;
while(left<=right&&top<=down){
for(int i=left;i<=right;i++){
res.add(matrix[top][i]);
}
for(int i=top+1;i<=down;i++){
res.add(matrix[i][right]);
}
if(left!=right&&top!=down){
for(int i=right-1;i>=left;i--){
res.add(matrix[down][i]);
}
for(int i=down-1;i>=top+1;i--){
res.add(matrix[i][left]);
}
}
left++;
right--;
top++;
down--;
}
return res;
}
}
BM99 顺时针旋转矩阵
描述
有一个NxN整数矩阵,请编写一个算法,将矩阵顺时针旋转90度。
给定一个NxN的矩阵,和矩阵的阶数N,请返回旋转后的NxN矩阵。
数据范围:0 < n < 300,矩阵中的值满足0≤val≤1000
要求:空间复杂度 O(N^2),时间复杂度 O(N^2)
进阶:空间复杂度 O(1),时间复杂度 O(N^2)
示例1
输入:[[1,2,3],[4,5,6],[7,8,9]],3
返回值:[[7,4,1],[8,5,2],[9,6,3]]
思路1 :
新建二维数组,依次把行放进列中
思路2 :
原地修改,左右左边互换,纵坐标=n-1-横坐标
思路3:
上下反转 + 对角线翻转
思路2:
import java.util. ;
public class Solution {
public int[][] rotateMatrix(int[][] mat, int n) {
int[][] temp = new int[n][n];
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
temp[j][n-1-i] = mat[i][j];
}
}
return temp;
}
}
思路3:
public int[][] rotateMatrix(int[][] matrix, int n) {
int length = matrix.length;
for (int i = 0; i < length / 2; i++) {
int temp[] = matrix[i];
matrix[i] = matrix[length - i - 1];
matrix[length - i - 1] = temp;
}
for (int i = 0; i < length; ++i) {
for (int j = i + 1; j < length; ++j) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
return matrix;
}
BM100 设计LRU缓存结构
描述
设计LRU(最近最少使用)缓存结构,该结构在构造时确定大小,假设大小为 k ,操作次数是 n ,并有如下两个功能
1. set(key, value):将记录(key, value)插入该结构
2. get(key):返回key对应的value值
提示:
1.某个key的set或get操作一旦发生,认为这个key的记录成了最常使用的,然后都会刷新缓存。
2.当缓存的大小超过k时,移除最不经常使用的记录。
3.输入一个二维数组与k,二维数组每一维有2个或者3个数字,第1个数字为opt,第2,3个数字为key,value
若opt=1,接下来两个整数key, value,表示set(key, value)
若opt=2,接下来一个整数key,表示get(key),若key未出现过或已被移除,则返回-1
对于每个opt=2,输出一个答案
4.为了方便区分缓存里key与value,下面说明的缓存里key用""号包裹
要求:set和get操作复杂度均为 O(1)
数据范围: 1≤k≤n≤10^5,∣key∣,∣val∣≤2×10^9
示例1
输入:[[1,1,1],[1,2,2],[1,3,2],[2,1],[1,4,4],[2,2]],3
返回值:[1,-1]
说明:
[1,1,1],第一个1表示opt=1,要set(1,1),即将(1,1)插入缓存,缓存是{"1"=1}
[1,2,2],第一个1表示opt=1,要set(2,2),即将(2,2)插入缓存,缓存是{"1"=1,"2"=2}
[1,3,2],第一个1表示opt=1,要set(3,2),即将(3,2)插入缓存,缓存是{"1"=1,"2"=2,"3"=2}
[2,1],第一个2表示opt=2,要get(1),返回是[1],因为get(1)操作,缓存更新,缓存是{"2"=2,"3"=2,"1"=1}
[1,4,4],第一个1表示opt=1,要set(4,4),即将(4,4)插入缓存,但是缓存已经达到最大容量3,移除最不经常使用的{"2"=2},插入{"4"=4},缓存是{"3"=2,"1"=1,"4"=4}
[2,2],第一个2表示opt=2,要get(2),查找不到,返回是[1,-1]
示例2
输入:[[1,1,1],[1,2,2],[2,1],[1,3,3],[2,2],[1,4,4],[2,1],[2,3],[2,4]],2
返回值:[1,-1,-1,3,4]
思路1:
借助LinkedHashMap
思路2;
自己构建双链表,用hashmap 来快速定位,用双向链表来快速插入和删除
import java.util. ;
public class Solution {
public int[] LRU (int[][] operators, int k) {
LRUCache lru=new LRUCache(k);
ArrayList<Integer> res=new ArrayList<Integer>();
for(int i=0;i<operators.length;i++){
if(operators[i][0]==1){
lru.set(operators[i][1],operators[i][2]);
}else if(operators[i][0]==2){
int temp= lru.get(operators[i][1]);
res.add(temp);
}
}
int[] r=new int[res.size()];
for(int i=0;i<res.size();i++){
r[i]=res.get(i);
}
return r;
}
class LRUCache{
LinkedHashMap<Integer,Integer> cache;
int SIZE;
public LRUCache(int k){
this.SIZE=k;
this.cache=new LinkedHashMap<Integer,Integer>();
}
public void set(int key,int value){
if(cache.containsKey(key)){
makeRecent(key);
}else if(cache.size()==SIZE){
int tkey=cache.entrySet().iterator().next().getKey();
cache.remove(tkey);
}
cache.put(key,value);
}
public int get(int key){
if(cache.containsKey(key)){
makeRecent(key);
return cache.get(key);
}else{
return -1;
}
}
public void makeRecent(int key){
int value=cache.get(key);
cache.remove(key);
cache.put(key,value);
}
}
}
import java.util. ;
class LRUCache {
class Node {
int k, v;
Node l, r;
Node(int _k, int _v) {
k = _k;
v = _v;
}
}
int n;
Node head, tail;
Map<Integer, Node> map;
public LRUCache(int capacity) {
n = capacity;
map = new HashMap<>();
head = new Node(-1, -1);
tail = new Node(-1, -1);
head.r = tail;
tail.l = head;
}
public int get(int key) {
if (map.containsKey(key)) {
Node node = map.get(key);
refresh(node);
return node.v;
}
return -1;
}
public void put(int key, int value) {
Node node = null;
if (map.containsKey(key)) {
node = map.get(key);
node.v = value;
} else {
if (map.size() == n) {
Node del = tail.l;
map.remove(del.k);
delete(del);
}
node = new Node(key, value);
map.put(key, node);
}
refresh(node);
}
void refresh(Node node) {
delete(node);
node.r = head.r;
node.l = head;
head.r.l = node;
head.r = node;
}
void delete(Node node) {
if (node.l != null) {
Node left = node.l;
left.r = node.r;
node.r.l = left;
}
}
}
public class Solution {
/
lru design
@param operators int整型二维数组 the ops
@param k int整型 the k
@return int整型一维数组
/
public int[] LRU (int[][] operators, int k) {
List<Integer> list = new ArrayList<>();
LRUCache lru = new LRUCache(k);
for (int[] op : operators) {
int type = op[0];
if (type == 1) {
lru.put(op[1], op[2]);
} else {
list.add(lru.get(op[1]));
}
}
int n = list.size();
int[] ans = new int[n];
for (int i = 0; i < n; i++) ans[i] = list.get(i);
return ans;
}
}
import java.util. ;
class LRUcache{
class Node {
int k,v;
Node l,r;
public Node(int k,int v){
this.k=k;
this.v=v;
}
}
HashMap<Integer,Node> map=new HashMap<Integer,Node>();
int size=0;
Node head,tail;
public LRUcache(int size){
this.size=size;
head=new Node(-1,-1);
tail=new Node(-1,-1);
head.r=tail;
tail.l=head;
}
public int get(int k){
if(map.containsKey(k)){
makeRecently(k);
return map.get(k).v;
}
return -1;
}
public void put(int k,int v){
if(map.containsKey(k)){
makeRecently(k);
map.get(k).v=v;
}else{
if(map.size()==size){
int deletek=tail.l.k;
deleteNode(deletek);
}
addHead(k,v);
}
}
public void addHead(int k,int v){
Node putNode=new Node(k,v);
Node tempput= head.r;
head.r=putNode;
putNode.l=head;
putNode.r=tempput;
tempput.l=putNode;
map.put(k,putNode);
}
public void makeRecently(int k){
if(map.containsKey(k)){
Node tempdelete=map.get(k);
deleteNode(k);
addHead(k,tempdelete.v);
}
}
public void deleteNode(int k){
Node tempdelete=map.get(k);
tempdelete.l.r=tempdelete.r;
tempdelete.r.l=tempdelete.l;
map.remove(k);
}
}
public class Solution {
public int[] LRU (int[][] operators, int k) {
LRUcache lru=new LRUcache(k);
ArrayList<Integer> res=new ArrayList<Integer>();
for(int i=0;i<operators.length;i++){
if(operators[i][0]==1){
lru.put(operators[i][1],operators[i][2]);
}else if(operators[i][0]==2){
int temp= lru.get(operators[i][1]);
res.add(temp);
}
}
int[] r=new int[res.size()];
for(int i=0;i<res.size();i++){
r[i]=res.get(i);
}
return r;
}
}
BM101 设计LFU缓存结构
描述
一个缓存结构需要实现如下功能。
set(key, value):将记录(key, value)插入该结构
get(key):返回key对应的value值
但是缓存结构中最多放K条记录,如果新的第K+1条记录要加入,就需要根据策略删掉一条记录,然后才能把新记录加入。这个策略为:在缓存结构的K条记录中,哪一个key从进入缓存结构的时刻开始,被调用set或者get的次数最少,就删掉这个key的记录;
如果调用次数最少的key有多个,上次调用发生最早的key被删除
这就是LFU缓存替换算法。实现这个结构,K作为参数给出
数据范围:0<k≤10^5,∣val∣≤2×10^9
要求:get和set的时间复杂度都是O(logn),空间复杂度是O(n)
若opt=1,接下来两个整数x, y,表示set(x, y)
若opt=2,接下来一个整数x,表示get(x),若x未出现过或已被移除,则返回-1
对于每个操作2,返回一个答案
示例1
输入:[[1,1,1],[1,2,2],[1,3,2],[1,2,4],[1,3,5],[2,2],[1,4,4],[2,1]],3
返回值:[4,-1]
说明:在执行"1 4 4"后,"1 1 1"被删除。因此第二次询问的答案为-1
备注: 1≤k≤n≤10^5 ; −2×10^9≤x,y≤2×10^9
思路1:
需要维持一个稳定排序的列表,get和set次数最少的先排除,如果get和set次数一致,先移除最老的那个
LinkedHashMap
import java.util. ;
public class Solution {
public int[] LFU (int[][] operators, int k) {
LFUCache cache=new LFUCache(k);
ArrayList<Integer> res=new ArrayList<>();
for(int i=0;i<operators.length;i++){
if(operators[i][0]==1){
cache.put(operators[i][1],operators[i][2]);
}else{
int n=cache.get(operators[i][1]);
res.add(n);
}
}
int[] resArray=new int[res.size()];
for(int i=0;i<res.size();i++){
resArray[i]=res.get(i);
}
return resArray;
}
}
class LFUCache{
int size=0;
LinkedHashMap<Integer,Node> map;
public LFUCache(int k){
this.size=k;
map=new LinkedHashMap<>();
}
public void put(int k,int v){
if(map.containsKey(k)){
makeRecently(k);
Node node=map.get(k);
node.value=v;
map.put(k,node);
}else{
if(map.size()>=this.size){
removeMin();
}
Node nodep=new Node(v,1);
map.put(k,nodep);
}
}
public int get(int k){
if(map.containsKey(k)){
makeRecently(k);
Node tempnode=map.get(k);
return tempnode.value;
}
return -1;
}
public void makeRecently(int k){
if(map.containsKey(k)){
Node node=map.get(k);
node.opsnum++;
map.remove(k);
map.put(k,node);
}
}
public void removeMin(){
int min=Integer.MAX_VALUE;
int index=-1;
for(Map.Entry<Integer,Node> m:map.entrySet()){
if(m.getValue().opsnum < min){
index=m.getKey();
min=m.getValue().opsnum;
}
}
if(min!=Integer.MAX_VALUE) {
map.remove(index);
}
}
}
class Node{
int value;
int opsnum;
public Node(int v,int opsnum){
this.value=v;
this.opsnum=opsnum;
}
}