Cracking the coding interview--Q3.3

Cracking the Coding Interview, 4 Edition 立即下载

原文:

Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in real life, we would likely start a new stack when the previous stack exceeds some threshold. Implement a data structure SetOfStacks that mimics this. SetOfStacks should be composed of several stacks, and should create a new stack once the previous one exceeds capacity. SetOfStacks.push() and SetOfStacks.pop() should behave identically to a single stack (that is, pop() should return the same values as it would if there were just a single stack).

FOLLOW UP

Implement a function popAt(int index) which performs a pop operation on a specific sub-stack.

译文:

栈就像叠盘子,当盘子叠得太高时,就会倾斜倒下。因此,在真实的世界中,当一叠盘子 (栈)超过了一定的高度时,我们就会另起一堆,再从头叠起。实现数据结构SetOfStacks 来模拟这种情况。SetOfStacks由几个栈组成,当前一栈超出容量时,需要创建一个新的栈 来存放数据。SetOfStacks.push()和SetOfStacks.pop()的行为应当和只有一个栈时 表现的一样。

进一步地,

实现函数popAt(int index)在指定的子栈上进行pop操作。


首先当不考虑进一步的操作,popAt(int index)时。 单纯考虑入栈出栈操作。每个子栈用相等长度数组存储栈元素,当一个栈数组存满元素,则记录下一个栈

可以使用变量curStack 记录当前所在栈编号。

如果元素入栈,首先判断整个栈是否满栈,当整个栈满栈,则提示栈满。

	/**
	 * 是否栈满
	 * 栈满条件为子栈为最大且同时也满栈
	 * @return
	 */
	public boolean isFull() {
		return curStack == (childStack-1) && stack[curStack].isFull();
	}

否则入栈,入栈则需要先判断当前子栈是否已经满栈,如果当前子栈已经满栈,则curStack++。进入下一个子栈,如果当前子栈不满栈,则元素入栈stack[curStack].push(data);

如果元素出栈,则首先判断是否整个栈栈空,栈空则提示栈空。

	/**
	 * 是否栈空
	 * 栈空条件为子栈是否最小且同时为空
	 * @return
	 */
	public boolean isEmpty() {
		return curStack == 0 && stack[curStack].isEmpty();
	}


如果栈不空,则出栈,如果子栈为空,则curStack--进入前一个子栈获取元素。

package chapter_3_StacksandQueues;

/**
 * @author LiangGe
 * 栈的基本操作
 */
class Stack_3 {
	private int size;
	private int stack[];
	private int top = -1;
	
	public Stack_3(int size) {
		this.size = size;
		stack = new int[size];
		top = -1;
	}
	
	public boolean isFull() {
		return top == (size-1);
	}
	
	public boolean isEmpty() {
		return top == -1;
	}
	
	public void push(int data) {
		if(isFull()) {
			//System.out.println("Stack is full!");
			return;
		}
		stack[++top] = data;
	}
	
	public void pop() {
		if(isEmpty()) {
			//System.out.println("Stack is empty!");
			return;
		}
		top --;
	}
	
	public int top() {
		if(isEmpty()) {
			//System.out.println("Stack is empty!");
			return -1;
		}
		return stack[top--];
	}
	
}

/**
 * @author LiangGe
 * 大栈操作
 */
class SetOfStacks{
	private int childStack;
	private Stack_3 stack[];
	private int curStack;
	
	public SetOfStacks(int size) {
		childStack = size;
		stack = new Stack_3[childStack];
		for(int i=0; i<childStack; i++) {
			stack[i] = new Stack_3(childStack);
		}
		curStack = 0;
	}
	
	/**
	 * 是否栈满
	 * 栈满条件为子栈为最大且同时也满栈
	 * @return
	 */
	public boolean isFull() {
		return curStack == (childStack-1) && stack[curStack].isFull();
	}
	
