[Algo] 280. Sort With 2 Stacks
Given an array that is initially stored in one stack, sort it with one additional stacks (total 2 stacks).
After sorting the original stack should contain the sorted integers and from top to bottom the integers are sorted in ascending order.
Assumptions:
- The given stack is not null.
- There can be duplicated numbers in the give stack.
Requirements:
- No additional memory, time complexity = O(n ^ 2).
public class Solution {
public void sort(LinkedList<Integer> s1) {
LinkedList<Integer> s2 = new LinkedList<Integer>();
// Write your solution here.
while (!s1.isEmpty()) {
s2.offerFirst(s1.pollFirst());
}
while (!s2.isEmpty()) {
Integer cur = s2.pollFirst();
while (!s1.isEmpty() && cur > s1.peekFirst()) {
s2.offerFirst(s1.pollFirst());
}
s1.offerFirst(cur);
}
}
}
[Algo] 280. Sort With 2 Stacks的更多相关文章
- Chp11: Sorting and Searching
Common Sorting Algo: Bubble Sort: Runime: O(n2) average and worst case. Memory: O(1). void BubbleSor ...
- [topcoder]KingdomReorganization
http://community.topcoder.com/stat?c=problem_statement&pm=11282&rd=14724 这道题是最小生成树,但怎么转化是关键. ...
- leetcode 280.Wiggle Sort 、324. Wiggle Sort II
Wiggle Sort: 注意:解法一是每次i增加2,题目不是保证3个3个的情况,而是整个数组都要满足要求. 解法一错误版本: 如果nums的长度是4,这种情况下nums[i+1]会越界.但是如果你用 ...
- Leetcode 280. Wiggle Sort
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- LeetCode 280. Wiggle Sort (摆动排序)$
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- [LeetCode] 280. Wiggle Sort 摆动排序
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- 【LeetCode】280. Wiggle Sort 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序后交换相邻元素 日期 题目地址:https://l ...
- 280. Wiggle Sort
题目: Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] ...
- LeetCode 280. Wiggle Sort C#
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
随机推荐
- ios系统web(微信公众号)开发遇到的问题及解决方案
1.1. 页面滚动不流畅(2017-09-25) 现象: 网页竖向滚动或横向滚动不流畅. 解决方案: 为滚动元素添加css样式: -webkit-overflow-scrolling: touch; ...
- Vulkan 开发学习资料汇总
开发资料汇总 1.API Reference 2.Vulkan Spec 有详细说明的pdf 文章 1.知乎Vulkan-高性能渲染 2.Life of a triangle - NVIDIA's l ...
- Python属性和内建属性
属性property 1. 私有属性添加getter和setter方法 class Money(object): def __init__(self): self.__money = 0 def ge ...
- java web实现在线编辑word,并将word导出(二)
前一篇文章介绍了后台将前台html转为word文档的一种方式,但却有一个问题是没法讲图片放置在生成的word报告中.我在网上找了很多方法,甚至将图片转换成base64编码的方式也不成功.效果如下: 由 ...
- 使用maven构建项目的注意事项
一.如果修改了pom.xml文件,就有点类似修改了项目的结构,在再次运行项目前,应该Mvaen >>Update project一下. 二.对于依赖一个系列的的包,如spring,我们应该 ...
- 吴裕雄--天生自然TensorFlow2教程:填充与复制
import tensorflow as tf a = tf.reshape(tf.range(9), [3, 3]) a tf.pad(a, [[0, 0], [0, 0]]) tf.pad(a, ...
- Day 2:线程与进程系列问题(二)
补充: 线程的创建方式二: 1.自定义一个实现Runnable接口的类 2.实现Runnable接口中的run方法把自定义线程的任务写在run方法中 3.创建实现Runnable接口的对象 4.创建T ...
- SpringMVC_执行原理
什么是SpringMVC 概述 Spring MVC是Spring Framework的一部分,是基于Java实现MVC的轻量级Web框架. 查看官方文档:https://docs.spring.io ...
- CCCC 红色警报
题意: 战争中保持各个城市间的连通性非常重要.本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报.注意:若该国本来就不完全连通,是分裂的k个区域,而失去一个 ...
- HTTP协议(二):作用
前言 上一节我们简单介绍了一下TCP/IP协议族的基本情况,知道了四大层的职责,也了解到我们这一族的家族成员以及他们的能力. 无良作者把我这个主角变成了配角,让我很不爽,好在我打了作者一顿,没错,这次 ...