数据结构与算法-队列之leetcode练习(641,239,622)

21、深入了解SELinux安全系统与/proc文件系统 本文深入探讨了SELinux安全系统与/proc文件系统的原理及实际应用。详细介绍了SELinux的安全上下文、域转换、运行模式、角色管理以及策略查看工具seinfo的使用;同时解析了/proc文件系统的结构与功能,涵盖进程信息查看、系统资源监控及性能优化方法。结合SELinux与/proc的协同应用,提供了系统问题排查流程与安全管理最佳实践,帮助用户提升Linux系统的安全性与稳定性。 阅读详情

设计实现双端循环队列

 

641. Design Circular Deque

 

Design your implementation of the circular double-ended queue (deque).

 

Your implementation should support following operations:

 

MyCircularDeque(k): Constructor, set the size of the deque to be k.

insertFront(): Adds an item at the front of Deque. Return true if the operation is successful.

insertLast(): Adds an item at the rear of Deque. Return true if the operation is successful.

deleteFront(): Deletes an item from the front of Deque. Return true if the operation is successful.

deleteLast(): Deletes an item from the rear of Deque. Return true if the operation is successful.

getFront(): Gets the front item from the Deque. If the deque is empty, return -1.

getRear(): Gets the last item from Deque. If the deque is empty, return -1.

isEmpty(): Checks whether Deque is empty or not. 

isFull(): Checks whether Deque is full or not.

 

 

Example:

 

MyCircularDeque circularDeque = new MycircularDeque(3); // set the size to be 3

circularDeque.insertLast(1);                      // return true

circularDeque.insertLast(2);                      // return true

circularDeque.insertFront(3);                    // return true

circularDeque.insertFront(4);                    // return false, the queue is full

circularDeque.getRear();                         // return 2

circularDeque.isFull();                                  // return true

circularDeque.deleteLast();                       // return true

circularDeque.insertFront(4);                    // return true

circularDeque.getFront();                           // return 4

 

 

Note:

 

All values will be in the range of [0, 1000].

The number of operations will be in the range of [1, 1000].

Please do not use the built-in Deque library.

 

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/design-circular-deque

 

使用链表来实现双端队列

使用链表实现起来较为容易,记录下队头与队尾的指针,对其节点进行添加与删除,就可以很容易的实现双端队列

 

还是看代码吧

/**
 * design implement circular double-ended queue
 *
 *
<p>use linked list
 *
 * @author liujun
 * @version 0.0.1
 * @date 2019/08/25
 */

public class MyCircularDequeLinked {

 
class Node {
   
private int val;

    private
Node next;

    private
Node prev;

    public
Node(int val) {
     
this.val = val;
   
}
  }

 
private final int capacity;

  private int
size;

  private
Node root = new Node(-1);

  private
Node tail = root;

 
/** Initialize your data structure here. Set the size of the deque to be k. */
 
public MyCircularDequeLinked(int k) {
   
this.capacity = k;
   
size = 0;
 
}

 
/** Adds an item at the front of Deque. Return true if the operation is successful. */
 
public boolean insertFront(int value) {

   
if (size < capacity) {

      Node tmpValue =
new Node(value);
     
tmpValue.prev = root;
     
tmpValue.next = root.next;

     
// set next
     
if (root.next != null) {
       
root.next.prev = tmpValue;
     
}
     
     
root.next = tmpValue;

      if
(tail == root) {
       
tail = tmpValue;
     
}

     
size++;
      return true;
   
} else {
     
return false;
   
}
  }

 
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
 
public boolean insertLast(int value) {
   
if (size < capacity) {
      Node tmpValue =
new Node(value);
     
tmpValue.prev = tail;
     
tail.next = tmpValue;
     
tail = tmpValue;

     
size++;
      return true;
   
} else {
     
return false;
   
}
  }

 
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
 
public boolean deleteFront() {
   
if (size > 0) {
      Node front =
root.next;
      if
(front.next != null) {
        front.
next.prev = root;
       
root.next = front.next;
     
} else {
       
root.next = null;
       
tail = root;
     
}

     
size--;
      return true;
   
} else {
     
return false;
   
}
  }

 
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
 
public boolean deleteLast() {
   
if (size > 0) {
      Node lastPrent =
tail.prev;
     
lastPrent.next = tail.next;
     
tail = lastPrent;
     
size--;
      return true;
   
} else {
     
return false;
   
}
  }

 
/** Get the front item from the deque. */
 
public int getFront() {
   
if (size > 0) {
     
return root.next.val;
   
} else {
     
return -1;
   
}
  }

 
/** Get the last item from the deque. */
 
public int getRear() {
   
if (size > 0) {
     
return tail.val;
   
} else {
     
return -1;
   
}
  }

 
public int size() {
   
return size;
 
}

 
/** Checks whether the circular deque is empty or not. */
 
public boolean isEmpty() {
   
return size == 0;
 
}

 
/** Checks whether the circular deque is full or not. */
 
public boolean isFull() {
   
return size == capacity;
 
}
}

 

