[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] < ...
随机推荐
- POJ 1944:Fiber Communications
Fiber Communications Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4236 Accepted: 1 ...
- 63.Python中contains和icontains
1. contains: 进行大小写敏感的判断,某个字符串是否包含在指定的字段中,这个判断条件使用大小写敏感进行判断,因此在被翻译成"SQL"语句的时候,会使用"like ...
- 使用DOM4J生成XML文档
package xml; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; imp ...
- git push 现有代码到一个新的分支
git push origin HEAD:task/xxx-test-local git push的一般形式为 git push <远程主机名> <本地分支名> <远程 ...
- 转载电子发烧友网---STM32的IO口灌入电流和输出驱动电流
刚开始学习一款单片机的时候一般都是从操作IO口开始的,所以我也一样,先是弄个流水灯. 刚开始我对STM32的认识不够,以为是跟51单片机类似,可以直接操作端口,可是LED灯却没反应,于是乎,仔细查看资 ...
- 洛谷 P2196 挖地雷
题目传送门 解题思路: 记忆化搜索,题目比较坑的地方在于,这是个有向图,给的边是单向边!!!!!!!! AC代码: #include<iostream> #include<cstdi ...
- like not like 优化
instr(title,’手册’)>0 相当于like instr(title,’手册’)=0 相当于not like 对于LIKE语句,我们可以使用instr函数来进行SQL调优
- sendmail 的安装、配置与发送邮件的具体实现
Ubuntu 中sendmail 的安装.配置与发送邮件的具体实现 centos安装sendmail与使用详解 CentOS下搭建Sendmail邮件服务器 使用外部SMTP发送邮件 使用mailx ...
- 12 react 基础 的 css 过渡动画 及 动画效果 及 使用 react-transition-group 实现动画
一. 过渡动画 # index.js import React from 'react';import ReactDOM from 'react-dom';import App from './app ...
- ftp限制
/etc/hosts.deny /etc/vsftpd/user_list 从2.3.5之后,vsftpd增强了安全检查,如果用户被限定在了其主目录下,则该用户的主目录不能再具有写权限了!如果检查发现 ...