[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] < ...
随机推荐
- 51nod 1191:消灭兔子 贪心+优先队列
1191 消灭兔子 题目来源: 2013腾讯马拉松赛第三场 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 有N只兔子,每只有一个血量B[i],需要 ...
- 项目版本回退后出现java compiler level does not match the version of the installed java project facet错误的解决
今天项目出问题了,采取了项目版本回退的方法解决了代码不能够下拉和上送的问题以后,出现如下错误,项目是微服务的,更新相关的依赖项目,仍得不到解决,检查mapper.xml文件亦没问题.然后在控制台那块发 ...
- Linux 正则表达式与文本处理器 三剑客
Linux 正则表达式与文本处理器 三剑客 一.正则表达式 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则. 在linu ...
- idea xml文件去掉背景黄色
编写dao中的sql时,xml文件中背景一大片黄色,看着不舒服,如何去掉了? 1. File -> Settings... 2. 去消以下两项勾选 (Inspections -- 如 ...
- POJ 1745:Divisibility 枚举某一状态的DP
Divisibility Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11001 Accepted: 3933 Des ...
- POST和GET方法乱码解决方案
前言 在WEB开发的过程中,中文乱码是最为常见的问题之一.之所以会出现中文乱码的情况,主要原因是:前端使用POST或者GET方法传递的参数一般使用浏览器预先设置的编码方式进行编码,中文浏览器一般是使用 ...
- zabbix监控tcp/nginx/memcache连接数自定义监控shell
#!/bin/bashtcp_status_fun(){ TCP_STAT=$1 #netstat -n | awk '/^tcp/ {++state[$NF]} END {for(key in st ...
- java.lang.SecurityException: Permission denied (missing INTERNET permission?)
ndroid app里试图用HttpUrlConnection获取网络连接,忘记在AndroidManifest清单文件里声明需要用到Internet的权限,运行时报此错误. 解决方法 在Androi ...
- php里parent,::和self的分别
01.php里parent,::和self的分别/*self的特点*/class a{ public static $a1="我是类a"; function ca() { echo ...
- ES系列之Promise async 和 await
概述 promise是异步编程的一种解决方案,比传统的解决方案—回调函数和事件—更合理更强大. 所谓的promise就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作的结果). Pro ...