File tree 3 files changed +123
-0
lines changed
3 files changed +123
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * public class ListNode {
4
+ * int val;
5
+ * ListNode next;
6
+ * ListNode(int x) {
7
+ * val = x;
8
+ * next = null;
9
+ * }
10
+ * }
11
+ */
12
+ public class Solution {
13
+ public ListNode addTwoNumbers (ListNode l1 , ListNode l2 ) {
14
+ // Start typing your Java solution below
15
+ // DO NOT write main() function
16
+ int carry = 0 ;
17
+ ListNode dummy = new ListNode (0 );
18
+ while (l1 != null || l2 != null ){
19
+ int sum = carry + getVal (l1 ) + getVal (l2 );
20
+ carry = sum / 10 ;
21
+
22
+ ListNode current = new ListNode (sum % 10 );
23
+ current .next = dummy .next ;
24
+ dummy .next = current ;
25
+ l1 = getNext (l1 );
26
+ l2 = getNext (l2 );
27
+ }
28
+ if (carry != 0 ){
29
+ ListNode head = new ListNode (carry );
30
+ head .next = dummy .next ;
31
+ dummy .next = head ;
32
+ }
33
+ return reverse (dummy .next );
34
+ }
35
+
36
+ public int getVal (ListNode node ){
37
+ if (node == null ) return 0 ;
38
+ return node .val ;
39
+
40
+ }
41
+
42
+ public ListNode getNext (ListNode node ){
43
+ if (node == null ) return null ;
44
+ return node .next ;
45
+ }
46
+
47
+ public ListNode reverse (ListNode head ) {
48
+ ListNode current = head ;
49
+ ListNode newHead = null ;
50
+ while (current != null ) {
51
+ ListNode tmp = current ;
52
+ current = current .next ;
53
+ tmp .next = newHead ;
54
+ newHead = tmp ;
55
+ }
56
+ return newHead ;
57
+ }
58
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+
3
+ public class Solution {
4
+ public ArrayList <String > anagrams (String [] strs ) {
5
+ // Start typing your Java solution below
6
+ // DO NOT write main() function
7
+ ArrayList <String > result = new ArrayList <String >();
8
+ HashMap <String , ArrayList <Integer >> ruler = new HashMap <String , ArrayList <Integer >>();
9
+
10
+ for (int i = 0 ; i < strs .length ; i ++){
11
+ ArrayList <Integer > temp = new ArrayList <Integer >();
12
+ String hash = hashWord (strs [i ]);
13
+ if (ruler .containsKey (hash )){
14
+ temp = ruler .get (hash );
15
+ }
16
+ temp .add (i );
17
+ ruler .put (hash , temp );
18
+
19
+ }
20
+
21
+ Iterator it = ruler .entrySet ().iterator ();
22
+ while (it .hasNext ()) {
23
+ Map .Entry me = (Map .Entry )it .next ();
24
+ ArrayList <Integer > values = (ArrayList <Integer >) me .getValue ();
25
+ if (values .size () > 1 ){
26
+ for (int index = 0 ; index < values .size (); index ++){
27
+ result .add (strs [values .get (index )]);
28
+ }
29
+ }
30
+ }
31
+ return result ;
32
+ }
33
+
34
+ public String hashWord (String word ){
35
+ int [] letters = new int [26 ];
36
+ for (int i = 0 ; i < 26 ; i ++) {
37
+ letters [i ] = 0 ;
38
+ }
39
+ for (int i = 0 ; i < word .length (); i ++) {
40
+ int index = (int ) word .charAt (i ) - 97 ;
41
+ letters [index ] ++;
42
+ }
43
+ String result = "" ;
44
+ for (int i : letters ){
45
+ result += i + "/" ;
46
+ }
47
+ return result ;
48
+ }
49
+ }
Original file line number Diff line number Diff line change
1
+ public class Solution {
2
+ public ArrayList <Integer > inorderTraversal (TreeNode root ) {
3
+ // Start typing your Java solution below
4
+ // DO NOT write main() function
5
+ ArrayList <Integer > result = new ArrayList <Integer >();
6
+ inOrder (root , result );
7
+ return result ;
8
+ }
9
+
10
+ public void inOrder (TreeNode root , ArrayList <Integer > result ){
11
+ if (root == null ) return ;
12
+ inOrder (root .left , result );
13
+ result .add (root .val );
14
+ inOrder (root .right , result );
15
+ }
16
+ }
You can’t perform that action at this time.
0 commit comments