Cracking the coding interview--Q3.6

从零开始安装 Windows 11 和 Ubuntu 22.04 双系统 本文详解Windows与Ubuntu 22.04双系统安装流程:需提前确认BIOS模式(UEFI/GPT或Legacy/MBR),关闭BitLocker加密、设备加密及快速启动;推荐Rufus/Ventoy制作启动盘。安装时需规划分区方案(建议单独划分EFI系统分区),注意引导器安装位置选择。双系统启动通过BIOS引导顺序管理,安装后需同步Windows与Linux系统时间。教程适配主流UEFI+GPT设备,强调操作前数据备份及Windows更新策略调整,避免兼容性问题。 阅读详情

原文:

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 this program: push | pop | peek | isEmpty.

译文:

写程序将一个栈按升序排序。对这个栈是如何实现的,你不应该做任何特殊的假设。 程序中能用到的栈操作有:push | pop | peek | isEmpty。


使用一个附加的栈来模拟插入排序。将原栈中的数据依次出栈与附加栈中的栈顶元素比较, 如果附加栈为空,则直接将数据压栈。否则, 如果附加栈的栈顶元素大于从原栈中弹出的元素,则将附加栈的栈顶元素压入原栈。 一直这样查找直到附加栈为空或栈顶元素已经不大于该元素, 则将该元素压入附加栈。

package chapter_3_StacksandQueues;

import java.util.Scanner;

/**
 *
 * 写程序将一个栈按升序排序。对这个栈是如何实现的,你不应该做任何特殊的假设。 程序中能用到的栈操作有:push | pop | peek | isEmpty。
 * 
 */
public class Question_3_6 {
	
	/**
	 * 使用插入排序方法排序
	 */
	public static void sort(Stack_3_5 stack) {
		Stack_3_5 stack2 = new Stack_3_5(10);
		while(!stack.isEmpty()) {
			int data = stack.top();
			stack.pop();	
			// 如果当前stack2栈顶元素大于当前需排序元素,元素出栈暂时存储在stack栈顶
			while(!stack2.isEmpty() && stack2.top() > data) {
				stack.push(stack2.top());
				stack2.pop();
			}
			stack2.push(data);
		}
	}
	
	public static void main(String args[]) {
		Scanner scanner = new Scanner(System.in);
		String string = scanner.nextLine();
		String strs[] = string.split(" ");
		Stack_3_5 stack = new Stack_3_5(10);
		for(int i=0; i< strs.length; i++) {
			stack.push(Integer.parseInt(strs[i]));
		}
		sort(stack);
		while(!stack.isEmpty()) {
			int data = stack.top();
			System.out.format("%3d", data);
		}
	}
}


参考自:http://www.hawstein.com/posts/3.6.html

华为OD干了两个月,还是跑了 ​本人工作了快13年,base南京。2024年2月份由于公司业务变动第一次拿到了公司发的大礼包,同时也面临着失业的问题,我对自己的技术还是很有信心的,但是,找了大概有一个多月时间,发现心意的工作的确不太好找,当然收到挺多家offer,就是薪资不太满意,外包也一直不想去,不过说实话外包给的薪资普遍要比自研的单位要高一些,最后为了钱还是进华为(既然选择外包那就选择好一些的吧),毕竟中年老男人工作不能断太久。 阅读详情

相关推荐

Altium Designer学习---如何进行SI仿真

Altium designer 如何进行SI仿真。         1、仿真电路中需要至少一块集成电路;             2、器件的IBIS模型;              3、在规则中必须设定电源网络和地网络;              4、建立SI规则约束;              5、层堆栈必须设置正确,电源平面必须连续;     建立的文件必须是一个工程,并把相应的文件放在工程目...

weixin_34050519的博客 4901

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

GD32F303片内FLASH实战:从地址映射到数据持久化存储

本文详细介绍了GD32F303片内FLASH的实战应用,从地址映射到数据持久化存储的全过程。通过解析FLASH的基础特性、安全操作区域规划、擦除写入读取的三重奏操作,以及工程化进阶技巧,帮助开发者高效利用片内FLASH进行数据存储。文章还提供了调试技巧和常见问题解决方案,特别适合嵌入式开发者在实际项目中应用。

weixin_26742753的博客 214

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

Kubernetes+EdgeX:构建企业级AI边缘推理平台

