# 《剑指offer第二版》五

## 1. 包含min函数的栈

（1）题目描述

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.min();   --> 返回 -3.
minStack.pop();
minStack.top();      --> 返回 0.
minStack.min();   --> 返回 -2.


（2）题目分析

（3）代码

package swordOffer.day5;

import java.util.Stack;

/**
* @author chengzhengda
* @version 1.0
* @date 2020-03-25 18:43
* @desc
*/
public class t21 {
Stack<Integer> stack1;
Stack<Integer> stack2;

public t21() {
stack1 = new Stack();
stack2 = new Stack();
}

public void push(int x) {
if (stack2.empty() || (!stack2.empty() && x <= stack2.peek())) {
stack2.push(x);
}
stack1.push(x);
}

public void pop() {
if (stack1.empty() || stack2.empty()) {
return;
}
int x = stack1.pop();
if (x == stack2.peek()) {
stack2.pop();
}
}

public int top() {
return stack1.peek();
}

public int min() {
return stack2.peek();
}

public static void main(String[] args) {
t21 tt = new t21();
tt.push(3);
tt.push(2);
tt.push(1);
System.out.println(tt.min());
}
}


## 2. 栈的压入、弹出序列

（1）题目描述

push(1), push(2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1


（2）题目分析

（3）代码

package swordOffer.day5;

import java.util.Stack;

/**
* @author chengzhengda
* @version 1.0
* @date 2020-03-25 19:20
* @desc
*/
public class t23 {
public static boolean validateStackSequences(int[] pushed, int[] popped) {
Stack<Integer> stack = new Stack<>();
int index = 0;
for (int i = 0; i < pushed.length; i++) {
stack.push(pushed[i]);
while (!stack.empty() && index < popped.length && stack.peek() == popped[index]) {
stack.pop();
index++;
}
}
return stack.empty();
}

public static void main(String[] args) {
int push[] = {1, 2, 3, 4, 5};
int pop[] = {4, 5, 3, 2, 1};
System.out.println(validateStackSequences(push, pop));
}
}


## 3. 数组中出现次数超过一半的数字

（1）题目描述

（2）题目分析

（3）代码

package swordOffer.day5;

/**
* @author chengzhengda
* @version 1.0
* @date 2020-03-25 19:03
* @desc
*/
public class t22 {

public static int majorityElement(int[] nums) {
int x = 0, vote = 0;
for (int num : nums) {
if (vote == 0) {
x = num;
}
vote += num == x ? 1 : -1;
}
return x;
}

public static void main(String[] args) {
int[] nums = {1, 3, 3, 3, 3, 3, 1, 2, 2, 2};
System.out.println(majorityElement(nums));
}
}


## 4. 最小的k个数

（1）题目描述

（2）题目分析

（3）代码

package swordOffer.day5;

/**
* @author chengzhengda
* @version 1.0
* @date 2020-03-25 20:22
* @desc
*/
public class t24 {

public static int[] getLeastNumbers(int[] arr, int k) {
if (k == 0) {
return new int[0];
}
if (arr.length <= k) {
return arr;
}
int res[] = new int[k];
partitionArray(arr, 0, arr.length - 1, k);
for (int i = 0; i < k; i++) {
res[i] = arr[i];
}
return res;
}

public static void partitionArray(int[] arr, int begin, int end, int k) {
int m = quickSort(arr, begin, end);
if (m > k) {
partitionArray(arr, begin, m - 1, k);
} else if (m < k) {
partitionArray(arr, m + 1, end, k);
}
}

public static int quickSort(int[] arr, int begin, int end) {
int i = begin;
int j = end;
int temp = arr[i];
while (i < j) {
while (i < j && arr[j] >= temp) {
j--;
}
arr[i] = arr[j];
while (i < j && arr[i] <= temp) {
i++;
}
arr[j] = arr[i];
}
arr[i] = temp;
return j;
}

public static void main(String[] args) {
int[] arr = {1, 5, 3, 2, 0, 6};
int[] res = getLeastNumbers(arr, 2);
for (int i : res) {
System.out.println(i);
}
}
}


## 5. 连续子数组的最大和

（1）题目描述

（2）题目分析

（3）代码

package swordOffer.day5;

/**
* @author chengzhengda
* @version 1.0
* @date 2020-03-25 21:44
* @desc
*/
public class t25 {
public static int maxSubArray(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int sum = nums[0];
int max = nums[0];
for (int i = 1; i < nums.length; i++) {
if (sum < 0) {
sum = nums[i];
} else {
sum += nums[i];
}
max = Math.max(sum, max);
}
return max;
}

public static void main(String[] args) {
int[] arr = {-2, 1};
System.out.println(maxSubArray(arr));
}
}


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客