Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Week_02/id_5/LeetCode_1_05.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.HashMap;
import java.util.Map;

public class LeetCode_1_05 {

/**
* 两数之和
* - 做这个题的时候,已经做了三数之和了。
* - 那我就不再使用枚举的方法了
* -
* @param nums
* @param target
* @return
*/
public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> book = new HashMap<>();
for (int i = 0;i<nums.length;i++){
int resultNum = target - nums[i];
if (book.containsKey(resultNum)){
return new int[]{book.get(resultNum),i};
}
book.put(nums[i],i);
}
return null;
}


}
30 changes: 30 additions & 0 deletions Week_02/id_5/LeetCode_3_05.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.util.HashSet;

public class LeetCode_3_05 {

/**
* 计算 字符串中不含有重复字符的最大子串的长度
* - 要点 滑动窗口 HashSet
*
* @param s
* @return
*/
public int lengthOfLongestSubstring(String s) {
int length = s.length();
HashSet<Character> window = new HashSet<>();
int i = 0, j = 0, maxLength = 0;

while (i < length && j < length) {
if (!window.contains(s.charAt(j))) {
window.add(s.charAt(j));
j++;
maxLength = Math.max(maxLength, j - i);
}else{
window.remove(s.charAt(i));
i++;
}
}
return maxLength;
}

}