	/**
	 * 是否栈空
	 * 栈空条件为子栈是否最小且同时为空
	 * @return
	 */
	public boolean isEmpty() {
		return curStack == 0 && stack[curStack].isEmpty();
	}
	
	public void push(int data) {
		if(isFull()) {
			System.out.println("Stack is full!");
			return;
		}
		
		if(stack[curStack].isFull()) {
			curStack ++;
		}
		
		stack[curStack].push(data);
	}
	
	public void pop() {
		if(isEmpty()) {
			System.out.println("Stack is empty!");
			return;
		}
		if(stack[curStack].isEmpty()) {
			curStack --;
		}
		stack[curStack].pop();
	}
	
	public int top() {
		if(isEmpty()) {
			System.out.println("Stack is empty!");
			return -1;
		}
		if(stack[curStack].isEmpty()) {
			curStack --;
		}
		return stack[curStack].top();
	}
}

/**
 * @author LiangGe
 *
 * 栈就像叠盘子,当盘子叠得太高时,就会倾斜倒下。因此,在真实的世界中,当一叠盘子 (栈)超过了一定的高度时,我们就会另起一堆,再从头叠起。
 * 实现数据结构SetOfStacks 来模拟这种情况。SetOfStacks由几个栈组成,当前一栈超出容量时,需要创建一个新的栈 来存放数据。
 * SetOfStacks.push()和SetOfStacks.pop()的行为应当和只有一个栈时 表现的一样。
 *
 */
public class Question_3_3 {
	public static void main(String args[]) {
		SetOfStacks setOfSatacks = new SetOfStacks(3);
		
		for(int i=0; i<10; i++) {
			setOfSatacks.push(i);
		}
		
		for(int i=0; i< 11 ;i++) {
			int top = setOfSatacks.top();
			if(top != -1) {
				System.out.println("Top stack :" + top);
			}
		}
	}
}


如果考虑进一步的操作popAt(int index)时,

	public int top() {
		if(isEmpty()) {
			System.out.println("Stack is empty!");
			return -1;
		}
		while(stack[curStack].isEmpty()) {
			curStack --;
		}
		
		return stack[curStack].top();
	}
	
	public void pop() {
		if(isEmpty()) {
			System.out.println("Stack is empty!");
			return;
		}
		while(stack[curStack].isEmpty()) {
			curStack --;
		}
		stack[curStack].pop();
	}
	
	public void popAt(int index) {
		stack[index].pop();
	}


则如果考虑中间栈元素需要出栈,则会使得中间元素栈内有空闲位置,此时如果考虑牺牲空间问题,则空闲位置继续空闲。

则此时在上述问题中只需要考虑出栈操作,因为入栈操作没有变化,只是出栈操作需要判断。

如果元素出栈,则首先判断大栈是否为空,如果不为空则出栈,此时出栈首先判断当前子栈是否为空,为空则需要while循环往前查找

下一个非空子栈。

		while(stack[curStack].isEmpty()) {
			curStack --;
		}

所需只需要在出栈和获取栈顶元素代码出进行修改即可

	public int top() {
		if(isEmpty()) {
			System.out.println("Stack is empty!");
			return -1;
		}
		while(stack[curStack].isEmpty()) {
			curStack --;
		}
		
		return stack[curStack].top();
	}
	
	public void pop() {
		if(isEmpty()) {
			System.out.println("Stack is empty!");
			return;
		}
		while(stack[curStack].isEmpty()) {
			curStack --;
		}
		stack[curStack].pop();
	}
	
	public void popAt(int index) {
		stack[index].pop();
	}


Cracking the coding interview 4th 150 programming interview questions and solutions Plus: Five proven approaches to solving tough algorithm questions Ten mistakes candidates make -- and how to avoid them Steps to prepare for behavioral and technical questions Interviewer war stories: a view from the interviewer’s side 立即下载

相关推荐

解决eNSP安装WinPcap版本冲突的完整指南

