Skip to content

Commit

Permalink
Some new solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Yifeng Li authored and Yifeng Li committed Oct 23, 2018
1 parent 8175548 commit 7f11e0d
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 7 deletions.
13 changes: 6 additions & 7 deletions Leet/Java/src/LeetSolutions.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ public static void main(String[] args) {
//MaximumSwap x = new MaximumSwap();
//x.maximumSwap(98368);

ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
head.next.next.next = new ListNode(4);
head.next.next.next.next = new ListNode(5);


ReorderList rl = new ReorderList();
MagicDictionary md = new MagicDictionary();

String[] dict = {"hello","leetcode"};
md.buildDict(dict);
boolean s = md.search("hhelo");

rl.reorderList(head);
}
}
78 changes: 78 additions & 0 deletions Leet/Java/src/MagicDictionary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import java.util.HashMap;
import java.util.ArrayList;

class MagicDictionary {

HashMap<String,ArrayList<String>> mdict;

/** Initialize your data structure here. */
public MagicDictionary() {
mdict = new HashMap<String,ArrayList<String>>();
}

/** Build a dictionary through a list of words */
public void buildDict(String[] dict) {

for (String str : dict) {
String[] strarray = buildStrArray(str);

for (String var : strarray) {

if(mdict.keySet().contains(var))
{
ArrayList<String> list = mdict.get(var);
list.add(str);
}
else
{
ArrayList<String> list = new ArrayList<String>();
list.add(str);
mdict.put(var,list);
}

}
}
}

/** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
public boolean search(String word) {

String[] strarray = buildStrArray(word);

for (String var : strarray) {
if(mdict.keySet().contains(var))
{

ArrayList<String> list = mdict.get(var);


if(list.size() > 1)
return true;

if(!list.get(0).equals(word))
return true;

}
}

return false;
}

public String[] buildStrArray(String str)
{
char[] array = str.toCharArray();

String[] r = new String[array.length];

for(int i = 0; i<array.length;i++)
{
char t = array[i];
array[i] = '?';
r[i] = new String(array);
array[i] = t;

}

return r;
}
}
46 changes: 46 additions & 0 deletions Leet/Java/src/OddEvenLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* OddEvenLinkedList
*/
public class OddEvenLinkedList {

public ListNode oddEvenList(ListNode head) {

if(head == null || head.next == null)
return head;

ListNode head2 = head.next;

ListNode evenListHead = head;
ListNode oddListHead = head.next;


while(evenListHead.next != null && oddListHead.next != null)
{
if(evenListHead.next.next != null)
{
ListNode Next = evenListHead.next.next;
evenListHead.next = Next;
evenListHead = Next;
}
else
{
evenListHead.next = null;
}

if(oddListHead.next.next != null)
{
ListNode Next = oddListHead.next.next;
oddListHead.next = Next;
oddListHead = Next;
}
else
{
oddListHead.next = null;
}
}

evenListHead.next = head2;

return head;
}
}

0 comments on commit 7f11e0d

Please sign in to comment.