Skip to content

Commit f502f36

Browse files
committed
1004
1 parent d1de52f commit f502f36

8 files changed

Lines changed: 193 additions & 1 deletion

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ Feel free to add issues, comment and pull request.
4949
| Leetcode | [598. Range Addition II](https://leetcode.com/problems/range-addition-ii/description/) | [Java](./java/maxCount.java) \| [Python](./Python/) | _O(n)_ | _O(1)_ | Easy | |
5050
| Leetcode | [674. Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/description/) | [Java](./java/findLengthOfLCIS.java) \| [Python](./Python/) | _O(n)_ | _O(1)_ | Easy | |
5151
| Leetcode | [383. Ransom Note](https://leetcode.com/problems/ransom-note/description/) | [Java](./java/canConstruct.java) \| [Python](./Python/) | _O(n)_ | _O(1)_ | Easy | |
52-
52+
| Leetcode | [350. Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/description/) | [Java](./java/intersect.java) \| [Python](./Python/) | _O(m+n)_ | _O(1)_ | Easy | |
53+
| Leetcode | [346. Moving Average from Data Stream](https://leetcode.com/problems/moving-average-from-data-stream/description/) | [Java](./java/MovingAverage.java) \| [Python](./Python/) | _O(n)_ | _O(1)_ | Easy | |
5354

5455

5556
## Strings
@@ -68,6 +69,11 @@ Feel free to add issues, comment and pull request.
6869
| Leetcode | [541. Reverse String II](https://leetcode.com/problems/reverse-string-ii/description/) | [Java](./java/reverseStr.java) \| [Python](./Python/) | _O(n)_ | _O(1)_ | Easy | |
6970
| Leetcode | [205. Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/description/) | [Java](./java/isIsomorphic.java) \| [Python](./Python/) | _O(n)_ | _O(1)_ | Easy | |
7071
| Leetcode | [520. Detect Capital](https://leetcode.com/problems/detect-capital/description/) | [Java](./java/detectCapitalUse.java) \| [Python](./Python/) | _O(n)_ | _O(1)_ | Easy | |
72+
| Leetcode | [408. Valid Word Abbreviation](https://leetcode.com/problems/valid-word-abbreviation/description/) | [Java](./java/validWordAbbreviation.java) \| [Python](./Python/) | _O(n)_ | _O(1)_ | Easy | |
73+
| Leetcode | [243. Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance/description/) | [Java](./java/shortestDistance.java) \| [Python](./Python/) | _O(n)_ | _O(1)_ | Easy | |
74+
| Leetcode | [389. Find the Difference](https://leetcode.com/problems/find-the-difference/description/) | [Java](./java/findTheDifference.java) \| [Python](./Python/) | _O(n)_ | _O(1)_ | Easy | |
75+
| Leetcode | [345. Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/description/) | [Java](./java/reverseVowels.java) \| [Python](./Python/) | _O(n)_ | _O(1)_ | Easy | |
76+
| Leetcode | [290. Word Pattern](https://leetcode.com/problems/word-pattern/description/) | [Java](./java/wordPattern.java) \| [Python](./Python/) | _O(n)_ | _O(1)_ | Easy | |
7177

7278
## HashMap
7379
| Website | Title | Solution | Time | Space | Difficulty | Note|

java/MovingAverage.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class MovingAverage {
2+
3+
Queue<Integer> queue;
4+
double sum = 0;int size;
5+
6+
/** Initialize your data structure here. */
7+
public MovingAverage(int s) {
8+
size = s;
9+
queue = new LinkedList<>();
10+
}
11+
12+
public double next(int val) {
13+
if(queue.size() == size)
14+
sum = sum - queue.poll();
15+
16+
queue.offer(val);
17+
sum += val;
18+
return sum/queue.size();
19+
20+
}
21+
}
22+
23+
/**
24+
* Your MovingAverage object will be instantiated and called as such:
25+
* MovingAverage obj = new MovingAverage(size);
26+
* double param_1 = obj.next(val);
27+
*/

java/findTheDifference.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public char findTheDifference(String s, String t) {
3+
char result = 0;
4+
for (char x : s.toCharArray()) result ^= x;
5+
for (char x : t.toCharArray()) result ^= x;
6+
return result;
7+
}
8+
}

java/intersect.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class Solution {
2+
public int[] intersect(int[] nums1, int[] nums2) {
3+
Arrays.sort(nums1);
4+
Arrays.sort(nums2);
5+
int p1 = 0;
6+
int p2 = 0;
7+
List <Integer> s = new ArrayList<>();
8+
while(p1 < nums1.length && p2 < nums2.length){
9+
if (nums1[p1] == nums2[p2]){
10+
s.add(nums1[p1]);
11+
p1 ++;
12+
p2 ++;
13+
}
14+
15+
16+
else if (nums1[p1] < nums2[p2])
17+
p1 ++;
18+
else
19+
p2 ++;
20+
}
21+
int [] r = new int[s.size()];
22+
for (int i =0; i < s.size(); i++){
23+
r[i] = s.get(i);
24+
}
25+
return r;
26+
}
27+
}
28+
29+
public class Solution {
30+
public int[] intersect(int[] nums1, int[] nums2) {
31+
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
32+
ArrayList<Integer> result = new ArrayList<Integer>();
33+
for(int i = 0; i < nums1.length; i++)
34+
{
35+
if(map.containsKey(nums1[i])) map.put(nums1[i], map.get(nums1[i])+1);
36+
else map.put(nums1[i], 1);
37+
}
38+
39+
for(int i = 0; i < nums2.length; i++)
40+
{
41+
if(map.containsKey(nums2[i]) && map.get(nums2[i]) > 0)
42+
{
43+
result.add(nums2[i]);
44+
map.put(nums2[i], map.get(nums2[i])-1);
45+
}
46+
}
47+
48+
int[] r = new int[result.size()];
49+
for(int i = 0; i < result.size(); i++)
50+
{
51+
r[i] = result.get(i);
52+
}
53+
54+
return r;
55+
}
56+
}

java/reverseVowels.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public String reverseVowels(String s) {
3+
if(s == null) return null;
4+
String vowels = "aeiouAEIOU";
5+
char[] word = s.toCharArray();
6+
int start = 0, end = word.length - 1;
7+
8+
while(start < end)
9+
{
10+
while(start < end && !vowels.contains(word[start]+""))
11+
start++;
12+
13+
while(start<end && !vowels.contains(word[end]+""))
14+
end--;
15+
16+
char temp = word[start];
17+
word[start] = word[end];
18+
word[end] = temp;
19+
20+
start++;
21+
end--;
22+
}
23+
return new String(word);
24+
}
25+
}

java/shortestDistance.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int shortestDistance(String[] words, String word1, String word2) {
3+
int ret = Integer.MAX_VALUE, index1 = -1, index2 = -1;
4+
for(int i = 0; i < words.length; i++) {
5+
if(words[i].equals(word1)) {
6+
index1 = i;
7+
if(index2 >= 0) ret = Math.min(ret, i - index2);
8+
} else if(words[i].equals(word2)) {
9+
index2 = i;
10+
if(index1 >= 0) ret = Math.min(ret, i - index1);
11+
}
12+
}
13+
return ret;
14+
}
15+
}

java/validWordAbbreviation.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public boolean validWordAbbreviation(String word, String abbr) {
3+
int i = 0, j = 0;
4+
5+
while(i < word.length() && j < abbr.length())
6+
{
7+
if(word.charAt(i) == abbr.charAt(j))
8+
{
9+
++i;
10+
++j;
11+
continue;
12+
}
13+
14+
if (abbr.charAt(j) <= '0' || abbr.charAt(j) > '9') {
15+
return false;
16+
}
17+
18+
int start = j;
19+
while(j < abbr.length() && abbr.charAt(j) >= '0' && abbr.charAt(j) <= '9')
20+
++j;
21+
22+
int num = Integer.valueOf(abbr.substring(start,j));
23+
i += num;
24+
}
25+
return i == word.length() && j == abbr.length();
26+
}
27+
}

java/wordPattern.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public boolean wordPattern(String pattern, String str) {
3+
4+
String [] words = str.split(" ");
5+
6+
if(words.length != pattern.length())
7+
return false;
8+
9+
HashMap<Character, String> map = new HashMap<>();
10+
11+
for(int i=0; i<words.length; i++)
12+
{
13+
char c = pattern.charAt(i);
14+
if(map.containsKey(c))
15+
{
16+
if(!map.get(c).equals(words[i]))
17+
return false;
18+
}
19+
else
20+
{
21+
if(map.containsValue(words[i]))
22+
return false;
23+
map.put(c, words[i]);
24+
}
25+
}
26+
return true;
27+
}
28+
}

0 commit comments

Comments
 (0)