大家好,我是
方圆
无它,唯手熟尔
题号
数组操作
54. 螺旋矩阵
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) {
for(int i = left; i <= right && up <= down; i++) {
res.add(matrix[up][i]);
}
up++;
for(int i = up; i <= down && left <= right; i++) {
res.add(matrix[i][right]);
}
right--;
for(int i = right; i >= left && up <= down; i--) {
res.add(matrix[down][i]);
}
down--;
for(int i = down; i >= up && left <= right; i--) {
res.add(matrix[i][left]);
}
left++;
}
return res;
}
}
73. 矩阵置零
class Solution {
public void setZeroes(int[][] matrix) {
int row = matrix.length, col = matrix[0].length;
boolean rowFlag = false, colFlag = false;
for(int i = 0; i < col; i++) {
if(matrix[0][i] == 0) {
rowFlag = true;
break;
}
}
for(int j = 0; j < row; j++) {
if(matrix[j][0] == 0) {
colFlag = true;
break;
}
}
for(int i = 1; i < row; i++) {
for(int j = 1; j < col; j++) {
if(matrix[i][j] == 0) {
matrix[0][j] = 0;
matrix[i][0] = 0;
}
}
}
for(int i = 1; i < row; i++) {
for(int j = 1; j < col; j++) {
if(matrix[0][j] == 0 || matrix[i][0] == 0)
matrix[i][j] = 0;
}
}
if(rowFlag) {
for(int i = 0; i < col; i++) {
matrix[0][i] = 0;
}
}
if(colFlag) {
for(int i = 0; i < row; i++) {
matrix[i][0] = 0;
}
}
}
}
78. 子集
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++) {
ArrayList<Integer> temp = new ArrayList<>(res.get(j));
temp.add(nums[i]);
res.add(temp);
}
}
return res;
}
}
384. 打乱数组
class Solution {
private int[] array;
private int[] original;
private Random random = 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> copy = new ArrayList<>();
for(int i = 0; i < array.length; i++) {
copy.add(array[i]);
}
for(int i = 0; i < array.length; i++) {
int index = random.nextInt(copy.size());
array[i] = copy.remove(index);
}
return array;
}
}
581. 最短无序连续子数组
class Solution {
public int findUnsortedSubarray(int[] nums) {
int len = nums.length;
//这个right = -1 实在是有点儿东西
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 - i - 1] <= min) min = nums[len - 1 - i];
else left = len - i - 1;
}
return right - left + 1;
}
}
945. 使数组唯一的最小增量
class Solution {
public int minIncrementForUnique(int[] A) {
Arrays.sort(A);
int res = 0;
for(int i = 0; i < A.length - 1; i++) {
if(A[i + 1] <= A[i]) {
int temp = A[i + 1];
A[i + 1] = A[i] + 1;
res += A[i + 1] - temp;
}
}
return res;
}
}
栈相关
20. 有效的括号
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();
}
}
32. 最长有效括号
class Solution {
public int longestValidParentheses(String s) {
int res = 0;
Stack<Integer> stack = new Stack<>();
stack.push(-1);
for(int i = 0; i < s.length(); i++) {
if(s.charAt(i) == '(') {
stack.push(i);
}else {
stack.pop();
if(stack.empty()) {
stack.push(i);
}else {
res = Math.max(res,i - stack.peek());
}
}
}
return res;
}
}
155. 最小栈
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. 用栈实现队列
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();
}
}
316. 去除重复字母
class Solution {
public String removeDuplicateLetters(String s) {
Stack<Character> stack = new Stack<>();
char[] chars = s.toCharArray();
for(int i = 0; i < chars.length; i++) {
if(stack.contains(chars[i])) continue;
while(!stack.empty() && stack.peek() > chars[i]
&& s.indexOf(stack.peek(),i) != -1)
stack.pop();
stack.push(chars[i]);
}
StringBuilder res = new StringBuilder();
while(!stack.empty()) res.append(stack.pop());
return res.reverse().toString();
}
}
堆相关
215. 数组中的第K个最大值
class Solution {
public int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> heap = new PriorityQueue<>();
for(int i = 0; i < nums.length; i++) {
heap.offer(nums[i]);
if(heap.size() > k) heap.poll();
}
return heap.poll();
}
}
347. 前K个高频元素
class Solution {
public int[] topKFrequent(int[] nums, int k) {
HashMap<Integer,Integer> map = new HashMap<>();
for(int n : nums) map.put(n,map.getOrDefault(n,0) + 1);
PriorityQueue<Integer> heap = new PriorityQueue<>(
(a,b) -> map.get(a) - map.get(b));
for(int i : map.keySet()) {
heap.offer(i);
if(heap.size() > k) heap.poll();
}
int[] res = new int[k];
for(int i = 0; i < k; i++) res[i] = heap.poll();
return res;
}
}
加油儿!