使用数组来实现双端队列

此使用循环队列来实现,在实现上比链式队列,会复杂很多,队尾的处理与单循环队列无异,但还需要处理队头的指针位置时,在队头插入与删除操作,我画了个图,比较直观点

 

 

 

代码实现

 


/**
 * use array implement circular double-ended queue
 *
 * @author liujun
 * @version 0.0.1
 * @date 2019/08/26
 */

public class MyCircularDequeArray {

 
private final int capacity;

  private final int
[] array;

  private int
size;

  private int
head;

  private int
tail;

 
/** Initialize your data structure here. Set the size of the deque to be k. */
 
public MyCircularDequeArray(int k) {
   
this.array = new int[k + 1];
    this
.capacity = k + 1;
   
size = 0;
 
}

 
/** Adds an item at the front of Deque. Return true if the operation is successful. */
 
public boolean insertFront(int value) {
   
// when queue is full
   
if ((tail + 1) % capacity == head) {
     
return false;
   
} else {

     
head = (head + capacity - 1) % capacity;
     
array[head] = value;
     
size++;

      return true;
   
}
  }

 
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
 
public boolean insertLast(int value) {
   
// when queue is full
   
if ((tail + 1) % capacity == head) {
     
return false;
   
} else {
     
array[tail] = value;
     
tail = (tail + 1) % capacity;
     
size++;
      return true;
   
}
  }

 
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
 
public boolean deleteFront() {
   
if (tail == head) {
     
return false;
   
} else {
     
head = (head + 1) % capacity;
     
size--;
      return true;
   
}
  }

 
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
 
public boolean deleteLast() {
   
if (tail == head) {
     
return false;
   
} else {
     
tail = (tail - 1 + capacity) % capacity;
     
size--;
      return true;
   
}
  }

 
/** Get the front item from the deque. */
 
public int getFront() {
   
if (tail == head) {
     
return -1;
   
} else {
     
return array[head];
   
}
  }

 
/** Get the last item from the deque. */
 
public int getRear() {
   
if (tail == head) {
     
return -1;
   
} else {
     
int readIndex = (tail - 1 + capacity) % capacity;
      return
array[readIndex];
   
}
  }

 
public int size() {
   
return size;
 
}

 
/** Checks whether the circular deque is empty or not. */
 
public boolean isEmpty() {
   
return size == 0;
 
}

 
/** Checks whether the circular deque is full or not. */
 
public boolean isFull() {
   
return (tail + 1) % capacity == head;
 
}
}

 

 

求滑动窗口的最大值

Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window.

 

Example:

 

Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3

Output: [3,3,5,5,6,7]

Explanation:

 

Window position                Max

---------------               -----

[1  3  -1] -3  5  3  6  7       3

 1 [3  -1  -3] 5  3  6  7       3

 1  3 [-1  -3  5] 3  6  7       5

 1  3  -1 [-3  5  3] 6  7       5

 1  3  -1  -3 [5  3  6] 7       6

 1  3  -1  -3  5 [3  6  7]      7

Note:

You may assume k is always valid, 1 ≤ k ≤ input array's size for non-empty array.

 

Follow up:

Could you solve it in linear time?

 

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/sliding-window-maximum

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

 

