Cracking the coding interview--Q3.1

MATLABGUI读取文件并将路径显示在文本框中 阅读详情

原文:

Describe how you could use a single array to implement three stacks.

译文:

你如何只用一个数组实现三个栈?


如果实现使用一个数组表示一个栈就简单很多,一个指针指向当前栈顶元素位置,初始top=-1,表示栈为空

如果入栈则top++,并元素入栈即可,出栈也只需要top--,top指向栈顶元素。


方法一:

如果用一个数组代表三个栈的操作,假设前天不知道每个栈所需要的空间大小,则可以使用数组等分,每个栈使用

三分之一空间,各个栈都有自己的栈顶指针来操作自己的空间。


package chapter_3_StacksandQueues;

class Stack_1 {
	private int stack[];
	private int size;
	
	// 分别记录三个栈顶指针位置,栈顶指针指向当前栈最高元素
	private int top[] = new int[3];
	private int bottom[] = new int[3];
	
	public Stack_1(int size) {
		stack = new int[size * 3];
		this.size = size;
		top[0] = bottom[0] = -1;
		top[1] = bottom[1] = size * 1 -1;
		top[2] = bottom[2] = size * 2 -1;
	}
	
	public boolean isEmpty(int stackNum) {
		return top[stackNum] == (stackNum * size -1);
	}
	
	public boolean isFull(int stackNum) {
		return top[stackNum] == ((stackNum+1) *size - 1);
	}
	
	public void push(int stackNum, int value) {
		if(isFull(stackNum)) {
			System.out.println("Stack num :" + stackNum + " is full!");
			return;
		}
		stack[++ top[stackNum]] = value;
	}
	
	public int top(int stackNum) {
		if(isEmpty(stackNum)) {
			System.out.println("Stack num :" + stackNum + " is empty!");
			return -1;
		}
		return stack[top[stackNum]];
	}
	
	public void pop(int stackNum) {
		if(isEmpty(stackNum)) {
			System.out.println("Stack num :" + stackNum + " is empty!");
			return;
		}
		
		top[stackNum]--;
	}
	// 清空并输出
	public String getSatckandClear(int stackNum) {
		StringBuilder result = new StringBuilder();
		// 当前不空
		while(!isEmpty(stackNum)) {
			result.append("  " + top(stackNum));
			pop(stackNum);
		}
		return result.toString();
	}
}

/**
 * 
 * 你如何只用一个数组实现三个栈?
 *
 */
public class Question_3_1 {
	public static void main(String args[]) {
		Stack_1 stack = new Stack_1(5);
		int stackNum = 0;
		System.out.println("stackNum " + stackNum + " is Empty :" + stack.isEmpty(stackNum));

		for(int i=0; i<3; i++) {
			for(int j=i*10, k=1; k<=10; k++,j++) {
				stack.push(i, j);
			}
		}
		
		for(int i=0; i<3; i++) {
			String result = stack.getSatckandClear(i);
			System.out.format("stack %d : %s\n", i, result);
		}
	}
}





方法二:

如果考虑三个栈直接使用同一个数组,这个可以灵活各个栈空间大小,不约束于三分之一。这样连续使用则每个栈都有自己的栈顶指针

但是同时由于站内数据混在一起,则需要每个栈当前栈顶元素,如果出栈,则需要找到其前一个元素,此时需要记录每个元素的前继元

素在数组中的下标,所以数组元素就不能单纯使用基本类型了,可以使用c++的结构体,java中则使用类

class Node {
	public int data;
	public int pre;
}

此时对于操作流程中则需要注意每个栈的出栈和入栈,如果只是单纯各管自己的,每次出栈去数组最高位,则考虑如果栈1一直出栈,最终为空

而此时栈2入栈则单纯只考虑数组最高位则浪费了栈1出栈的位置空间,所以每次都需要有一个全部栈空间指针stack_top,随时指针当前最低位空闲位置


每次有元素需要出栈的时候,元素出栈,如果元素位置比全部stack_top要低位,则更新stack_top为当前出栈位置,如果要高则无需变动,这样实时保证

下次入栈时候能利用最低位空闲位置。

每次有元素需要入栈,则stack_top记录最低位入栈,但是stack_top的下一个高位比一定是空闲位置,所以需要往高位扫描直到找到下一个空闲位置,此时

也是整个数组最低位空闲位置。



package chapter_3_StacksandQueues;

class Node {
	public int data;
	public int pre;
}

class Stack_1_2 {
	private Node[] stack;
	// 整个栈的大小
	private int size;
	private int top[] = new int[3];
	// 记录当前全局栈顶可存放位置
	private int stack_top;
	
