Skip to content

Instantly share code, notes, and snippets.

View clarkdo's full-sized avatar

Xin Du (Clark) clarkdo

  • Dublin, Ireland
View GitHub Profile
class Solution {
public int solution(int[] A) {
int length = A.length;
boolean[] appearance = new boolean[length];
int count = 0;
for (int i : A) {
int position = i-1;
if (i <= length && !appearance[position]) {
appearance[position] = true;
count++;
class Solution {
public int solution(int X, int[] A) {
int second = -1;
int count = 0;
boolean[] counter = new boolean[X];
for (int i = 0; i < A.length; i++) {
int pos = A[i];
if (pos <= A.length && !counter[pos-1]) {
counter[pos-1] = true;
count++;
class Solution {
public int solution(int[] A) {
int min = A.length + 1;
int length = A.length;
boolean[] appearance = new boolean[length];
for (int i = 0; i < length; i++) {
if (A[i] > 0 && A[i] <= length) {
appearance[A[i]-1] = true;
}
}
class Solution {
public int solution(int A, int B, int K) {
return B/K - (A == 0 ? -1 : (A-1)/K);
}
}
class Solution {
public int solution(int[] A) {
int pairs = 0;
int easts = 0;
for (int i : A) {
if (i == 0) {
easts++;
} else {
pairs += easts;
}
import java.util.Arrays;
class Solution {
public int solution(int[] A) {
if (A == null || A.length < 3) return 0;
int max = Integer.MAX_VALUE;
Arrays.sort(A);
long last = A[A.length-1];
for (int i = 0; i < A.length-2; i++) {
long P = A[i];
import java.util.Set;
import java.util.HashSet;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
Set<Integer> distincts = new HashSet<>();
for (int i: A) {
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
Arrays.sort(A);
int length = A.length;
import java.util.List;
import java.util.ArrayList;
class Solution {
public int solution(String S) {
if (S == null || S.length() == 0) return 1;
if (S.length() % 2 != 0) return 0;
char first = S.charAt(0);
if (first != '{' && first != '[' && first != '(') return 0;
class Solution {
public int solution(int[] A) {
int length = A.length;
int leader = A[0];
int votes = 1;
//Boyer–Moore majority vote algorithm
for (int i = 1; i < length; i++) {
int digit = A[i];