使用双端队实现

此实现就是在窗口中求最大值。可用使用一个双端队列实现,双端队列的大小为窗口的大小,每放入一个元素,从队头移除一个元素,又向队尾追加一个元素。在队列中求中最大值。还是来画个图,更为直观点吧!

使用java原生的LinkedList实现


/**
 *
使用双端队列来解决此滑动窗口的问题
 
*
 * @author liujun
 * @version 0.0.1
 * @date 2018/11/17
 */

public class SolutionDequeue {

 
/**
   *
求滑动窗口的最大值
  
*
   *
<p>由于需要对每个窗口做求最大数,时间复杂度为O(N*K)
   *
   * @param
nums 输入数据集
  
* @param k 窗口大小
  
* @return 窗口内最大值
  
*/
 
public int[] maxSlidingWindow(int[] nums, int k) {

   
if (null == nums) {
     
return null;
   
}

   
if (nums.length == 0) {
     
return nums;
   
}

   
if (k > nums.length) {
     
return null;
   
}

   
int[] result = new int[nums.length - k + 1];

    int
readIndex = 0;

   
// 读取首次记录
   
LinkedList<Integer> dequeue = new LinkedList<>();

    for
(int i = 0; i < nums.length; i++) {
     
// 移除比当前元素小的对象
     
while (!dequeue.isEmpty() && nums[dequeue.peekLast()] <= nums[i]) {
        dequeue.pollLast()
;
     
}
      dequeue.add(i)
;

     
// 进行窗口滑动的调整
     
while (dequeue.peekFirst() < i - k + 1) {
        dequeue.pollFirst()
;
     
}

     
if (i + 1 >= k) {
        result[readIndex++] = nums[dequeue.peekFirst()]
;
     
}
    }

   
return result;
 
}
}

 

 

 

使用循环双端队实现

 

使用循环队列来替代linkedList,解题思路与前面队列一致

 

 


/**
 *
使用双端循环队列来解决求窗口最大值
 
*
 * @author liujun
 * @version 0.0.1
 * @date 2019/08/27
 */

public class SolutionCycleArrayQueue {

 
/**
   *
求滑动窗口的最大值
  
*
   * @param
nums 输入数据集
  
* @param k 窗口大小
  
* @return 窗口内最大值
  
*/
 
public int[] maxSlidingWindow(int[] nums, int k) {
   
if (null == nums || nums.length == 0 || nums.length <= k) {
     
return nums;
   
}
   
int[] reslut = new int[nums.length - k + 1];

   
//使用一个循环队列
   
int[] queue = new int[k];

   
// 加载值
   
for (int i = 0; i < k; i++) {
      queue[i] = nums[i]
;
   
}

   
// 求最大值
   
int readIndex = 0;
   
reslut[readIndex] = maxValue(queue);
   
readIndex++;

    int
addnextPosition = 0;

    for
(int i = k; i < nums.length; i++) {
      queue[addnextPosition] = Integer.
MIN_VALUE;
     
queue[addnextPosition] = nums[i];
     
reslut[readIndex++] = maxValue(queue);
     
addnextPosition = (addnextPosition + 1) % k;
   
}

   
return reslut;
 
}

 
private int maxValue(int[] value) {
   
int maxValue = value[0];
    for
(int i = 1; i < value.length; i++) {
     
if (maxValue < value[i]) {
        maxValue = value[i]
;
     
}
    }
   
return maxValue;
 
}
}

 

 

 

 

设计实现一个循环双端队列

Design your implementation of the circular queue.
The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out)
principle and the last position is connected back to the first position to make a circle.
It is also called "Ring Buffer".

One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue,
once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue.
But using the circular queue, we can use the space to store new values.

Your implementation should support following operations:

MyCircularQueue(k): Constructor, set the size of the queue to be k.
Front: Get the front item from the queue. If the queue is empty, return -1.
Rear: Get the last item from the queue. If the queue is empty, return -1.
enQueue(value): Insert an element into the circular queue. Return true if the operation is successful.
deQueue(): Delete an element from the circular queue. Return true if the operation is successful.
isEmpty(): Checks whether the circular queue is empty or not.
isFull(): Checks whether the circular queue is full or not.
 

