Hello everyone, I'm
方圆
over and over again, only hand-cooked Seoul
Question number
Array operations
54. Spiral Matrix
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<>();
if(matrix == null || matrix.length == 0) return res;
int left = 0,right = matrix[0].length - 1;
int up = 0,down = matrix.length - 1;
while(left <= right && up <= down){
//注意每一步都要再加上left <= right && up <= down其中一个条件
//以防重复打印,因为在执行过程中各位置它是变化的
//从左向右
for(int i = left;i <= right && up <= down;i++)
res.add(matrix[up][i]);
up++;
//从上向下
for(int j = up;j <= down && left <= right;j++)
res.add(matrix[j][right]);
right--;
//从右向左
for(int i = right;i >= left && up <= down;i--)
res.add(matrix[down][i]);
down--;
//从下向上
for(int j = down;j >= up && left <= right;j--)
res.add(matrix[j][left]);
left++;
}
return res;
}
}
73. Matrix Zeroing
class Solution {
public void setZeroes(int[][] matrix) {
int row = matrix.length;
int col = matrix[0].length;
boolean row0_flag = false;
boolean col0_flag = false;
//第0行是否有0
for(int i = 0;i < col;i++){
if(matrix[0][i] == 0){
row0_flag = true;
break;
}
}
//第0列是否有0
for(int j = 0;j < row;j++){
if(matrix[j][0] == 0){
col0_flag = true;
break;
}
}
//以第0行和第0列为标志位
for(int i = 1;i < row;i++){
for(int j = 1;j < col;j++){
if(matrix[i][j] == 0){
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
//置0
for(int i = 1;i < row;i++){
for(int j = 1;j < col;j++){
if(matrix[i][0] == 0 || matrix[0][j] == 0){
matrix[i][j] = 0;
}
}
}
if(row0_flag){
for(int i = 0;i < col;i++){
matrix[0][i] = 0;
}
}
if(col0_flag){
for(int j = 0;j < row;j++){
matrix[j][0] = 0;
}
}
}
}
78. Subset
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
res.add(new ArrayList<>());
for(int i = 0;i < nums.length;i++){
int size = res.size();
for(int j = 0;j < size;j++){
List<Integer> temp = new ArrayList<>(res.get(j));
temp.add(nums[i]);
res.add(temp);
}
}
return res;
}
}
384. Shuffle the array
class Solution {
private int[] array;
private int[] original;
private Random rand = new Random();
public Solution(int[] nums) {
array = nums;
original = nums.clone();
}
/** Resets the array to its original configuration and return it. */
public int[] reset() {
return original;
}
/** Returns a random shuffling of the array. */
public int[] shuffle() {
List<Integer> copyArray = getCopyArray();
for(int i = 0;i < array.length;i++){
int remove = rand.nextInt(copyArray.size());
array[i] = copyArray.get(remove);
copyArray.remove(remove);
}
return array;
}
private List<Integer> getCopyArray(){
List<Integer> copyArray = new ArrayList<>();
for(int i = 0;i < array.length;i++)
copyArray.add(array[i]);
return copyArray;
}
}
581. The shortest unordered contiguous subarray
class Solution {
public int findUnsortedSubarray(int[] nums) {
int len = nums.length;
int left = 0,right = -1;
int max = nums[0],min = nums[len - 1];
for(int i = 0;i < len;i++){
if(nums[i] >= max) max = nums[i];
else right = i;
if(nums[len - 1- i] <= min) min = nums[len - 1 - i];
else left = len - 1 - i;
}
return right - left + 1;
}
}
945. Minimum increment to make the array unique
class Solution {
public int minIncrementForUnique(int[] A) {
// 先排序
Arrays.sort(A);
int move = 0;
// 遍历数组,若当前元素小于等于它的前一个元素,则将其变为前一个数+1
for (int i = 1; i < A.length; i++) {
if (A[i] <= A[i - 1]) {
int pre = A[i];
A[i] = A[i - 1] + 1;
move += A[i] - pre;
}
}
return move;
}
}
Stack related
20. Valid parentheses
class Solution {
public boolean isValid(String s) {
if(s.length() == 0) return true;
if(s.length() % 2 == 1) return false;
Stack<Character> stack = new Stack<>();
for(char c : s.toCharArray()){
if(c == '(') stack.push(')');
else if(c == '[') stack.push(']');
else if(c == '{') stack.push('}');
else if(stack.empty() || stack.pop() != c)
return false;
}
return stack.empty();
}
}
155. Minimal Stack
class MinStack {
private Stack<Integer> stack;
private Stack<Integer> minStack;
/** initialize your data structure here. */
public MinStack() {
stack = new Stack<>();
minStack = new Stack<>();
}
public void push(int x) {
stack.push(x);
//注意这里小于等于号
if(minStack.empty() || x <= minStack.peek())
minStack.push(x);
}
public void pop() {
int x = stack.pop();
if(x == minStack.peek())
minStack.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return minStack.peek();
}
}
232. Realize Queue with Stack
class MyQueue {
Stack<Integer> stackIn;
Stack<Integer> stackOut;
/** Initialize your data structure here. */
public MyQueue() {
stackIn = new Stack<>();
stackOut = new Stack<>();
}
/** Push element x to the back of queue. */
public void push(int x) {
stackIn.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
if(stackOut.empty()){
while(!stackIn.empty())
stackOut.push(stackIn.pop());
}
return stackOut.pop();
}
/** Get the front element. */
public int peek() {
if(stackOut.empty()){
while(!stackIn.empty())
stackOut.push(stackIn.pop());
}
return stackOut.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
return stackOut.empty() && stackIn.empty();
}
}