Datawhale 系列數據結構
Task2.1 棧
2.1.1用數組實現一個順序棧
public class ArrayStack<T> {
private T [] data;
private int size;
private int cnt;
@SuppressWarnings("unchecked")
public ArrayStack(){
data= (T[]) new Object [10];
cnt = 0;
size =10;
}
public void push(T t){
if(cnt>=size){
data=Arrays.copyOf(data, data.length*2);
size*=2;
}
data[size] = t;
cnt++;
}
public T peek(){
if (cnt==0) {
throw new EmptyStackException();
}
return data[cnt];
}
public T pop(){
if (cnt==0) {
throw new EmptyStackException();
}
cnt--;
return data[cnt];
}
public int search(T t){
for(int i=cnt;i>0;i--){
if(data[i]==t)
return i;
}
return -1;
}
public boolean isEmpty(){
return cnt==0;
}
}
2.1.2 用鏈表實現一個鏈式棧
public class ListStack<T> {
private List<T> data;
private int cnt;
public ListStack(){
data= new LinkedList<T>();
cnt = 0;
}
public void push(T t){
data.add(t);
cnt++;
}
public T peek(){
if (cnt==0) {
throw new EmptyStackException();
}
return data.get(cnt);
}
public T pop(){
if (cnt==0) {
throw new EmptyStackException();
}
T t=data.remove(cnt);
cnt--;
return t;
}
public int search(T t){
for(int i=cnt;i>0;i--){
if(data.get(i)==t)
return i;
}
return -1;
}
public boolean isEmpty(){
return cnt==0;
}
}
2.1.3 編程模擬實現一個瀏覽器的前進後退功能
public class BrowserSimula {
public static void main(String[] args) {
Browser b1=new Browser();
b1.open();
b1.open();
b1.open();
b1.open();
System.out.println(b1.backward());
System.out.println(b1.backward());
System.out.println(b1.forward());
System.out.println(b1.forward());
}
}
class Browser{
private Stack<Integer> s1;
private Stack<Integer> s2;
int cnt;
Browser(){
s1=new Stack<Integer>();
s2=new Stack<Integer>();
cnt=0;
}
/**
* 點開一個新的鏈接
*/
public void open(){
cnt++;
s1.push(cnt);
}
//後退
public Integer backward(){
if(cnt==0)
throw new ArrayIndexOutOfBoundsException(cnt);
Integer a =s1.pop();
s2.push(a);
return s1.peek();
}
//前進
public Integer forward(){
if(s2.isEmpty())
throw new ArrayIndexOutOfBoundsException(cnt);
Integer a =s2.pop();
s1.push(a);
return a;
}
}
2.1.4 練習
//Valid Parentheses(有效的括號)
private HashMap<Character, Character> mappings;
public Solution() {
this.mappings = new HashMap<Character, Character>();
this.mappings.put(')', '(');
this.mappings.put('}', '{');
this.mappings.put(']', '[');
}
public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (this.mappings.containsKey(c)) {
char topElement = stack.empty() ? '#' : stack.pop();
if (topElement != this.mappings.get(c)) {
return false;
}
} else {
stack.push(c);
}
}
return stack.isEmpty();
}
//Longest Valid Parentheses(最長有效的括號)[作爲可選]
/*dp 方法:
我們用 dp[i] 表示以 i 結尾的最長有效括號;
1 當 s[i] 爲 ( , dp[i] 必然等於0,因爲不可能組成有效的括號;
2 那麼 s[i] 爲 )
2.1 當 s[i-1] 爲 (,那麼 dp[i] = dp[i-2] + 2;
2.2 當 s[i-1] 爲 ) 並且 s[i-dp[i-1] - 1] 爲 (,那麼 dp[i] = dp[i-1] + 2 + dp[i-dp[i-1]-2];
時間複雜度:O(n)
*/
public int longestValidParentheses(String s) {
if (s == null || s.length() == 0) return 0;
int[] dp = new int[s.length()];
int res = 0;
for (int i = 0; i < s.length(); i++) {
if (i > 0 && s.charAt(i) == ')') {
if (s.charAt(i - 1) == '(') {
dp[i] = (i - 2 >= 0 ? dp[i - 2] + 2 : 2);
} else if (s.charAt(i - 1) == ')' && i - dp[i - 1] - 1 >= 0 && s.charAt(i - dp[i - 1] - 1) == '(') {
dp[i] = dp[i - 1] + 2 + (i - dp[i - 1] - 2 >= 0 ? dp[i - dp[i - 1] - 2] : 0);
}
}
res = Math.max(res, dp[i]);
}
return res;
}
//Evaluate Reverse Polish Notatio(逆波蘭表達式求值)
public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
for (int i = 0 ;i < tokens.length;i++){
String str = tokens[i];
if (str.length() == 1){
char ch = str.charAt(0);
if (ch-'0' >= 0 && ch - '0' <= 9 ){
Integer a = Integer.valueOf(str);
stack.push(a);
}
else{
if (stack.size() < 2)
return 0;
int num2 = stack.pop();
int num1 = stack.pop();
switch(ch){
case '+':
stack.push(num1 + num2);
break;
case '-':
stack.push(num1 - num2);
break;
case '*':
stack.push(num1 * num2);
break;
case '/':
stack.push(num1 / num2);
break;
}
}
}else{
int n = Integer.valueOf(str);
stack.push(n);
}
}
return stack.pop();
}
TASK2.2 隊列
2.2.1 用數組實現一個順序隊列
public class ArrayQueue<T> {
private T [] data ;
private int cnt;//元素個數
private int size;//隊列長度
@SuppressWarnings("unchecked")
public ArrayQueue(){
data =(T[]) new Object [10];
cnt = 0;
size = 10;
}
@SuppressWarnings("unchecked")
public ArrayQueue(int size){
data =(T[]) new Object [size];
cnt = 0;
this.size = size;
}
public void add(T t){
if(cnt>=size){
throw new IllegalStateException();
}
data[cnt]=t;
cnt++;
}
public T remove(){
if(cnt<0){
throw new NoSuchElementException();
}
T t= data[0];
data = Arrays.copyOfRange(data,0,size);
cnt--;
return t;
}
public boolean offer(T t){
if(cnt>=size){
return false;
}
data[cnt]=t;
cnt++;
return true;
}
public boolean pull(T t){
if(cnt<0){
return false;
}
data = Arrays.copyOfRange(data,0,size);
cnt--;
return true;
}
//返回隊列頭元素
public T element(){
return data[0];
}
public boolean isEmpty(){
return cnt==0 ;
}
public boolean isFull(){
return cnt==size;
}
}
2.2.2 用鏈表實現一個鏈式隊列
public class ListQueue<T> {
private List<T> data ;
private int cnt;//元素個數
private int size;//隊列長度(用鏈表的話這裏可以強行定義)
public ListQueue(){
data =new LinkedList<T>();
cnt = 0;
size = 10;
}
public void add(T t){
data.add(t);
cnt++;
}
public T remove(){
if(cnt<0){
throw new NoSuchElementException();
}
T t= data.remove(cnt);
cnt--;
return t;
}
public boolean offer(T t){
if(cnt>=size){
return false;
}
data.add(t);
cnt++;
return true;
}
public boolean pull(T t){
if(cnt<0){
return false;
}
data.remove(cnt);
cnt--;
return true;
}
//返回隊列頭元素
public T element(){
return data.get(0);
}
public boolean isEmpty(){
return cnt==0 ;
}
public boolean isFull(){
return cnt==size;
}
}
2.2.3 實現一個循環隊列
public class MyCircularQueue {
private final int capacity;
private final int[] array;
private int head = 0;
private int tail = 0;
private int count = 0;
/**
* Initialize your data structure here. Set the size of the queue to be k.
*/
public MyCircularQueue(int k) {
this.capacity = k;
this.array = new int[this.capacity];
}
/**
* Insert an element into the circular queue. Return true if the operation is successful.
*/
public boolean enQueue(int value) {
//隊列已滿
if (count == capacity) {
return false;
}
//隊列爲空, 重新設置頭部
if (count == 0) {
head = (head == capacity) ? 0 : head;
tail = head;
array[head] = value;
count++;
return true;
}
//隊列未滿 (有空位)
if (tail == capacity - 1) {
//tail 達到 maxIndex, 重置爲 0
tail = 0;
} else {
//tail 未達到 maxIndex, tail++
tail++;
}
array[tail] = value;
count++;
return true;
}
/**
* Delete an element from the circular queue. Return true if the operation is successful.
*/
public boolean deQueue() {
if (count == 0) {
//隊列爲空
return false;
}
count--;
head++;
return true;
}
/**
* Get the front item from the queue.
*/
public int Front() {
if (count == 0 ) {
return -1;
}
return array[head];
}
/**
* Get the last item from the queue.
*/
public int Rear() {
if (count == 0 ) {
return -1;
}
return array[tail];
}
/**
* Checks whether the circular queue is empty or not.
*/
public boolean isEmpty() {
return count == 0;
}
/**
* Checks whether the circular queue is full or not.
*/
public boolean isFull() {
return count == capacity;
}
}
2.2.4 練習
//Design Circular Deque(設計一個雙端隊列)
class MyCircularDeque {
public int k;
public int[] numbers;
public int head;
public int tail;
/** Initialize your data structure here. Set the size of the deque to be k. */
public MyCircularDeque(int k) {
numbers=new int[k+1];
head=0;
tail=0;
this.k=k;
}
/** Adds an item at the front of Deque. Return true if the operation is successful. */
public boolean insertFront(int value) {
if(isFull())
return false;
else{
head=(head+k)%(k+1);
numbers[head]=value;
return true;
}
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
public boolean insertLast(int value) {
if(isFull())
return false;
else{
numbers[tail]=value;
tail=(tail+1)%(k+1);
return true;
}
}
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
public boolean deleteFront() {
if(isEmpty())
return false;
else{
head=(head+1)%(k+1);
return true;
}
}
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
public boolean deleteLast() {
if(isEmpty())
return false;
else{
tail=(tail+k)%(k+1);
return true;
}
}
/** Get the front item from the deque. */
public int getFront() {
if(isEmpty())
return -1;
return numbers[head];
}
/** Get the last item from the deque. */
public int getRear() {
if(isEmpty())
return -1;
return numbers[(tail+k)%(k+1)];
}
/** Checks whether the circular deque is empty or not. */
public boolean isEmpty() {
return tail==head;
}
/** Checks whether the circular deque is full or not. */
public boolean isFull() {
return (tail+1)%(k+1)==head;
}
}
//Sliding Window Maximum(滑動窗口最大值)
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if(k==0){
return new int[0];
}
List<Integer> list = new ArrayList<>();//存放窗口內的數字
int max = Integer.MIN_VALUE;//窗口內的最大數字
for(int i = 0; i<k;i++){
if(max<nums[i]){
max = nums[i];
}
list.add(nums[i]);
}
int[] res = new int[nums.length - k + 1];//要返回的結果數據
res[0] = max;
for(int i = k; i < nums.length;i++){
int z =list.remove(0);//移走第一位數並插入新的一位數
list.add(nums[i]);
if(z!=max){//移走的數不是max,只需判斷max與新插入的數哪個大
if(nums[i]> max){
max = nums[i];
}
res[i-k+1] = max;
}else{//移走的數是max,重新判斷列表中哪個數是最大的
if(!list.contains(max)){
max = Integer.MIN_VALUE;
for(Integer num : list){
if(max<num){
max = num;
}
}
}else{
if(nums[i]> max){
max = nums[i];
}
}
}
res[i-k+1] = max;
}
return res;
}
}
3 遞歸
3.1 編程實現斐波那契數列求值 f(n)=f(n-1)+f(n-2)
public static int Fibe(int n){
if(n==1) return 1;
if(n==2) return 1;
return Fibe(n-1)+Fibe(n-2);
}
3.2 編程實現求階乘 n!
public static int factorial(int n){
if(n==1) return 1;
return factorial(n-1)*n;
}
3.3 編程實現一組數據集合的全排列
class Solution {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> permute(int[] nums) {
permition(nums, 0, nums.length-1);
return res;
}
public void permition(int[] nums, int p, int q){
if(p==q){
res.add(arrayToList(nums));
}
for(int i = p; i <= q; i++){
swap(nums, i, p);
permition(nums, p+1, q);
swap(nums, i,p);
}
}
private List<Integer> arrayToList(int[] nums){
List<Integer> res = new ArrayList<>();
for(int i = 0; i < nums.length; i++){
res.add(nums[i]);
}
return res;
}
private void swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
3.4 練習
// Climbing Stairs(爬樓梯)
//直接遞歸,但是直接遞歸leetcode竟然不能通過,於是又方法2
class Solution {
public int climbStairs(int n) {
if(n==1) return 1;
if(n==2) return 2;
return climbStairs(n-1)+climbStairs(n-2);
}
}
//非遞歸放啊
class Solution {
public int climbStairs(int n) {
int [] ways = new int[n+1];
ways[0] = 0;
for (int i = 1;i<ways.length;i++){
if (i < 3 ){
ways[i] = i;
}else {
ways[i] = ways[i-1] + ways[i-2];
}
}
return ways[n];
}
}