View exception_handler.rb
module ExceptionHandler
extend ActiveSupport::Concern
included do
rescue_from ActiveRecord::RecordNotFound do |e|
render json: { message: e.message }, status: :not_found
end
end
end
View post_spec.rb
require 'rails_helper'
RSpec.describe "Posts", type: :request do
let!(:posts) { create_list(:post, 10) }
let(:post_id) { posts.last.id }
describe "GET /posts" do
before { get posts_path }
it "returns status code 200" do
View rails_helper.diff
diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb
index 46ba8e0..3b99862 100644
--- a/spec/rails_helper.rb
+++ b/spec/rails_helper.rb
@@ -7,6 +7,8 @@ abort("The Rails environment is running in production mode!") if Rails.env.produ
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
+require 'database_cleaner'
+
View MaximalRectangle.java
import java.util.Stack;
public class MaximalRectangle {
static int findMaxArea(int l, int[] heights) {
Stack<Integer> stack = new Stack<Integer>();
int max_area = 0;
for (int i = 0; i < l; i++) {
while (!stack.isEmpty() && heights[stack.peek()] > heights[i]) {
int top_height = heights[stack.pop()];
int w = stack.isEmpty() ? i : i - stack.peek() - 1;
View MaximalSquare.java
public class MaximalSquare {
static int min(int a, int b, int c) {
return Math.min(a, Math.min(b, c));
}
static int findMaxSquare(int[][] matrix) {
int n = matrix.length;
int m = matrix[0].length;
int[][] memo = new int[n][m];
int max = 0;
View NestedListIterator.java
import java.util.*;
public class NestedListIterator implements Iterator<Integer> {
interface NestedInteger {
boolean isInteger();
Integer getInteger();
List<NestedInteger> getList();
}
Stack<Iterator<NestedInteger>> stack;
View Vector2DIterator.java
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class Vector2DIterator implements Iterator<Integer> {
Iterator<List<Integer>> parent = null;
Iterator<Integer> child = null;
void updateChild() {
while (!child.hasNext() && parent.hasNext()) {
View RestoreLotteryNumber.java
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class RestoreLotteryNumber {
static void restoreLotteryNumbers(String s, List<String> result, Set<String> seen,
String number, int count, int index) {
if (count > 7) { return; }
if (count == 7 && number.length() == (s.length() + 6)) {
View RestoreIpAddresses.java
import java.util.ArrayList;
import java.util.List;
public class RestoreIpAddresses {
static void restoreIpAddresses(String s, List<String> result, String ip, int count, int index) {
if (count > 4) { return; }
if (count == 4 && ip.length() == (s.length() + 3)) {
result.add(ip);
}
View FindTheCelebrity.java
import java.util.*;
public class FindTheCelebrity {
static int N = 7;
static Map<Integer, List<Integer>> relation = new HashMap();
static {
relation.put(0, Arrays.asList(1));
relation.put(1, new ArrayList()); // celebrity
relation.put(2, Arrays.asList(1));
relation.put(3, Arrays.asList(1, 2));