File tree 3 files changed +102
-0
lines changed
3 files changed +102
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int countBattleships (char [][] board ) {
3
+ int n = board .length ;
4
+ if (n == 0 ) return 0 ;
5
+ int m = board [0 ].length ;
6
+
7
+ int count = 0 ;
8
+ for (int i =0 ;i <n ;i ++) {
9
+ for (int j =0 ;j <m ;j ++) {
10
+ if (board [i ][j ] == '.' ) continue ;
11
+ if (i > 0 && board [i -1 ][j ] == 'X' )continue ;
12
+ if (j > 0 && board [i ][j -1 ] == 'X' )continue ;
13
+
14
+ count ++;
15
+ }
16
+ }
17
+
18
+ return count ;
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * public class TreeNode {
4
+ * int val;
5
+ * TreeNode left;
6
+ * TreeNode right;
7
+ * TreeNode(int x) { val = x; }
8
+ * }
9
+ */
10
+ class Solution {
11
+ int ans = 0 ;
12
+ int height = 0 ;
13
+
14
+ public int findBottomLeftValue (TreeNode root ) {
15
+ findBottomLeftValue (root , 1 );
16
+ return ans ;
17
+ }
18
+
19
+ public void findBottomLeftValue (TreeNode root , int depth ) {
20
+ if (height < depth ) {
21
+ ans = root .val ;
22
+ height = depth ;
23
+ }
24
+ if (root .left !=null ) findBottomLeftValue (root .left , depth +1 );
25
+ if (root .right !=null ) findBottomLeftValue (root .right , depth +1 );
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * public class TreeNode {
4
+ * int val;
5
+ * TreeNode left;
6
+ * TreeNode right;
7
+ * TreeNode(int x) { val = x; }
8
+ * }
9
+ */
10
+ class Solution {
11
+
12
+ public List <Integer > largestValues (TreeNode root ) {
13
+
14
+ List <Integer > ans = new ArrayList <>();
15
+
16
+ if (root == null ) return ans ;
17
+
18
+ ArrayList <TreeNode > arr = new ArrayList <>();
19
+ arr .add (root );
20
+
21
+ while (arr .size () != 0 ) {
22
+ ArrayList <TreeNode > roots = new ArrayList <>();
23
+ ArrayList <Integer > a = new ArrayList <>();
24
+
25
+ int i = 0 ;
26
+
27
+ while (i < arr .size ()) {
28
+ TreeNode p = arr .get (i );
29
+ if (p .left != null ) {
30
+ roots .add (p .left );
31
+ }
32
+ if (p .right != null ) {
33
+ roots .add (p .right );
34
+ }
35
+
36
+ a .add (p .val );
37
+
38
+ i ++;
39
+ }
40
+
41
+ Collections .sort (a );
42
+
43
+ ans .add (a .get (a .size ()-1 ));
44
+ arr .clear ();
45
+
46
+ i = 0 ;
47
+ while (i < roots .size ()) {
48
+ arr .add (roots .get (i ));
49
+ i ++;
50
+ }
51
+ }
52
+
53
+ return ans ;
54
+ }
55
+ }
You can’t perform that action at this time.
0 commit comments