1
This commit is contained in:
commit
27490a9869
@ -1,6 +1,10 @@
|
||||
package com.raorao;
|
||||
|
||||
<<<<<<< HEAD
|
||||
import java.util.Scanner;
|
||||
=======
|
||||
import java.util.HashMap;
|
||||
>>>>>>> 1c2b8c5ebb8faea5298e53672bf08b78be1c7c89
|
||||
|
||||
/**
|
||||
* .
|
||||
@ -13,10 +17,15 @@ public class Test {
|
||||
private int a;
|
||||
|
||||
public static void main(String[] args) {
|
||||
<<<<<<< HEAD
|
||||
String[] s = new String[10];
|
||||
System.out.println(s.length);
|
||||
System.out.println(s[0]);
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
=======
|
||||
|
||||
|
||||
>>>>>>> 1c2b8c5ebb8faea5298e53672bf08b78be1c7c89
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,43 @@
|
||||
package com.raorao.interview.bytedance.th9.t1;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-09-09-10:01
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
String input = scanner.nextLine();
|
||||
System.out.print(process(input));
|
||||
}
|
||||
|
||||
private static int process(String s){
|
||||
if (s == null || s.length() == 0) {
|
||||
return 0;
|
||||
}
|
||||
Map<Character, Integer> map = new HashMap<>();
|
||||
int result = 1;
|
||||
int count = 0;
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
if (!map.containsKey(s.charAt(i))) {
|
||||
map.put(s.charAt(i), i);
|
||||
count++;
|
||||
} else {
|
||||
count = 0;
|
||||
i = map.get(s.charAt(i));
|
||||
map.clear();
|
||||
}
|
||||
if (result < count) {
|
||||
result = count;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package com.raorao.interview.bytedance.th9.t2;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-09-09-10:11
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
private static int[][] matrix;
|
||||
private static int[][] direction = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
|
||||
private static int m;
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
m = scanner.nextInt();
|
||||
matrix = new int[m][m];
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < m; j++) {
|
||||
matrix[i][j] = scanner.nextInt();
|
||||
}
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
if(m == 0){
|
||||
System.out.println(0);
|
||||
}
|
||||
for (int i = 0; i<m; i++){
|
||||
for (int j = 0; j<m;j++){
|
||||
if (matrix[i][j] != 0){
|
||||
dfs(i,j);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println(count);
|
||||
|
||||
}
|
||||
|
||||
private static void dfs(int i , int j) {
|
||||
if(i <0 || i >= m || j < 0 || j >= m || matrix[i][j] == 0){
|
||||
return;
|
||||
}
|
||||
matrix[i][j] = 0;
|
||||
for (int[] d : direction){
|
||||
dfs(i+d[0], j+ d[1]);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package com.raorao.interview.bytedance.th9.t3;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-09-09-10:33
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
String input = scanner.nextLine();
|
||||
System.out.println(restoreIpAddresses(input));
|
||||
}
|
||||
|
||||
public static int restoreIpAddresses(String s) {
|
||||
List<String> ret = new ArrayList<>();
|
||||
process(0, new StringBuilder(), ret, s);
|
||||
return ret.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param k 表示当前已经分割的ip段
|
||||
*/
|
||||
private static void process(int k, StringBuilder sb, List<String> ret, String s) {
|
||||
|
||||
if (k == 4 || s.length() == 0) {
|
||||
if (k == 4 && s.length() == 0) {
|
||||
ret.add(sb.toString());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < s.length() && i <= 2; i++) {
|
||||
if (i != 0 && s.charAt(0) == '0') {
|
||||
break;
|
||||
}
|
||||
String part = s.substring(0, i + 1);
|
||||
if (Integer.valueOf(part) <= 255) {
|
||||
if (sb.length() != 0) {
|
||||
part = "." + part;
|
||||
}
|
||||
sb.append(part);
|
||||
process(k + 1, sb, ret, s.substring(i + 1));
|
||||
sb.delete(sb.length() - part.length(), sb.length());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
package com.raorao.interview.bytedance.th9.t4;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-09-09-10:38
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int n = scanner.nextInt();
|
||||
int[] nums = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
nums[i] = scanner.nextInt();
|
||||
}
|
||||
|
||||
if(valid(nums)){
|
||||
System.out.println(1);
|
||||
}else {
|
||||
System.out.println(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static boolean valid(int[] data){
|
||||
boolean isUtf8 = false;// 当前num是否在一个验证的字符中
|
||||
int times = 0;
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
if(isUtf8==false){
|
||||
times = check(data[i]);//当前编码需要多少个10
|
||||
if (times == -2 || times == 0)//非编码
|
||||
return false;
|
||||
if(times>0)//后面需要跟多少个10编码
|
||||
isUtf8=true;
|
||||
}else{
|
||||
if(times>0&&check(data[i])==0)//是10编码
|
||||
times--;
|
||||
else
|
||||
return false;
|
||||
if(times==0)
|
||||
isUtf8=false;
|
||||
}
|
||||
}
|
||||
return times>0?false:true;
|
||||
}
|
||||
|
||||
public static int check(int n) {
|
||||
if ((128 & n) == 0) {
|
||||
return -1;
|
||||
}
|
||||
if ((192 & n) == 128)//10
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if ((224 & n) == 192) {
|
||||
return 1;
|
||||
}
|
||||
if ((240 & n) == 224) {
|
||||
return 2;
|
||||
}
|
||||
if ((248 & n) == 240) {
|
||||
return 3;
|
||||
}
|
||||
return -2;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package com.raorao.interview.bytedance.th9.t5;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-09-09-11:07
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int n = scanner.nextInt();
|
||||
int m = scanner.nextInt();
|
||||
int[][] matrix = new int[n][n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (i == j) {
|
||||
matrix[i][j] = 0;
|
||||
} else {
|
||||
matrix[i][j] = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < m; i++) {
|
||||
int a = scanner.nextInt();
|
||||
int b = scanner.nextInt();
|
||||
matrix[a - 1][b - 1] = 0;
|
||||
}
|
||||
for (int k = 0; k < n; k++) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (matrix[i][j] < 0 && matrix[i][k] + matrix[k][j] == 0) {
|
||||
matrix[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
for (int j = 0; j < n; j++) {
|
||||
int i;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (matrix[i][j] < 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i == n) {
|
||||
ret++;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(ret);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
79
code/src/main/java/com/raorao/interview/jd/t1/Main.java
Normal file
79
code/src/main/java/com/raorao/interview/jd/t1/Main.java
Normal file
@ -0,0 +1,79 @@
|
||||
package com.raorao.interview.jd.t1;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-09-09-20:29
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int t = sc.nextInt();
|
||||
for (int i = 0; i < t; i++) {
|
||||
int n = sc.nextInt();
|
||||
int m = sc.nextInt();
|
||||
int[][] g = new int[n + 1][n + 1];
|
||||
for (int j = 0; j < m; j++) {
|
||||
int p = sc.nextInt();
|
||||
int q = sc.nextInt();
|
||||
g[p][q] = 1;
|
||||
g[q][p] = 1;
|
||||
}
|
||||
if (judge(g, n)) {
|
||||
System.out.println("Yes");
|
||||
} else {
|
||||
System.out.println("No");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean judge(int[][] g, int n) {
|
||||
Set<Set<Integer>> sets = new HashSet<>();
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= n; j++) {
|
||||
if (i == j) {
|
||||
continue;
|
||||
}
|
||||
if (g[i][j] == 0) {
|
||||
boolean exist = false;
|
||||
for (Set<Integer> set : sets) {
|
||||
if (set.contains(i) || set.contains(j)) {
|
||||
set.add(i);
|
||||
set.add(j);
|
||||
exist = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!exist) {
|
||||
Set<Integer> s = new HashSet<>();
|
||||
s.add(i);
|
||||
s.add(j);
|
||||
sets.add(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (Set<Integer> set : sets) {
|
||||
List<Integer> list = new ArrayList<>(set);
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
for (int j = 0; j < list.size(); j++) {
|
||||
int p = list.get(i);
|
||||
int q = list.get(j);
|
||||
if (g[p][q] == 1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
61
code/src/main/java/com/raorao/interview/jd/t2/Main.java
Normal file
61
code/src/main/java/com/raorao/interview/jd/t2/Main.java
Normal file
@ -0,0 +1,61 @@
|
||||
package com.raorao.interview.jd.t2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-09-09-20:01
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
private static List<P> list = new ArrayList<>();
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int n = scanner.nextInt();
|
||||
for (int i = 0; i < n; i++) {
|
||||
int a = scanner.nextInt();
|
||||
int b = scanner.nextInt();
|
||||
int c = scanner.nextInt();
|
||||
list.add(new P(a, b, c));
|
||||
}
|
||||
Set<Integer> set = new HashSet<>();
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
for (int j = i + 1; j < list.size(); j++) {
|
||||
if (compare(list.get(i), list.get(j))) {
|
||||
set.add(i);
|
||||
list.remove(i);
|
||||
}
|
||||
if (compare(list.get(j), list.get(i))) {
|
||||
set.add(j);
|
||||
list.remove(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println(set.size());
|
||||
|
||||
}
|
||||
|
||||
public static boolean compare(P p1, P p2) {
|
||||
return (p1.a < p2.a) && (p1.b < p2.b) && (p1.c < p2.c);
|
||||
}
|
||||
|
||||
static class P {
|
||||
|
||||
int a;
|
||||
int b;
|
||||
int c;
|
||||
|
||||
public P(int a, int b, int c) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
this.c = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
44
code/src/main/java/com/raorao/interview/netease/t1/Main.java
Normal file
44
code/src/main/java/com/raorao/interview/netease/t1/Main.java
Normal file
@ -0,0 +1,44 @@
|
||||
package com.raorao.interview.netease.t1;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-09-08-15:52
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int t = scanner.nextInt();
|
||||
List<Integer[]> list = new ArrayList<>();
|
||||
for (int i = 0; i < t; i++) {
|
||||
Integer[] ret = compute(scanner.nextInt(), scanner.nextInt());
|
||||
list.add(ret);
|
||||
}
|
||||
for (Integer[] i : list) {
|
||||
System.out.println(i[0] + " " + i[1]);
|
||||
}
|
||||
}
|
||||
|
||||
private static Integer[] compute(int n, int k) {
|
||||
Integer[] ret = new Integer[2];
|
||||
ret[0] = 0;
|
||||
ret[1] = 0;
|
||||
if (k <= 1) {
|
||||
return ret;
|
||||
}
|
||||
ret[0] = 0;
|
||||
if (k <= n / 2) {
|
||||
ret[1] = k - 1;
|
||||
} else {
|
||||
ret[1] = n - k;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
17
code/src/main/java/com/raorao/interview/netease/t2/Main.java
Normal file
17
code/src/main/java/com/raorao/interview/netease/t2/Main.java
Normal file
@ -0,0 +1,17 @@
|
||||
package com.raorao.interview.netease.t2;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-09-08-16:54
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
System.out.println(scanner.nextLine().length());
|
||||
}
|
||||
}
|
79
code/src/main/java/com/raorao/interview/netease/t3/Main.java
Normal file
79
code/src/main/java/com/raorao/interview/netease/t3/Main.java
Normal file
@ -0,0 +1,79 @@
|
||||
package com.raorao.interview.netease.t3;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-09-08-16:18
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
private static ArrayList<Vote> votes = new ArrayList<>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int n = scanner.nextInt();
|
||||
int m = scanner.nextInt();
|
||||
for (int i = 0; i < n; i++) {
|
||||
votes.add(new Vote(scanner.nextInt(), scanner.nextInt()));
|
||||
}
|
||||
|
||||
votes.sort(Comparator.comparingInt(e -> e.yy));
|
||||
int count = 0;
|
||||
for (int i = 0; i < votes.size(); i++) {
|
||||
if (check(m)) {
|
||||
break;
|
||||
}
|
||||
int x = votes.get(i).xx;
|
||||
int y = votes.get(i).yy;
|
||||
if (x != 1) {
|
||||
x = 1;
|
||||
votes.set(i, new Vote(x, y));
|
||||
count += y;
|
||||
}
|
||||
}
|
||||
System.out.println(count);
|
||||
|
||||
}
|
||||
|
||||
private static boolean check(int m) {
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
for (Vote v : votes) {
|
||||
map.put(v.xx, map.getOrDefault(v.xx, 0) + 1);
|
||||
}
|
||||
|
||||
int xxMax = Integer.MIN_VALUE;
|
||||
int yyMax = Integer.MIN_VALUE;
|
||||
for (Entry<Integer, Integer> entry : map.entrySet()) {
|
||||
if (entry.getValue() > yyMax) {
|
||||
xxMax = entry.getKey();
|
||||
yyMax = entry.getValue();
|
||||
}
|
||||
}
|
||||
if (map.entrySet().size() == m) {// 一人一票
|
||||
return false;
|
||||
}
|
||||
return xxMax == 1;
|
||||
}
|
||||
|
||||
static class Vote {
|
||||
|
||||
int xx;
|
||||
int yy;
|
||||
|
||||
public Vote(int xx, int yy) {
|
||||
this.xx = xx;
|
||||
this.yy = yy;
|
||||
}
|
||||
|
||||
public Vote() {
|
||||
}
|
||||
}
|
||||
}
|
47
code/src/main/java/com/raorao/sword/MinStack.java
Normal file
47
code/src/main/java/com/raorao/sword/MinStack.java
Normal file
@ -0,0 +1,47 @@
|
||||
package com.raorao.sword;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 包含Min方法的栈.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-09-08-23:26
|
||||
*/
|
||||
public class MinStack {
|
||||
|
||||
private List<Integer> data = new ArrayList<>();
|
||||
|
||||
private List<Integer> min = new ArrayList<>();
|
||||
|
||||
public void push(int node) {
|
||||
data.add(node);
|
||||
if (min.size() == 0) {
|
||||
min.add(0);
|
||||
} else {
|
||||
int m = min();
|
||||
if (node < m) {
|
||||
min.add(data.size() - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void pop() {
|
||||
int popIndex = data.size() - 1;
|
||||
int minIndex = min.get(min.size() - 1);
|
||||
if (popIndex == minIndex) {
|
||||
min.remove(min.size() - 1);
|
||||
}
|
||||
data.remove(data.size() - 1);
|
||||
}
|
||||
|
||||
public int top() {
|
||||
return data.get(data.size() - 1);
|
||||
}
|
||||
|
||||
public int min() {
|
||||
int minIndex = min.get(min.size() - 1);
|
||||
return data.get(minIndex);
|
||||
}
|
||||
}
|
31
code/src/main/java/com/raorao/sword/Solution07.java
Normal file
31
code/src/main/java/com/raorao/sword/Solution07.java
Normal file
@ -0,0 +1,31 @@
|
||||
package com.raorao.sword;
|
||||
|
||||
/**
|
||||
* 斐波那契数列.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-09-08-22:03
|
||||
*/
|
||||
public class Solution07 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int re = new Solution07().Fibonacci(5);
|
||||
System.out.println(re);
|
||||
}
|
||||
|
||||
public int Fibonacci(int n) {
|
||||
int pre = 1;
|
||||
int cur = 1;
|
||||
if (n == 0) {
|
||||
return 0;
|
||||
} else if (n <= 2) {
|
||||
return 1;
|
||||
}
|
||||
for (int i = 3; i <= n; i++) {
|
||||
int temp = pre + cur;
|
||||
pre = cur;
|
||||
cur = temp;
|
||||
}
|
||||
return cur;
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ package com.raorao.sword;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* .
|
||||
|
Loading…
x
Reference in New Issue
Block a user