Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/MoveZeroes/Mnting/283.MoveZeros.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
package MoveZeroes.Mnting;
/*
* @lc app=leetcode.cn id=283 lang=java
*
* [283] 移动零
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/MoveZeroes/beau0303/MoveZeroes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package MoveZeroes.beau0303;

/**
* Created by IntelliJ IDEA.
* Author:MeiFan
* Date: 2019/5/29 15:37
* e-mail:[email protected].
*/
public class MoveZeroes {
public static void moveZeroes(int[] a) {
if (a.length == 1)
return;
for (int cur = 0, i = cur; i < a.length; cur++, i++) {
if (0 == a[cur]) { //cur定位到第一个0的位置,i去寻找后面第一个非0的值替换过来
while (i < a.length && a[i] == 0) {
i++;
}
if (i >= a.length)//判断是否被越界 如果越界 说明后面没有非0元素 直接返回
break;
swap(a, i, cur);
}
}
}

public static void swap(int a[], int i, int j) {
a[i] = a[i] ^ a[j];
a[j] = a[j] ^ a[i];
a[i] = a[i] ^ a[j];
}

public static void main(String[] args) {
int[] a = new int[]{1, 3, 0, 5, 0};
moveZeroes(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}

}
21 changes: 21 additions & 0 deletions src/main/java/RemoveElement/beau0303/RemoveElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package RemoveElement.beau0303;

/**
* Created by IntelliJ IDEA.
* Author:MeiFan
* Date: 2019/5/30 13:11
* e-mail:[email protected].
*/
public class RemoveElement {
/*双指针,i从头开始定位,j去寻找非val的元素,然后把它赋值给i,这样遍历完,前i个元素都是非val的,return i*/
public int removeElement(int[] a, int val) {
int i, j;
for (i = 0, j = 0; j < a.length; j++) {
if (a[j]!=val){
a[i]=a[j];
i++;
}
}
return i;
}
}
44 changes: 44 additions & 0 deletions src/main/java/ValidParentheses/beau0303/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ValidParentheses.beau0303;

import java.util.Stack;

/**
* Created by IntelliJ IDEA.
* Author:MeiFan
* Date: 2019/6/6 8:00
* e-mail:[email protected].
*/
public class Solution {
public static boolean isValid(String s) {
char[] chars = s.toCharArray();
Stack<Character> stack = new Stack();
for (int i = 0; i < chars.length; i++) {
if (chars[i] == '(' || chars[i] == '[' || chars[i] == '{')
stack.push(chars[i]);
else if(stack.isEmpty())
return false;
else if(isMatch(stack.peek(),chars[i]))
stack.pop();
else return false;

}
return stack.isEmpty();
}

public static boolean isMatch(char c1, char c2) {
if (c1 == '(' && c2 == ')')
return true;
else if (c1 == '[' && c2 == ']')
return true;
else if (c1 == '{' && c2 == '}')
return true;
else return false;
}

public static void main(String[] args) {


boolean valid = isValid("()()()()");
System.out.println(valid);
}
}