Skip to content

Commit eb5ffd4

Browse files
committed
add leetcode solution # 30 #35 for java
1 parent a6cfe6c commit eb5ffd4

5 files changed

Lines changed: 225 additions & 4 deletions

File tree

Common/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Algorithm
2+
一些关于算法的总结
3+
让你从算法入门到放弃
4+
5+
![](./Document/src/main/jpg/BB527E2D-79C4-4303-9039-CE78973812AF.jpg)
6+
7+
8+
9+
## Leetcode专区
10+
LeetCode 网站题目解析 [详见](./LeetCode)
11+
12+
## AHaAlgorithms 啊哈算法
13+
书籍内部经典案例的再现,使用java实现
14+
15+
## NewCodes
16+
牛客网
17+
18+
## Pintia
19+
拼题A
20+
21+
22+
## Document
23+
算法文章类 [详见wiki](https://github.com/HbnKing/Algorithm/wiki) 已开放编辑
24+
25+
## 贡献
26+
如果你有更好代码或者是算法相关专题(当然我们也非常的重视文档),你可以fork 修改测试后 pull requset to me,如果你对这个玩意非常感兴趣,可以将你加入到
27+
collaborator.
28+
作为贡献者 你可以在这里留下你的名字.
29+
贡献请阅读([Contributing](./CONTRIBUTING.md))
30+
31+
## 其他
32+
后期将加入一些机器学习的东西

Common/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>algorithm</artifactId>
7+
<groupId>com.hbn</groupId>
8+
<version>1.0.0</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>common</artifactId>
13+
14+
15+
</project>

LeetCode/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,13 @@
311311
|39|[Combination Sum](https://oj.leetcode.com/problems/combination-sum/)| [Java](src/main/java/indi/ours/algorithm/leetcode/algorithms/_1.java/combinationSum/combinationSum)|Medium|
312312
|38|[Count and Say](https://oj.leetcode.com/problems/count-and-say/)| [Java](./src/main/java/indi/ours/algorithm/leetcode/algorithms/_38.java)|Easy| |
313313
|37|[Sudoku Solver](https://oj.leetcode.com/problems/sudoku-solver/)| [Java](src/main/java/indi/ours/algorithm/leetcode/algorithms/_1.java/sudokuSolver/sudokuSolver)|Hard|
314-
|36|[Valid Sudoku](https://oj.leetcode.com/problems/valid-sudoku/)| [Java](src/main/java/indi/ours/algorithm/leetcode/algorithms/_1.java/validSudoku/validSudoku)|Easy|
315-
|35|[Search Insert Position](https://oj.leetcode.com/problems/search-insert-position/)| [Java](src/main/java/indi/ours/algorithm/leetcode/algorithms/_1.java/searchInsertPosition/searchInsertPosition), [Java](./algorithms/java/src/searchInsertPosition/searchInsertPosition.java)|Medium|
314+
|36|[Valid Sudoku](https://oj.leetcode.com/problems/valid-sudoku/)| [Java](./src/main/java/indi/ours/algorithm/leetcode/algorithms/_1.java/validSudoku/validSudoku)|Easy|
315+
|35|[Search Insert Position](https://oj.leetcode.com/problems/search-insert-position/)| [Java](./src/main/java/indi/ours/algorithm/leetcode/algorithms/_35.java) |Easy| Array Binary Search| O(N) | | |
316316
|34|[Search for a Range](https://oj.leetcode.com/problems/search-for-a-range/)| [Java](src/main/java/indi/ours/algorithm/leetcode/algorithms/_1.java/searchForRange/searchForRange), [Java](./algorithms/java/src/searchForRange/searchForRange.java)|Medium|
317317
|33|[Search in Rotated Sorted Array](https://oj.leetcode.com/problems/search-in-rotated-sorted-array/)| [Java](src/main/java/indi/ours/algorithm/leetcode/algorithms/_1.java/searchInRotatedSortedArray/searchInRotatedSortedArray), [Java](./algorithms/java/src/searchInRotatedSortedArray/searchInRotatedSortedArray.java)|Hard|
318318
|32|[Longest Valid Parentheses](https://oj.leetcode.com/problems/longest-valid-parentheses/)| [Java](src/main/java/indi/ours/algorithm/leetcode/algorithms/_1.java/longestValidParentheses/longestValidParentheses)|Hard| String Dynamic Programming |
319319
|31|[Next Permutation](https://oj.leetcode.com/problems/next-permutation/)| [Java](src/main/java/indi/ours/algorithm/leetcode/algorithms/_1.java/nextPermutation/nextPermutation)|Medium|
320-
|30|[Substring with Concatenation of All Words](https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/)| [Java](src/main/java/indi/ours/algorithm/leetcode/algorithms/_1.java/substringWithConcatenationOfAllWords/substringWithConcatenationOfAllWords)|Hard| String|
320+
|30|[Substring with Concatenation of All Words](https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/)| [Java](./src/main/java/indi/ours/algorithm/leetcode/algorithms/_30.java)|Hard| String Hash Table Two Pointer| O(M*N)| | |
321321
|29|[Divide Two Integers](https://oj.leetcode.com/problems/divide-two-integers/)| [Java](src/main/java/indi/ours/algorithm/leetcode/algorithms/_1.java/divideTwoInt/divideTwoInt)|Medium|
322322
|28|[Implement strStr()](https://oj.leetcode.com/problems/implement-strstr/)| [Java](src/main/java/indi/ours/algorithm/leetcode/algorithms/_1.java/strStr/strStr), [Scala](./algorithms/java/src/strStr/strStr.java)|Easy|
323323
|27|[Remove Element](https://oj.leetcode.com/problems/remove-element/)| [Java](src/main/java/indi/ours/algorithm/leetcode/algorithms/_1.java/removeElement/removeElement)|Easy|
@@ -328,7 +328,7 @@
328328
|22|[Generate Parentheses](https://oj.leetcode.com/problems/generate-parentheses/)| [Java](src/main/java/indi/ours/algorithm/leetcode/algorithms/_1.java/generateParentheses/generateParentheses)|Medium|
329329
|21|[Merge Two Sorted Lists](https://oj.leetcode.com/problems/merge-two-sorted-lists/)| [Java](src/main/java/indi/ours/algorithm/leetcode/algorithms/_1.java/mergeTwoSortedList/mergeTwoSortedList)|Easy|
330330
|20|[Valid Parentheses](https://oj.leetcode.com/problems/valid-parentheses/)| [Java](./src/main/java/indi/ours/algorithm/leetcode/algorithms/_20.java)|Easy|String Stack|O(n) | |
331-
|19|[Remove Nth Node From End of List](https://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/)| [Java](./src/main/java/indi/ours/algorithm/leetcode/algorithms/_19.java)|Easy| Linked List Two Pointer| O(N) ||&gamma;|
331+
|19|[Remove Nth Node From End of List](https://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/)| [Java](./src/main/java/indi/ours/algorithm/leetcode/algorithms/_19.java)|Medium| Linked List Two Pointer| O(N) ||&gamma;|
332332
|18|[4Sum](https://oj.leetcode.com/problems/4sum/)| [Java](./src/main/java/indi/ours/algorithm/leetcode/algorithms/_18.java)|Medium|Array HashTable TwoPointer | O(N^4) || Q15|
333333
|17|[Letter Combinations of a Phone Number](https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/)| [Java](./src/main/java/indi/ours/algorithm/leetcode/algorithms/_17.java)|Medium|String BackTracking|O(n^3) | | queue|
334334
|16|[3Sum Closest](https://oj.leetcode.com/problems/3sum-closest/)| [Java](./src/main/java/indi/ours/algorithm/leetcode/algorithms/_16.java)|Medium|Array Two Point | O(n^2) | | Q15|
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package indi.ours.algorithm.leetcode.algorithms;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
/**
9+
* @author wangheng
10+
* @create 2018-11-11 下午2:30
11+
* @desc
12+
* You are given a string, s, and a list of words, words,
13+
* that are all of the same length.
14+
* Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.
15+
*
16+
* Example 1:
17+
*
18+
* Input:
19+
* s = "barfoothefoobarman",
20+
* words = ["foo","bar"]
21+
* Output: [0,9]
22+
* Explanation: Substrings starting at index 0 and 9 are "barfoor" and "foobar" respectively.
23+
* The output order does not matter, returning [9,0] is fine too.
24+
* Example 2:
25+
*
26+
* Input:
27+
* s = "wordgoodstudentgoodword",
28+
* words = ["word","student"]
29+
* Output: []
30+
*
31+
*
32+
**/
33+
public class _30 {
34+
/**
35+
*
36+
* @param s
37+
* @param words
38+
* @return
39+
*/
40+
public List<Integer> findSubstring(String s, String[] words) {
41+
int length = words[0].length();
42+
List<Integer> res = new ArrayList<>();
43+
if (s == null || words == null || words.length == 0 || s.length() < words[0].length() * words.length) return res;
44+
Map<String, Integer> map = new HashMap();
45+
Map<String, Integer> cur = new HashMap();
46+
//将 数据放入到 map 中
47+
for (String word: words) {
48+
if (!map.containsKey(word)) map.put(word, 0);
49+
map.put(word, map.get(word) + 1);
50+
}
51+
int m = words[0].length(), n = s.length(), num = words.length;
52+
for (int i = 0; i < m; i++) {
53+
int left = i, right = i, cnt = 0;
54+
cur = new HashMap();
55+
for (; right + m <= n; right += m) {
56+
String sub = s.substring(right, right + m);
57+
if (!map.containsKey(sub)){
58+
cur = new HashMap();
59+
cnt = 0;
60+
left = right + m;
61+
}
62+
else {
63+
if (!cur.containsKey(sub)) {
64+
cur.put(sub, 0);
65+
}
66+
cur.put(sub, cur.get(sub) + 1);
67+
cnt++;
68+
while (cur.get(sub) > map.get(sub)) {
69+
String leftSub = s.substring(left, left + m);
70+
cur.put(leftSub, cur.get(leftSub) - 1);
71+
cnt--;
72+
left += m;
73+
}
74+
if (cnt == num) res.add(left);
75+
}
76+
}
77+
}
78+
return res;
79+
}
80+
81+
82+
/**
83+
* I think the following code is self-explanatory enough.
84+
* We use an unordered_map<string, int> counts to record
85+
* the expected times of each word and another unordered_map<string, int> seen to record the times we have seen.
86+
* Then we check for every possible position of i.
87+
* Once we meet an unexpected word or the times of some word is larger than its expected times, we stop the check.
88+
* If we finish the check successfully, push i to the result indexes.
89+
*
90+
* Just build a map for the words and their relative count in L. Then we traverse through S to check whether there is a match.
91+
* @param s
92+
* @param words
93+
* @return
94+
*/
95+
public List<Integer> findSubstring2(String s, String[] words) {
96+
final Map<String, Integer> counts = new HashMap<>();
97+
for (final String word : words) {
98+
counts.put(word, counts.getOrDefault(word, 0) + 1);
99+
}
100+
final List<Integer> indexes = new ArrayList<>();
101+
final int n = s.length(), num = words.length, len = words[0].length();
102+
for (int i = 0; i < n - num * len + 1; i++) {
103+
final Map<String, Integer> seen = new HashMap<>();
104+
int j = 0;
105+
while (j < num) {
106+
final String word = s.substring(i + j * len, i + (j + 1) * len);
107+
if (counts.containsKey(word)) {
108+
seen.put(word, seen.getOrDefault(word, 0) + 1);
109+
if (seen.get(word) > counts.getOrDefault(word, 0)) {
110+
break;
111+
}
112+
} else {
113+
break;
114+
}
115+
j++;
116+
}
117+
if (j == num) {
118+
indexes.add(i);
119+
}
120+
}
121+
return indexes;
122+
}
123+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package indi.ours.algorithm.leetcode.algorithms;
2+
3+
/**
4+
* @author wangheng
5+
* @create 2018-11-11 下午11:31
6+
* @desc
7+
* Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
8+
*
9+
* You may assume no duplicates in the array.
10+
*
11+
* Example 1:
12+
*
13+
* Input: [1,3,5,6], 5
14+
* Output: 2
15+
* Example 2:
16+
*
17+
* Input: [1,3,5,6], 2
18+
* Output: 1
19+
* Example 3:
20+
*
21+
* Input: [1,3,5,6], 7
22+
* Output: 4
23+
* Example 4:
24+
*
25+
* Input: [1,3,5,6], 0
26+
* Output: 0
27+
*
28+
* 找到目标应该 插入的位置
29+
*
30+
**/
31+
public class _35 {
32+
33+
/**
34+
* O(N)
35+
* @param nums
36+
* @param target
37+
* @return
38+
*/
39+
public int searchInsert(int[] nums, int target) {
40+
41+
for(int i = 0 ;i<nums.length ;i++){
42+
if(nums[i] >=target){
43+
return i ;
44+
}
45+
}
46+
//如果便利完成后,还没有返回 ,说明数组 的内容都比 target 小
47+
//应该放在最后面
48+
49+
return nums.length ;
50+
}
51+
}

0 commit comments

Comments
 (0)