Example:

MyCircularQueue circularQueue = new MyCircularQueue(3); // set the size to be 3
circularQueue.enQueue(1);  // return true
circularQueue.enQueue(2);  // return true
circularQueue.enQueue(3);  // return true
circularQueue.enQueue(4);  // return false, the queue is full
circularQueue.Rear();  // return 3
circularQueue.isFull();  // return true
circularQueue.deQueue();  // return true
circularQueue.enQueue(4);  // return true
circularQueue.Rear();  // return 4
 
Note:

All values will be in the range of [0, 1000].
The number of operations will be in the range of [1, 1000].
Please do not use the built-in Queue library.

 

 

我的实现

特别需要注意判断空

 


/**
 * @author liujun
 * @version 0.0.1
 * @date 2019/08/29
 */

public class MyCircularQueue {

 
private final int[] array;

  private final int
capacity;

  private int
head;

  private int
tail;

  private int
size;

 
/** Initialize your data structure here. Set the size of the queue to be k. */
 
public MyCircularQueue(int k) {
   
this.array = new int[k + 1];
    this
.capacity = k + 1;
 
}

 
/** Insert an element into the circular queue. Return true if the operation is successful. */
 
public boolean enQueue(int value) {
   
if ((tail + 1) % capacity == head) {
     
return false;
   
}
   
array[tail] = value;
    
tail = (tail + 1) % capacity;
   
size++;
    return true;
 
}

 
/** Delete an element from the circular queue. Return true if the operation is successful. */
 
public boolean deQueue() {
   
if (tail == head) {
     
return false;
   
}
   
head = (head + 1) % capacity;
   
size--;
    return true;
 
}

 
/** Get the front item from the queue. */
 
public int Front() {
   
if (this.isEmpty()) {
     
return -1;
   
}

   
return this.array[head];
 
}

 
/** Get the last item from the queue. */
 
public int Rear() {
   
if (this.isEmpty()) {
     
return -1;
   
}

   
int tailiIndex = (tail - 1 + capacity) % capacity;
    return
array[tailiIndex];
 
}

 
/** Checks whether the circular queue is empty or not. */
 
public boolean isEmpty() {
   
return tail == head;
 
}

 
/** Checks whether the circular queue is full or not. */
 
public boolean isFull() {
   
return (tail + 1) % capacity == head;
 
}

 
public int size() {
   
return size;
 
}
}

 

基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的农作物害虫检测系统(深度学习模型+UI界面+训练数据集) 随着人工智能技术在农业领域的深入应用,基于深度学习的害虫检测系统已成为智慧农业的重要组成部分。本文详细介绍了一种基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的农作物害虫检测系统,涵盖算法原理、数据集构建、模型训练、系统实现及完整代码。该系统能够实时检测并识别多种农作物害虫,为农业生产提供精准的病虫害监测解决方案。 阅读详情

相关推荐

GPU运维面试题

【代码】GPU运维面试题。

u011220233的博客 695

Deque 的pollFirst,pollLast,offerLast

接口中的两个方法,用于从双端队列(Deque)中检索和删除元素。这两者的主要区别在于它们对队列的操作方向。一般来说,如果你只关心是否成功将元素添加到末尾,并且希望避免异常,可以使用。: 从双端队列的尾部(末尾)检索并删除元素。: 从双端队列的头部(开头)检索并删除元素。都用于将元素添加到数据结构的末尾。在Java中的现代编码中,推荐使用。类,并且不关心异常,那么。

呆咖啡的博客 2549

1020空心杯电机驱动设计与ESP32实战

空心杯电机是一种高动态响应的直流微特电机,其驱动本质是解决电感负载下的大di/dt启动、反电动势耦合与电流纹波抑制问题。核心原理在于建立包含内阻、电感和反电动势的等效电路模型,并据此匹配开关器件、PWM参数与采样闭环。技术价值体现在小体积下实现毫秒级响应、低噪声(>30kHz)、高精度电流保护(±50mA)及热鲁棒性。典型应用于微型航模、手持云台与精密执行器等对功率密度和动态性能敏感的嵌入式系统。本文以1020规格空心杯电机为对象,基于ESP32平台完成从MOSFET选型、32kHz硬件PWM生成到INA2

