File tree Expand file tree Collapse file tree 5 files changed +151
-0
lines changed
best-time-to-buy-and-sell-stock
maximum-depth-of-binary-tree Expand file tree Collapse file tree 5 files changed +151
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int maxProfit (int [] prices ) {
3+ int N = prices .length ;
4+ int [] rangeMaxAfter = new int [N ];
5+ int maxValue = prices [N -1 ];
6+
7+ for (int i = N -1 ; i >= 0 ; i --){
8+ if (maxValue < prices [i ]){
9+ maxValue = prices [i ];
10+ }
11+ rangeMaxAfter [i ] = maxValue ;
12+ }
13+
14+ int answer = rangeMaxAfter [0 ] - prices [0 ];
15+
16+ for (int i = 0 ; i < N ; i ++){
17+ answer = Math .max (answer , rangeMaxAfter [i ] - prices [i ]);
18+ }
19+
20+ return answer ;
21+ }
22+ }
23+
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ /**
4+ * Definition for singly-linked list.
5+ * class ListNode {
6+ * int val;
7+ * ListNode next;
8+ * ListNode(int x) {
9+ * val = x;
10+ * next = null;
11+ * }
12+ * }
13+ */
14+ public class Solution {
15+ Set <ListNode > visit = new HashSet <>();
16+ public boolean hasCycle (ListNode head ) {
17+ return dfs (head , visit );
18+ }
19+ public boolean dfs (ListNode head , Set <ListNode > visit ){
20+ Deque <ListNode > stack = new ArrayDeque <>();
21+ if (head != null ){
22+ visit .add (head );
23+ stack .push (head );
24+ }
25+ while (!stack .isEmpty ()){
26+ ListNode cur = stack .pop ();
27+ if (cur .next == null ) continue ;
28+ if (visit .contains (cur .next )) {
29+ return true ;
30+ }
31+ visit .add (cur .next );
32+ stack .push (cur .next );
33+ }
34+ return false ;
35+ }
36+ }
37+
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ /**
4+ * Definition for a binary tree node.
5+ * public class TreeNode {
6+ * int val;
7+ * TreeNode left;
8+ * TreeNode right;
9+ * TreeNode() {}
10+ * TreeNode(int val) { this.val = val; }
11+ * TreeNode(int val, TreeNode left, TreeNode right) {
12+ * this.val = val;
13+ * this.left = left;
14+ * this.right = right;
15+ * }
16+ * }
17+ */
18+ class Solution {
19+ class Data {
20+ TreeNode node ;
21+ int depth ;
22+ public Data (TreeNode node , int depth ){
23+ this .node = node ;
24+ this .depth = depth ;
25+ }
26+ }
27+ public int maxDepth (TreeNode root ) {
28+ return bfs (root );
29+ }
30+ public int bfs (TreeNode root ) {
31+ int depth = 0 ;
32+ Queue <Data > q = new ArrayDeque <>();
33+ if (root != null ){
34+ q .add (new Data (root , 1 ));
35+ }
36+ while (!q .isEmpty ()){
37+ Data cur = q .poll ();
38+ if (cur .depth > depth ){
39+ depth = cur .depth ;
40+ }
41+ if (cur .node .left != null ) {
42+ q .add (new Data (cur .node .left , cur .depth + 1 ));
43+ }
44+ if (cur .node .right != null ) {
45+ q .add (new Data (cur .node .right , cur .depth + 1 ));
46+ }
47+ }
48+ return depth ;
49+ }
50+ }
51+
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int reverseBits (int n ) {
3+ Integer [] rev = new Integer [32 ];
4+ for (int i = 0 ; i < 32 ; i ++){
5+ rev [i ] = 0 ;
6+ }
7+ int ret = 0 ;
8+ int ptr = 0 ;
9+ int fac = 1 ;
10+ while (n != 0 ){
11+ rev [ptr ++] = n % 2 ;
12+ n = (int ) (n /2 );
13+ }
14+ for (int i = 0 ; i < 32 ; i ++){
15+ ret += rev [31 -i ] * fac ;
16+ fac *= 2 ;
17+ }
18+ return ret ;
19+ }
20+ }
21+
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public boolean isPalindrome (String s ) {
3+ String filterd = s .toLowerCase ();
4+ StringBuilder filterdBuilder = new StringBuilder ();
5+
6+ for (int i = 0 ; i < filterd .length (); i ++){
7+ Character c = filterd .charAt (i );
8+ if (Character .isLetterOrDigit (c )){
9+ filterdBuilder .append (c .toString ());
10+ }
11+ }
12+
13+ String ori = filterdBuilder .toString ();
14+ String rev = new StringBuilder (ori ).reverse ().toString ();
15+
16+ return rev .equals (ori );
17+ }
18+ }
19+
You can’t perform that action at this time.
0 commit comments