-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
806.java
42 lines (41 loc) · 1.34 KB
/
806.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public int[] numberOfLines(int[] widths, String S) {
int len=S.length();
int curr_width=0;
int lines=1;
for(int i=0;i<len;i++){
if(curr_width + widths[(int)(S.charAt(i) - 'a')]>100){
lines++;
curr_width=widths[(int)(S.charAt(i) - 'a')];
}else{
curr_width+=widths[(int)(S.charAt(i) - 'a')];
}
}
int[] res=new int[2];
res[0]=lines;
res[1]=curr_width;
return res;
}
}
__________________________________________________________________________________________________
sample 36868 kb submission
class Solution {
public int[] numberOfLines(int[] widths, String S) {
if(S == null || S.length() == 0) {
return new int[2];
}
int count = 0;
int line = 1;
for(int i = 0; i < S.length(); i++) {
count += widths[S.charAt(i) - 'a'];
if(count > 100) {
line++;
count = widths[S.charAt(i) - 'a'];
}
}
return new int[]{line, count};
}
}
__________________________________________________________________________________________________