weixin_42298164的博客 580

java 方法 示例_Java ArrayDeque pollLast()方法与示例

java 方法 示例 ArrayDeque类pollLast()方法 (ArrayDeque Class pollLast() method) pollLast() Method is available in java.lang package. pollLast()方法在java.lang包中可用。 pollLast() Method is used to return the last el...

cumtb2009的博客 2036

Claude Code vs Codex实战对比:哪个更适合你的Next.js项目?(含真实案例)

本文通过两个真实的Next.js项目案例,深度对比了Claude Code与Codex在UI实现和业务逻辑构建中的表现。Claude Code在代码质量、设计还原度和架构完整性方面优势明显,适合对可维护性要求高的生产级项目;而Codex则以快速响应和低成本见长,更适合原型开发或内部工具。文章提供了基于项目类型、团队能力和成本效益的详细选择策略。

weixin_29201859的博客 806

Shader Graph实战:5分钟搞定Flow Map水体流动效果(含UV偏移避坑指南)

本文详细解析了如何使用Unity的Shader Graph工具快速实现Flow Map水体流动效果,涵盖基础原理、核心节点搭建、高级技巧及性能优化。特别针对UV偏移问题提供避坑指南,帮助开发者轻松打造专业级动态水面效果,适合游戏场景增强视觉表现。

weixin_29250403的博客 244

java arraydeque poll,Java ArrayDeque pollLast()方法