	public Stack_1_2(int size) {
		stack = new Node[size];
		this.size = size;
		top[0] = top[1] = top[2] = -1;
		stack_top = 0;
		
		for(int i=0; i<size; i++) {
			stack[i] = new Node();
			stack[i].pre = -2;
		}
	}
	
	public boolean isEmpty(int stackNum) {
		return top[stackNum] == -1;
	}
	
	public boolean isFull() {
		return stack_top == (size -1);
	}
	// 入栈
	public void push(int stackNum, int data) {
		if(isFull()) {
			System.out.println("Stack :" + " is full!");
			return;
		}
		int pre = top[stackNum];
		stack[stack_top].pre = pre;
		stack[stack_top].data = data;
		top[stackNum] = stack_top;
		// 寻找下一个空闲栈位置
		for(int i=stack_top;i<size; i++) {
			if(stack[i].pre == -2) {
				stack_top = i;
				break;
			}
		}
		// 如果没有空闲栈位置,则指向栈顶下一个位置
	}
	
	public int top(int stackNum) {
		if(isEmpty(stackNum)) {
			System.out.println("Stack num :" + stackNum + " is empty!");
			return -1;
		}
		return stack[top[stackNum]].data;
	}
	// 出栈
	public void pop(int stackNum) {
		if(isEmpty(stackNum)) {
			System.out.println("Stack num :" + stackNum + " is empty!");
			return;
		}
		int now = top[stackNum];
		Node node = stack[now];
		if(now < stack_top) {
			stack_top = now;
		}
		
		int pre = node.pre;
		top[stackNum] = pre;
		node.pre = -2; // 标记为空闲位置
	}
	
	public String getSatckData(int stackNum) {
		StringBuilder result = new StringBuilder();

		int cur = top[stackNum];
		while(cur != -1) {
			result.append("  " + stack[cur].data);
			cur = stack[cur].pre;
		}
		return result.toString();
	}
}
/**
 * 
 * 你如何只用一个数组实现三个栈?
 *
 */
public class Question_3_1_2 {
	public static void main(String args[]) {
		Stack_1_2 stack = new Stack_1_2(20);
		int stackNum = 0;
		System.out.println("stackNum " + stackNum + " is Empty :" + stack.isEmpty(stackNum));
		
		int stackStart[] = {0, 100, 1000};
		
		// 3 * 3
		for(int i=0; i<3; i++) {
			for(int k=1; k<=3; k++) {
				stack.push(i, stackStart[i] ++);
			}
		}
		// 3 * 3
		for(int i=2; i>=0; i--) {
			for(int k=1; k<=3; k++) {
				stack.push(i, stackStart[i] ++);
			}
		}
		// 3 * 1
		for(int i=0; i<3; i++) {
			for(int k=1; k<=1; k++) {
				stack.push(i, stackStart[i] ++);
			}
		}
		// 输出各个栈的内容
		for(int i=0; i<3; i++) {
			String result = stack.getSatckData(i);
			System.out.format("stack %d : %s\n", i, result);
		}
		
		// 栈1出栈5个元素
		for(int i=0; i<5;i++) {
			System.out.format("stack 0 出栈 top pop : %d\n", i, stack.top(0));
			stack.pop(0);
		}
		
		// 栈2、3入栈元素
		for(int i=1; i<3; i++) {
			for(int k=1; k<=3; k++) {
				stack.push(i, stackStart[i] ++);
			}
		}
		
		// 输出各个栈的内容
		for(int i=0; i<3; i++) {
			String result = stack.getSatckData(i);
			System.out.format("stack %d : %s\n", i, result);
		}
	}
}


参考自:http://hawstein.com/posts/3.1.html


时间序列聚类在股票市场分析中的实践与优化 时间序列聚类是一种面向动态行为建模的无监督学习方法,其核心在于通过语义感知的距离度量(如DTW)捕捉价格形态的相位弹性与结构相似性,而非依赖静态数值或标签驱动。相比K-Means等传统算法,它能有效缓解金融时序中普遍存在的噪声敏感、对齐失效与形态失真问题,从而提升聚类结果的市场可解释性与策略鲁棒性。该技术已广泛应用于市场状态识别、行业行为分群、异常交易监测等场景,并成为构建可验证量化分析框架的关键预处理层。本文聚焦A股真实行情数据,详解从原始OHLCV到可聚类时间序列的完整链路,涵盖DTW距离计算、层次聚类 阅读详情

相关推荐

第十九届全国大学生智能车竞赛越野信标 硬件篇

全国大学生智能汽车竞赛,越野信标组

2301_77568650的博客 4115

Cracking the Coding Interview Q3.1

