Leetcode#2
LinkedList
像一个火车,
linked list 需要一个一个的去找到相对应的值应为,在内存中数字并不是储存在一起的
linkedlist常用的方法
int size():linkedlist 有多少element
get(int index): 找到相对应index的值
Dummy node 在linked list 是做什么用的?
应为head 和tail 都是特殊的node 因为和其他node 不同,他们两一个没有 prev 一个没有next。
? : 的用法:
condition ? result1: result2; //如果condition是true的话,返回result1,如果是false返回result2.
Example 两数之和
operation
/
% mod
我一开始想到的方法是将link里面的数先变成integer 在相加,在变成link,但是非常的繁琐!最简单的方法是将每一个数相加,但要考虑进一位和多出一位的结果!
每一个位数的相加. 以及如果有多出一位也就是carry还有数的话
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode node1 = l1;
ListNode node2 = l2;
ListNode dummyHead = new ListNode(0);
ListNode current = dummyHead;
int carry =0;//write
while(node1 !=null || node2!=null||carry!=0){
int number1 = 0;
int number2 =0;
if(node1!=null){
number1 = node1.val;
} else{
number1=0;
}
if(node2!=null){
number2 = node2.val;
} else{
number2=0;
}
int addNumber = number1+number2+carry;
carry = addNumber/10;
current.next = new ListNode(addNumber%10);
current = current.next;
node1 =node1.next;
node2 = node2.next;
}
return dummyHead.next;
}
}
3 无重复字符的最长子串
String:
String 常用method
-Construction: String str2 = new String(“asdf”);
-public int length()//返回该字符串的长度;
-public char charAt(int index)//返回字符串中指定位置的字符;注意字符串中第一个字符索引是0,最后一个是length()-1。
-public String substring(int beginIndex, int endIndex)//该方法从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符作为一个新的字符串返回。
-public String concat(String str)//将参数中的字符串str连接到当前字符串的后面,效果等价于"+"。
解题思路:
首先人脑是怎么解这道题的,像bbbb这种人脑可以直接看出来!
那我们kaxia第一种abcabcbb, 我们将第一个开始看,到第二看看有没有重复,
1.发现应该从重复的那个字母之前的都删掉, 应为之前的无重复的数组大小不会大于从第一个数开始数!
class Solution {
public int lengthOfLongestSubstring(String s) {
int largest = 0;
//create a hashmap for search and compare.
Map<Character, Integer> map = new HashMap();
for(int i=0; i<s.length();i++){
if(!map.containsKey(s.charAt(i))){
map.put(s.charAt(i), i);
} else {
//all the things is to remove the repete value, and start from the
//应为在第一个重复的前面任何一个数都会遇到这次的重复,也就是说无重复的这个数组的size不会大于第一个开始的,也就是说后面的都没用了!
int size = map.size();
if(size>largest){
largest = size;
}
//find the position of the repete value
int deleteFromHere = map.get(s.charAt(i));
int j=0;
while(j<deleteFromHere){
map.remove(deleteFromHere);
j++;
}
map.put(s.charAt(i), i);
}
}
return largest;
}
}
优化的版本是通过 滑动窗口来的, 也就是说需要两个指针!
HashSet
public class Solution {
public int lengthOfLongestSubstring(String s) {
int n = s.length(), ans = 0;
Map<Character, Integer> map = new HashMap<>(); // current index of character
// try to extend the range [i, j]
for (int j = 0, i = 0; j < n; j++) {
if (map.containsKey(s.charAt(j))) {
i = Math.max(map.get(s.charAt(j)), i);
}
ans = Math.max(ans, j - i + 1);
map.put(s.charAt(j), j + 1);
}
return ans;
}
}
(作者:LeetCode链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/solution/wu-zhong-fu-zi-fu-de-zui-chang-zi-chuan-by-leetcod/
来源:力扣(LeetCode)著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。)
math.MAX
这里还用了hashSet做了:
什么是HashSet 为什么用它?
HashSet 有几个重要的要素:
- HashSet 储存unique value
- HashSet 没有办法像HashMap 一样将插入的element 的顺序进行排序
- HashSet在线程里是不安全的, 如果多个线程修改它,那么将不知道结果
HashSet Construction: Set daysOfWeek = new HashSet<>();