Java ArrayDeque pollLast()方法java.util.ArrayDeque.pollLast()此方法检索并删除此双端队列的最后一个元素,如果此双端队列为空,则返回null。1 语法public E pollLast()2 参数无3 返回值此方法返回此双端队列的尾部;如果此双端队列为空,则返回null。4 示例package com.yiidian;/*** 一点教程网: ...

weixin_31315007的博客 468

给STM32供电,别只接VDD和GND!VBAT、VDDA这些引脚不接会怎样?

本文深入解析STM32电源设计中VBAT与VDDA引脚的关键作用,揭示仅连接VDD和GND可能导致的ADC精度下降、RTC功能异常等问题。通过实测数据对比不同连接方案,提供包括滤波电路设计、PCB布局布线和双电源自动切换在内的实用解决方案,帮助开发者优化系统稳定性与性能。

weixin_30521161的博客 621

Java集合系列(三)—— 使用LinkedList实现堆栈,队列

前言:岁月无情,善待眼前人。 原文出处: //使用LinkedList实现队列 import java.util.*; class MyQueue { private LinkedList link; public MyQueue() { link = new LinkedList(); } //入队 public void i

让技术为我所用 1920

【GameFramework扩展应用】6-5、GameFramework框架出包时增加版本信息Version.txt文件

框架在热更新的时候,会先去获取文件,然后获取版本号,对比版本号和资源大小,才会开始下载资源。但是,框架生成AssetBundle资源的包里面是没有这个文件的,只能我们新建一个文件,然后手动填入数据,非常麻烦。好消息是,框架把这些接口都留给我们了,我们只需要继承接口,重写函数,然后在打包的时候选择这个辅助器即可。下面就来看一下如何实现把。

优快云博客专家,Unity3D高级软件工程师,《Unity 3D从入门到实战》作者。 1037

OpenClaw进阶实战(二十一):工作流6:跨平台舆情监控与自动响应

摘要: 本文介绍基于OpenClaw构建自动化舆情监控系统,实现多平台数据采集、AI情感分析与智能预警。系统架构包含数据采集(微博/小红书/抖音)、消息队列、情感分析(SnowNLP/大模型API)、分级预警和自动响应模块。通过技能化开发(如微博API采集、小红书模拟登录)实现7×24小时监控,结合情感强度与传播速度分级处理负面舆情,形成"采集-分析-预警-处置"闭环,适用于企业私有化部署。

PeterPan的博客 299

java deque.pop_Java - dequeue的介紹及用法

閱讀時間: 15分鐘在電腦科學中, dequeue是指a double-ended queue(雙向佇列),一般簡寫為deque,讀音是 deck)。Dequeue是一種抽象資料型態,dequeue中的每個元素都可以在佇列中的頭部(左邊)或者在尾部(右邊)的位置進行任何元素的新增移除。java.util.Deque interface 是 java.util.Queue interface 的一個...

weixin_29215391的博客 1835

在 FreeStyle SAP UI5 应用里优雅落地 Draft 草稿机制:用 DraftManager 封装 DraftController 的一套实战套路

SAP Fiori应用中,Draft草稿机制是解决业务中断、多人协作和长表单填写等痛点的核心能力。本文提出了一套针对FreeStyle SAP UI5应用的Draft管理方案,通过封装sap.ui.generic.app.transaction.DraftController为DraftManager服务类,将复杂的草稿操作逻辑集中管理。该方案实现了草稿的创建、激活、丢弃等核心功能,使控制器只需调用简单意图式API即可完成操作,同时解决了组件化架构下的上下文切换、统一错误处理等问题。文章还详细介绍了后端Dr

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 2751

关于deque的offerLast和polllast方法和stack的区别

一个小细节:在Java 代码中,stack 并未用 push() 和 pop() ,而用的是 offerLast() 和 pollLast() 。 这是因为 Deque 提供的 push() 和 pop() 是 “头插头取”,这与 ans.add(new ArrayList(stack)); 所需的顺序刚好相反,所以才改用了 “尾插尾取”。(其实我认为既然要 “尾插尾取”,还不如直接使用 List 呢) ...

Charles的博客 2103

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

LeetCode刷题之队列

641. 设计循环双端队列 设计实现双端队列。 你的实现需要支持以下操作: MyCircularDeque(k):构造函数,双端队列的大小为k。 insertFront():将一个元素添加到双端队列头部。 如果操作成功返回 true。 insertLast():将一个元素添加到双端队列尾部。如果操作成功返回 true。 deleteFront():从双端队列头部删除一个元素。 如果操作成功返回 ...

辰星的博客 446

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

leetcode---用栈实现队列、用队列实现栈(栈和队列的习题)

用队列实现栈 使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 class MyStack { public: MyStack() {} void push(int x) //放入元素 { if (!q1.empty()) q1.push(x); else q2.pu...

weixin_43219708的博客 666

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

LeetCode 23. 合并K个排序链表(优先队列)

文章目录1. 题目信息2. 思路3. 代码 1. 题目信息 合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。 示例: 输入: [ 1->4->5, 1->3->4, 2->6 ] 输出: 1->1->2->3->4->4->5->6 来源:力扣(LeetCode) 链接:https://l...

Michael是个半路程序员 1453

如何在单台8G内存机器上实现1TB日志排序?

问题: 最近在面试人的时候,我经常会问一个问题,题目如下: 现在有一批日志数据,大小为1TB,数据是某一天的生产的日志数据,现在只有一台单CPU及8G内存的机器,请使用算法对此日志数据按时间的先后顺序进行排序。 可面试了好多人,都没有回答出这个问题 针对这个问题今天来讲讲这个问题我的解法。 解决方案1:桶排序 它的原理是利用分区字段的特点,将数据分别写入到多个桶文件中...

bug_null的博客 3837

二分查找

二分查找算法 描述 二分查找也称折半查找(Binary Search),它是一种效率非常高效的查找方法。但是折半查找要求线性表必须采用顺序存储结构,而且表中元素按关键字有序排列。 那它的效率有多高呢?还在给一个直观的数据做个展示吧,在一个1到2亿的个有充数组内查找一个给定值,使用二分查找需要多少次呢?最多也只需要28次。 ##时间复杂度 二分查找的时间复杂非常的高,那高效到什么程序呢? 假设数据大小是n,每次查找都会缩小为原来的一半,即为除2操作,最坏情况下,直到查找区间缩小为空,才停止 被查找到的大小区间

bug_null的博客 2896

自己动手实现搜索引擎之数据采集

自己动手实现搜索引擎之数据采集 曾经在学生时代,就很想实现一个自己的搜索引擎,但当时没有这个技术,偶然的一个机会,有幸参加了王争老师的《数据结构与算法之美》的课程,在这个专栏里,我感觉通过几个月的练习,觉得自己的数据结构与算法,有了很大的进步,老师还特别分享了自己动手实现一个搜索引擎的原理以及关键的技术点,通过这个的学习,我了解了一个简单的搜索引擎的原理与实现,我也就将自己动手实现搜索引擎的技术...

bug_null的博客 1404

一致性哈希算法

一致性哈希算法 一. 传统的哈希算法的局限性 要了解一致性哈希算法,就得先了解传统的哈希算法在分布式场景下的局限性。哈希算法就是按键值对的存储,给定一个键,可以做到O(1)的时间复杂度内的数据查找。例如根据学生的学号查找学生的相关的信息。一种简单的存储形式就是以哈希表的形式来存储<code,studentinfo>。 假如某个学校的学生太多了。一台机器上的哈希表已经存储不了。需要使用多台机器来进行存储学生信息,那这时该怎么办呢?这个最简单的办法就是使用哈希取模来确定,计算公式如下 节点编号=h

bug_null的博客 926

leetcode32-最长括号匹配-栈实现

给定一个只包含 '('和 ')'的字符串,找出最长的包含有效括号的子串的长度。 示例1: 输入: "(()" 输出: 2 解释: 最长有效括号子串为 "()" 示例 2: 输入: ")()())" 输出: 4 解释: 最长有效括号子串为 "()()" 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/proble...

bug_null的博客 555

单台机器上实现TB级的数据对比之数据合并

在单台机器上实现TB级的数据对比之数据合并 整体思路 在上一章节,我介绍了如何对切分后的数据进行并行排序,这节来到了最后的操作,数据的合并操作。将数据进行对比,找出添加的数据、修改的数据、删除的数据。需要特别处理下重复的数据。在第一章节我已经介绍了这个大致的思路,但未深入细节来讨论如何实现,这章将深入细节将数据合并的逻辑做一个完整的说明。 这个逻辑看着有点复杂吧,那我来详细的解释下吧! 在原始排序文件中读取所有文件的首行数据,并记录到以文件索引为下标的数组中。就叫它srcDataList 在目标排

bug_null的博客 497

单台机器上实现TB级的数据对比之数据排序

在单台机器上实现TB级的数据对比之数据排序 整体流程 在上一个章节已经实现了对大型文件的拆分.今天来继续分享关于数据的排序。 文件排序的逻辑不复杂。将数据读取进内存中然后排序。写入至磁盘中。由于这些文件相互完全不关联。可以并行化处理。那先还是看下整体的一个结构吧。 这个主要涉及到以下内容: 并行排序控制器,它的主要作用是控制向线程池中加入任务的数量。 线程池,用来执行当前的排序任务。 排序任务。将排序中涉及的到操作,封装到一个任务中。包括从文件中读取数据到内存,再将内存中的内容进行排序,最后再将排序后的

bug_null的博客 481

跳表加散列表实现HashMap

自己实现HashMap 在平时的开发中,jdk库中的HashMap是我们在平时的开发中用的非常多的容器之一,我常常在想,自己能否也实现一个呢?以前觉得非常的复杂,但当我学习了跳表,和散列表后,我觉得这个问题我可以尝试了,于是就开始了自己实现MyHashMap之旅。 1. 需求定义 既然要实现一个HashMap,那还是先需求吧。 public class MyHashMap<K, V> { public MyHashMap(int capacity){} public void put(

bug_null的博客 449

数据结构与算法-递归-leetcode练习

递归 编写递归要决,千万不要用人脑去递和归每一层,正确的做法是找到如何分解为子问题,然后几个子问题的关系是什么。然后翻译成代码。 leetcode17 题目描述: 给定一个仅包含数字2-9的字符串,返回所有它能表示的字母组合。 给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。 示例:...

bug_null的博客 428
上一篇: 数据结构与算法-队列
下一篇: 学习笔记——数据结构与算法.递归
null_null_bug
博客等级 码龄7年 72粉丝 71原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值