Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit631442d

Browse files
author
zhenyu zhang
committed
update MS oa solution
1 parentcf2929c commit631442d

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

‎src/com/hadley/_161.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
packagecom.hadley;
2+
3+
/*
4+
5+
*/
6+
7+
publicclass_161 {
8+
}

‎src/com/hadley/oa/CountBitSets.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
packagecom.hadley.oa;
2+
3+
importjava.util.HashMap;
4+
importjava.util.Scanner;
5+
6+
publicclassCountBitSets {
7+
8+
//input: 18
9+
//output: 2
10+
//explanation: 18 = 10010
11+
12+
publicstaticvoidmain(String[]args) {
13+
Scannerscanner =newScanner(System.in);
14+
intcount =scanner.nextInt();
15+
int[]numberList =newint[count];
16+
17+
for(inti =0;i <count;i++){
18+
numberList[i] =scanner.nextInt();
19+
}
20+
21+
intresult =Integer.MIN_VALUE;
22+
intmin =Integer.MAX_VALUE;
23+
24+
for(inti =0 ;i <numberList.length;i++){
25+
result =Math.max(result,numberList[i] -min);
26+
if(numberList[i] <min){
27+
min =numberList[i];
28+
}
29+
}
30+
31+
System.out.println(result);
32+
System.out.println(result >0 ?1:2);
33+
}
34+
}

‎src/com/hadley/oa/MS.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
packagecom.hadley.oa;
2+
3+
publicclassMS {
4+
publicintsolution1(int[]A) {
5+
// write your code in Java SE 8
6+
if(A ==null ||A.length ==0)return -1;
7+
if(A.length ==1)returnA[0] >=0 ?A[0] : -1;
8+
9+
intmaxRes = -1;
10+
inttemp =0;
11+
12+
for(inti =0;i <A.length;i++){
13+
if(A[i] <0){
14+
temp =0;
15+
continue;
16+
}else{
17+
temp =temp +A[i];
18+
maxRes =Math.max(temp,maxRes);
19+
}
20+
}
21+
22+
returnmaxRes;
23+
}
24+
25+
publicintsolution2(int[]A,intK) {
26+
// N >= 3, K < N
27+
intminRes =Integer.MAX_VALUE;
28+
29+
intleft =0;
30+
while(left <=A.length -K){
31+
32+
intmax =Integer.MIN_VALUE;
33+
intmin =Integer.MAX_VALUE;
34+
for(inti =0;i <A.length;i++){
35+
if(!(i >=left &&i <=left +K -1)){
36+
max =Math.max(max,A[i]);
37+
min =Math.min(min,A[i]);
38+
}
39+
}
40+
//minRes
41+
minRes =Math.min(minRes,max -min);
42+
43+
left++;
44+
}
45+
46+
returnminRes;
47+
}
48+
49+
publicintsolution3(int[]A,intM) {
50+
// write your code in Java SE 8
51+
int[]remainder =newint[M];
52+
for(inti =0;i <A.length;i++){
53+
inttemp =A[i] %M;
54+
intmod =temp >=0 ?temp :temp +M;
55+
remainder[mod] ++;
56+
}
57+
58+
intmax =remainder[0];
59+
for(inti =0;i <remainder.length;i++){
60+
max =Math.max(max,remainder[i]);
61+
}
62+
63+
returnmax;
64+
}
65+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp