Skip to content

Commit

Permalink
摩尔投票
Browse files Browse the repository at this point in the history
  • Loading branch information
cdtft committed May 11, 2020
1 parent b3a6704 commit 5b3adb1
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/main/java/com/cdtft/leetcode/MinStack.java
@@ -0,0 +1,68 @@
package com.cdtft.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
* @author : wangcheng
* @date : 2020年05月11日 11:13
*/
public class MinStack {

private Integer minValue;

private List<Node> nodeList;

public MinStack() {
this.nodeList = new ArrayList<>();
}

public void push(int x) {
if (nodeList.size() == 0) {
minValue = x;
}

nodeList.add(new Node(x - minValue, x));
if (x < minValue) {
minValue = x;
}
}

public void pop() {
if (nodeList.size() == 0) {
return;
}
Node node = nodeList.remove(nodeList.size() - 1);
if (node.diff < 0) {
refreshMinValue(node);
}
}

public int top() {
if (nodeList.size() == 0) {
return Integer.MAX_VALUE;
}
Node node = nodeList.get(nodeList.size() - 1);
return node.value;
}

public int getMin() {
return minValue;
}

private void refreshMinValue(Node node) {
minValue = node.value - node.diff;
}

private class Node {

Integer diff;

Integer value;

public Node(Integer diff, Integer value) {
this.diff = diff;
this.value = value;
}
}
}
34 changes: 34 additions & 0 deletions src/main/java/com/cdtft/leetcode/Solution.java
@@ -1,8 +1,10 @@
package com.cdtft.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.TreeMap;
Expand Down Expand Up @@ -379,6 +381,38 @@ public boolean isPalindrome(String s) {
return true;
}

public int singleNumber(int[] nums) {
int result = 0;
for (int i : nums) {
result ^= nums[i];
}
return result;
}

public int majorityElement(int[] nums) {
Arrays.sort(nums);
return nums[nums.length / 2];
}

/**
* 摩尔投票
*
* @param nums
* @return
*/
public int majorityElement_1(int[] nums) {
int cand = nums[0];
int count = 1;
for (int i = 1; i < nums.length - 1; i++) {
if (cand == nums[i]) {
count++;
} else if (--count == 0) {
cand = nums[i];
cand = 1;
}
}
return cand;
}

public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>();
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/com/cdtft/leetcode/MinStackTest.java
@@ -0,0 +1,35 @@
package com.cdtft.leetcode;

import org.junit.Test;

import static org.junit.Assert.*;

/**
* ["MinStack","push","push","push","top","pop","getMin","pop","getMin","pop","push","top","getMin","push","top",
* "getMin","pop","getMin"]
* [[],[2147483646],[2147483646],[2147483647],[],[],[],[],[],[],[2147483647],[],[],[-2147483648],[],[],[],[]]
*/
public class MinStackTest {

@Test
public void push() {
MinStack minStack = new MinStack();
minStack.push(2147483646);
minStack.push(2147483646);
minStack.push(2147483647);
System.out.println(minStack.top());
minStack.pop();
System.out.println(minStack.getMin());
minStack.pop();
System.out.println(minStack.getMin());
minStack.pop();
minStack.push(2147483647);
System.out.println(minStack.top());
System.out.println(minStack.getMin());
minStack.push(-2147483648);
System.out.println(minStack.top());
System.out.println(minStack.getMin());
minStack.pop();
System.out.println(minStack.getMin());
}
}
6 changes: 6 additions & 0 deletions src/test/java/com/cdtft/leetcode/SolutionTest.java
Expand Up @@ -103,4 +103,10 @@ public void maxProfit() {
public void isPalindrome() {
System.out.println(solution.isPalindrome("race a car"));
}

@Test
public void singleNumber() {
int[] nums = new int[]{2, 2, 1};
System.out.println(2^2^2);
}
}

0 comments on commit 5b3adb1

Please sign in to comment.