京东
This commit is contained in:
parent
6ebf23bad1
commit
1c2b8c5ebb
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user