网络数据包捕获工具WinPcap是网络仿真环境的基础组件,其版本兼容性直接影响eNSP等网络模拟器的正常运行。当系统已安装新版WinPcap时,eNSP内置的旧版安装程序会因版本校验机制触发报错。通过彻底卸载现有驱动、安装特定版本WinPcap 4.1.2,并验证注册表和服务状态,可解决版本冲突问题。对于需要同时使用新版驱动的场景,可采用虚拟机隔离或Npcap兼容模式。该方案适用于Windows 10/11系统环境,涉及驱动签名、虚拟网卡等典型网络工程问题。

weixin_32612263的博客 336

谷歌面试官经典作品(CTCI)目录

1.1 判断一个字符串中的字符是否唯一1.2 字符串翻转1.3 去除字符串中重复字符1.8 利用已知函数判断字符串是否为另一字符串的子串2.1 从链表中移除重复结点2.2 实现一个算法从一个单链表中返回倒数第n个元素2.3 给定链表中间某结点指针,删除链表中该结点2.4 求由两个链表结点组成的数之和2.5 给定一个循环链表,实现一个算法返回这个环的开始结点3.1 如何只用一个数组实现三个栈3.2

专注Android开发 1372

gtsam官方文档: The new IMU Factor学习笔记

gtsam官方文档:The new IMU Factor 本文为gtsam imu预积分文档学习笔记,为gtsam基于切向量实现的imu预积分的理论基础。 导航状态 定义导航状态为 Xbn={Rbn,Pbn,Vbn}X^n_b=\{R^n_b,P^n_b,V^n_b\}Xbn​={Rbn​,Pbn​,Vbn​} 上角标n:导航(navigation)坐标系 下角标b:载体(body)坐标系 下文均省略上下标 矢量场与微分方程 对于导航问题我们用如下微分方程描述 X˙(t)=F(t,X)\dot X(t)

南柯一梦的博客 1607

【亲测免费】 探秘《Cracking the Coding Interview》——编程面试的黄金指南

探秘《Cracking the Coding Interview》——编程面试的黄金指南 该项目是一个开源版本的,由GitHub用户black-shadows维护。这本书是许多软件工程师准备技术面试的必备资源,它涵盖了数据结构、算法和问题解决策略等多个方面。在这里,我们将从技术角度深入剖析其价值,并分享如何利用它提升你的编程面试技巧。 技术分析 代码实现 - 项目中包含了大量与书本内容相对应的...

gitblog_00091的博客 821

攻防世界 web高手进阶区 9分题 Web_python_flask_sql_injection

继续ctf的旅程,开始攻防世界web高手进阶区的9分题,本文是Web_python_flask_sql_injection的writeup

闵行小鱼塘 1472

【STM32/GD32】Keil uVision生成bin文件:从编译选项到Bootloader升级实战

本文详细讲解了如何在Keil uVision中配置STM32/GD32工程,使其在编译后自动生成Bootloader升级所需的bin文件。通过解析fromelf工具的使用、构建后命令的配置以及内存布局规划,深入阐述了bin文件在固件升级中的核心作用,并提供了串口Bootloader的实战框架与常见问题解决方案。

weixin_29190169的博客 391

Cracking the coding interview--问题与解答

March 14, 2013 作者:Hawstein 出处:http://hawstein.com/posts/ctci-solutions-contents.html 声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 ,转载请注明作者及出处。 前言 《Cracking the codin

leiyifight的专栏 3457

稳定双共轭梯度法BiCGSTAB流程

稳定双共轭梯度法BiCGSTAB流程

abner361的博客 1154

经典算法题目:Cracking the coding interview 问题与解答

Cracking the coding interview--问题与解答 March 14, 2013 作者:Hawstein 出处:http://hawstein.com/posts/ctci-solutions-contents.html 声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 ,转载请注

我的专栏 7584

基于YOLOv8与UI界面的短视频内容审核系统实现与深度学习应用