设备:工业摄像头(RTSP协议,输出1920x1080视频流);AI模型:YOLOv8训练的轴承裂纹检测模型(识别“裂纹”“正常”两种类型);目标:摄像头采集图像→EdgeX传输数据→K8s推理服务检测→结果反馈到报警器(HTTP协议)。Kubernetes+EdgeX的组合,为企业构建AI边缘推理平台提供了标准化、可扩展标准化:K8s的容器编排和EdgeX的设备管理,都是行业标准,避免“重复造轮子”;可扩展:支持从单边缘节点到 thousands of nodes 的规模,满足企业的增长需求;

AI算力网络与通信的博客 1343

基于Simulink的前馈解耦SVPWM整流器仿真

本文提出了一种基于Simulink的前馈解耦SVPWM整流器仿真方案,针对传统双闭环控制存在的耦合严重、电压利用率低等问题进行优化。通过前馈补偿dq轴耦合项(ωLiq和ωLid)结合SVPWM调制算法,实现了电流跟踪误差<3%、电压利用率达0.99、THD<3%的性能指标。文章详细阐述了系统架构设计,包括前馈解耦原理推导、SVPWM算法实现步骤,并提供了完整的Simulink建模方法。仿真结果表明,该方案在动态响应速度(25ms)和直流电压纹波(1.1%)方面显著优于传统SPWM控制。同时给出了

MHD0815的博客 162

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

ISO21434 组织网络安全管理(二)

为了实现网络安全工程,该组织建立并维护网络安全治理和网络安全文化,包括网络安全意识管理、能力管理和持续改进。这涉及到指定针对本文档的目标进行独立审计的特定于组织的规则和过程。 为了支持网络安全工程,该组织实施了网络安全的管理系统,包括管理工具和应用质量管理系统。

qq_42357877的博客 1322

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

串口、COM、UART、TTL、USB、RS-232、RS-485、I2C、SPI、CAN、1-WIRE

一文搞定,赶紧收藏吧

记得诚 3万+

ansys非线性分析中接触类型

不分离(No Separation)‌:法向始终保持接触不分离,切向允许自由滑动,属于线性接触,适合需要法向约束、允许微小切向位移的场景,比如过盈配合的简化模拟。有摩擦(Frictional)‌:法向允许分离,切向根据用户设置的摩擦系数计算摩擦力,是工程中最符合实际机械接触的类型,绝大多数装配体分析都选择该类型。无摩擦(Frictionless)‌:法向允许接触面分离,切向无摩擦、允许自由滑动,适合模拟理想润滑的光滑接触面,图中当前选择的就是该类型。非线性接触(需要多次迭代,属于真正的非线性接触行为)‌。

qq_15963745的博客 71

Cracking the coding interview--Q3.5

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

cfanr 1448

【华为OD机试】真题-小明减肥(Java)

摘要:题目要求从n个可选运动中选取k个,使其卡路里总和等于目标值t,统计可行方案数。输入包含运动数量n、目标t、选择数量k及卡路里数组。采用DFS回溯算法,通过剪枝优化(提前终止无效分支),分别处理选择/不选当前运动的情况。当遍历完所有运动且满足条件时计数。示例输入"4 3 2"和"1 2 3"时,输出2(对应两种选择组合)。Java实现通过递归完成搜索,最终输出符合条件的方案总数。(149字)

u014481728的博客 282

Cracking the coding interview--Q3.3

原文: 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. Impl

执子之手 509

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

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

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

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

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

Cracking the coding interview--Q3.4

题目 原文: In the classic problem of the Towers of Hanoi, you have 3 rods and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order of size fr

cfanr 1367

Cracking the coding interview--Q8.3

题目 原文: Write a method that returns all subsets of a set. 译文: 写一个方法返回一个集合的所有子集合。 解答 对于一个集合,它的总共子集数为2^n(包括空集)。对于S(5)={1,2,3,4,5},则S(5)的子集数可以写为:5与S(4)的子集的组合+S(4)的子集,得递归算法如下: public static Linke

cfanr 1907

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

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 Q1.3

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

weixin_33721427的博客 92

Cracking the coding interview--Q19.3

题目 原文:

cfanr 1195

Cracking the coding interview--Q4.3

题目 原文: Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal height. 译文: 给一个已经排好序(增序)的数组,写一个算法创建一个具有最小高度二叉树。 解答 要使二叉树高度最小,则其左右结点数应相当。那么将数组对半分,以中间数为父结

cfanr 1307

Cracking the coding interview--Q17.3

题目 原文: Compare and contrast the IPv4 and IPv6 protocols. 译文: 对比下IPv4和IPv6协议。 解答 什么是IPv4?     目前的全球因特网所采用的协议族是TCP/IP协议族。IP是TCP/IP协议族中网络层的协议,是TCP/IP协议族的核心协议。目前IP协议的版本号是4(简称为IPv4),发展至今已经使用了30多年。

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

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值