Describe how you could use a single array to implement three stacks. 思路1:fixed division package Question3_1; import java.util.EmptyStackException; public class Question { stat...

weixin_30484247的博客 113

如何在软件复位后保留指定RAM区域的数据

本文主要介绍S32K144的RAM retention功能

北极熊的脖子(16605192620@163.com) 1万+

Cracking the coding interview--Q1.3

题目: 原文: Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional variables are fine. An extra copy of the ar

cfanr 1642

转义序列字符(\0,\n,\r,\t,\v,\a,\f,\b,\\,\‘,\“,\?)详解,转义字符对应的全称,输出结果和对应的ASCII码值详解;

转义序列字符(\0,\n,\r,\t,\v,\a,\f,\b,\\,\',\",\?)详解,转义字符对应的全称,输出结果和对应的ASCII码值详解;

南城花开过的博客 3万+

Vivado SDK中LWIP库版本冲突与RTL8211FDI兼容性实战解析

本文深入解析Vivado 2019.1 SDK中LWIP 1.0库与RTL8211FDI千兆PHY芯片的版本冲突问题,并提供实战解决方案。通过手动移植高版本LWIP库、整合第三方驱动及优化BSP配置,有效解决了以太网通信兼容性问题,确保千兆性能稳定运行。

ios99的博客 271

Cracking the Coding Interview Q1.3

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_33721427的博客 92

Springboot——接入企业微信聊天机器人

最近企业微信的聊天机器人很火也很好玩,本期想自己接入一个玩玩试试水,同时也接入AI大模型进行数据的交互。

专注写bug 1320

Cracking the Coding Interview Chap1 Q1.3

Q1.3 Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer.  NOTE: One or two additional variables are fine. An extra copy of the array

biubiu的专栏 651

康耐视visionpro-CogBlobTool工具详细说明

康耐视visionpro-CogBlobTool工具详细说明

视觉人机器视觉的博客 1724

AI Agent在医疗诊断中的智能应用研究

本文探讨了AIAgent在医疗诊断中的智能应用,分析了其技术原理、实际案例及发展挑战。AIAgent通过机器学习算法处理医疗数据,提升诊断准确性和效率,市场规模预计2025年达200亿美元。核心技术包括数据预处理、深度学习模型和隐私保护方法。应用案例涵盖肺癌筛查、阿尔茨海默病预测等,但面临数据偏差、伦理责任等挑战。未来趋势包括多模态学习、个性化医疗和量子计算融合。随着技术优化和伦理完善,AIAgent将成为医疗体系的重要组成部分。

郑伟强dev的专栏 96

【Cracking the coding interview】Q1.3(移除重复字符)

Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional variables are fine. An extra copy of the array is not

pipi666661的专栏 588

【毕设级教程】从0到1实现基于YOLOv8的xView卫星图像目标检测系统(含UI界面,助力遥感智能毕设)

本文基于YOLOv8模型设计了一个面向xView卫星图像的目标检测系统。首先对xView数据集进行了格式转换处理,将原始多边形标注转换为YOLO格式的边界框标注。随后通过配置YOLOv8模型参数完成训练,实现了对卫星图像中建筑、车辆等多类目标的准确识别。为提高系统交互性,采用PyQt开发了可视化界面,支持图像选择、实时检测和结果展示功能。整个系统兼顾学术价值和实用价值,为遥感领域的自动化目标检测提供了可行方案。

m0_64661770的博客 130

Cracking the coding interview--Q4

Chapter 4 | Trees and Graphs 4.1 Implement a function to check if a tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that no two leaf nodes differ

763

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

cracking-the-coding-interview的核心秘密:如何用单元测试“定义”面试题?C++ Catch+Python unittest的TDD完整教程

cracking-the-coding-interview 是一个为《Cracking the Coding Interview》(CTCI)第 6 版提供 **C++ 与 Python 双语言解法** 的开源项目,其最大特色是:**每一道面试题的解法都配有完整的自动化单元测试**(C++ 端用 Catch 框架,Python 端用 unittest 框架),自动化测试覆盖率超过 90%。更妙的是

gitblog_00501的博客 317

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Cracking the Coding Interview Chap1 Q1.1

Q1.1 Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures? Sol.1 : check every char of the string to every other char of

biubiu的专栏 965

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Cracking the coding interview--Q9

Chapter 9 | Sorting and Searching 9.1 You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order. 插入排序,需要调整数

1020

Cracking the coding interview--Q1.1

作者:ustcweijie 参考:http://hawstein.com/posts/1.1.html 1. 题目 原文:Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?

蔚蓝天空 546

渣基础:比照Hawstein学Cracking the coding interview(1)

《C++ Primer 第五版》书实在是太长,太厚了。总是看了十几页就看累了,坚持不了多久,想了想还是别勉强自己,jued

低端玩家的记录地 949
上一篇: Cracking the coding interview--Q2.5
下一篇: Cracking the coding interview--Q3.2
java_wliang
博客等级 码龄15年 19粉丝 82原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值