YOLO(You Only Look Once)是一种用于实时物体检测的深度学习模型,它能在单次前向传播中同时完成物体检测和定位。YOLOv8是YOLO系列中的最新版本,相较于之前的版本,YOLOv8具有更高的检测精度和更快的推理速度,特别适合需要实时检测的应用场景。YOLOv8通过卷积神经网络(CNN)来提取图像中的特征,并通过多个卷积层来预测每个目标的位置和类别。该模型的优势在于其端到端的结构,使得在视频监控、自动驾驶、安防监控等实时应用中,能够实现快速且高效的目标检测。

YOLO 314

拼多多Android面试题及参考答案

Android 系统采用分层架构设计,从底层到上层依次为Linux 内核层硬件抽象层(HAL)系统运行时库层应用框架层应用层,共五层。位置:Linux 内核层与系统运行时库层之间,是“硬件与系统的隔离层”。核心作用:屏蔽硬件差异,为上层提供统一的硬件访问接口,解决 Android 开源与硬件厂商闭源的矛盾,具体功能如下:硬件能力抽象:将硬件的具体实现(如不同厂商的摄像头芯片、音频芯片)封装为标准化接口(如Camera HALAudio HAL。

大模型大数据攻城狮的专栏 1279

探秘编程面试利器:《Cracking the Coding Interview》C++解题方案

探秘编程面试利器:《Cracking the Coding Interview》C++解题方案 1、项目介绍 在编程求职场上,《Cracking the Coding Interview》(简称CTCI)是一本广受推崇的面试准备指南。这本书涵盖了算法、数据结构以及软件工程等多个方面的知识。而这个开源项目,正是对书中第四版问题的详细解答集,全部采用高效的C++语言实现。 项目的作者不仅提供了代码实现...

gitblog_00019的博客 502

educoder实训——计算几何形状的表面积与体积

educoder python实训

qq_41234663的博客 1230

Cracking the coding interview--Q3.6

原文: Write a program to sort a stack in ascending order. You should not make any assumptions about how the stack is implemented. The following are the only functions that should be used to write thi

执子之手 560

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

Cracking the coding interview--Q9.3

题目 原文: Given a sorted array of n integers that has been rotated an unknown number of times, give an O(log n) algorithm that finds an element in the array. You may assume that the array was originall

cfanr 1620

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

Cracking the coding interview--Q3

Chapter 3 | Stacks and Queues 3.1 Describe how you could use a single array to implement three stacks. 分段呗- - 3.2 How would you design a stack which, in addition to push and pop, also has a f

737

Cracking the coding interview--Q2.3

题目 原文: Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node. EXAMPLE Input: the node ‘c’ from the linked list a->b->c->d->e Result: nothin

cfanr 1388

Cracking the coding interview--Q3.1

题目 原文: Describe how you could use a single array to implement three stacks. 译文: 描述怎样用一个单独的数组来实现三个栈。 解答 解法一: 将数组划分成3等份,每一份独立的用来实现堆栈。 *第一个堆栈:从 0     至 n/3 *第二个堆栈:从 n/3  至 2n/3 *第三个堆栈:从2n/3 至

cfanr 1451

Cracking the coding interview--Q3.5

题目 原文: Implement a MyQueue class which implements a queue using two stacks. 译文: 用两个栈实现一个队列MyQueue。 解答 方法

cfanr 1448

Cracking the coding interview--Q20.3

题目 原文: Write a method to randomly generate a set of m integers from an array of size n. Each element must have equal probability of being chosen. 译文:

cfanr 1296

Cracking the coding interview--Q12.3

题目 原文: Given an input file with four billion integers, provide an algorithm to generate an integer which is not contained in the file. Assume you have 1 GB of memory. FOLLOW UP What if you have on

cfanr 1486

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
上一篇: Cracking the coding interview--Q3.2
下一篇: Cracking the coding interview--Q3.4
java_wliang
博客等级 码龄15年 19粉丝 82原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值