Skip to content

Commit da4655d

Browse files
committed
Refactored 3 solutions
1 parent e145100 commit da4655d

File tree

3 files changed

+83
-125
lines changed

3 files changed

+83
-125
lines changed

Easy/Search Insert Position.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
class Solution {
2-
public int searchInsert(int[] nums, int target) {
3-
return binarySearch(nums, target);
4-
}
5-
6-
private int binarySearch(int[] nums, int target) {
7-
int left = 0;
8-
int right = nums.length - 1;
9-
10-
while (left <= right) {
11-
int mid = (left + right) / 2;
12-
if (nums[mid] == target) {
13-
return mid;
14-
}
15-
else if (nums[mid] > target) {
16-
right = mid - 1;
17-
}
18-
else {
19-
left = mid + 1;
20-
}
21-
}
22-
23-
return left;
2+
public int searchInsert(int[] nums, int target) {
3+
int low = 0;
4+
int high = nums.length - 1;
5+
while (low <= high) {
6+
int mid = (low + high) / 2;
7+
if (nums[mid] == target) {
8+
return mid;
9+
}
10+
else if (nums[mid] > target) {
11+
high = mid - 1;
12+
}
13+
else {
14+
low = mid + 1;
15+
}
2416
}
17+
return low;
18+
}
2519
}

Medium/Add and Search Word - Data structure design.java

Lines changed: 51 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,63 @@
11
class WordDictionary {
22

3-
/** Initialize your data structure here. */
4-
TrieNode root;
5-
public WordDictionary() {
6-
root = new TrieNode();
7-
}
8-
9-
/** Adds a word into the data structure. */
10-
public void addWord(String word) {
11-
addToTrie(word);
3+
/** Initialize your data structure here. */
4+
Node root;
5+
public WordDictionary() {
6+
root = new Node('-');
7+
}
8+
9+
/** Adds a word into the data structure. */
10+
public void addWord(String word) {
11+
Node curr = root;
12+
for (char c : word.toCharArray()) {
13+
if (curr.children[c - 'a'] == null) {
14+
curr.children[c - 'a'] = new Node(c);
15+
}
16+
curr = curr.children[c - 'a'];
1217
}
13-
14-
private void addToTrie(String word) {
15-
TrieNode curr = root;
16-
for (int i = 0; i < word.length(); i++) {
17-
char c = word.charAt(i);
18-
if (curr.children[c - 'a'] == null) {
19-
curr.children[c - 'a'] = new TrieNode();
20-
}
21-
22-
curr = curr.children[c - 'a'];
23-
24-
if (i == word.length() - 1) {
25-
curr.isWord = true;
26-
}
27-
}
18+
curr.isWord = true;
19+
}
20+
21+
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
22+
public boolean search(String word) {
23+
Node curr = root;
24+
return searchHelper(curr, word, 0);
25+
}
26+
27+
private boolean searchHelper(Node curr, String word, int idx) {
28+
if (idx == word.length()) {
29+
return curr.isWord;
2830
}
29-
30-
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
31-
public boolean search(String word) {
32-
TrieNode curr = root;
33-
boolean[] ans = {false};
34-
helper(word, 0, curr, ans);
35-
return ans[0];
31+
char c = word.charAt(idx);
32+
if (c != '.') {
33+
if (curr.children[c - 'a'] == null) {
34+
return false;
35+
}
36+
return searchHelper(curr.children[c - 'a'], word, idx + 1);
3637
}
37-
38-
private void helper(String word, int idx, TrieNode curr, boolean[] ans) {
39-
if (curr == null) {
40-
return;
41-
}
42-
43-
if (idx == word.length()) {
44-
if (curr.isWord) {
45-
ans[0] = true;
46-
}
47-
return;
48-
}
49-
50-
if (word.charAt(idx) != '.') {
51-
helper(word, idx + 1, curr.children[word.charAt(idx) - 'a'], ans);
52-
}
53-
else {
54-
for (int i = 0; i < 26; i++) {
55-
if (curr.children[i] != null) {
56-
helper(word, idx + 1, curr.children[i], ans);
57-
}
58-
}
38+
else {
39+
boolean flag = false;
40+
for (Node child : curr.children) {
41+
if (child != null) {
42+
flag = flag | searchHelper(child, word, idx + 1);
5943
}
44+
}
45+
return flag;
6046
}
47+
}
6148
}
6249

63-
class TrieNode {
64-
TrieNode[] children;
65-
boolean isWord;
66-
67-
public TrieNode() {
68-
children = new TrieNode[26];
69-
isWord = false;
70-
}
50+
51+
class Node {
52+
char c;
53+
Node[] children;
54+
boolean isWord;
55+
56+
public Node(char c) {
57+
this.c = c;
58+
children = new Node[26];
59+
isWord = false;
60+
}
7161
}
7262

7363
/**

Medium/Plus One Linked List.java

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,22 @@
77
* }
88
*/
99
class Solution {
10-
public ListNode plusOne(ListNode head) {
11-
ListNode rev = reverse(head);
12-
ListNode prev = null;
13-
int carry = 1;
14-
ListNode curr = rev;
15-
while (curr != null) {
16-
int temp = curr.val + carry;
17-
if (temp > 9) {
18-
carry = 1;
19-
temp = temp - 10;
20-
}
21-
else {
22-
carry = 0;
23-
}
24-
25-
prev = curr;
26-
27-
curr.val = temp;
28-
curr = curr.next;
29-
}
30-
31-
if (carry != 0) {
32-
prev.next = new ListNode(carry);
33-
}
34-
35-
ListNode ans = reverse(rev);
36-
37-
return ans;
10+
public ListNode plusOne(ListNode head) {
11+
ListNode dummy = new ListNode(0);
12+
dummy.next = head;
13+
ListNode notNine = dummy;
14+
while (head != null) {
15+
if (head.val != 9) {
16+
notNine = head;
17+
}
18+
head = head.next;
3819
}
39-
40-
private ListNode reverse(ListNode node) {
41-
ListNode curr = node;
42-
ListNode prev = null;
43-
ListNode next = null;
44-
45-
while (curr != null) {
46-
next = curr.next;
47-
curr.next = prev;
48-
prev = curr;
49-
curr = next;
50-
}
51-
52-
return prev;
20+
notNine.val++;
21+
notNine = notNine.next;
22+
while (notNine != null) {
23+
notNine.val = 0;
24+
notNine = notNine.next;
5325
}
26+
return dummy.val != 0 ? dummy : dummy.next;
27+
}
5428
}

0 commit comments

Comments
 (0)