1 两数之和
### 解题思路
亮点1:利用map建立映射关系,储存映射关系,相比遍历,减少了大量的无用比较
亮点2:A+B=C 我通过判断C-B是否存在map中,这一步直接就判定了A B是否存在
### 代码
java
class Solution {
public int[] twoSum(int[] nums, int target) {
//Map<int,int> map = new HashMap(); 不能为基础类型,但可以是引用类型。所以不能为int,但可以是Integer
Map<Integer,Integer> map = new HashMap();
for(int i = 0;i < nums.length;i++) {
if(map.containsKey(target-nums[i])) {//亮点2 A+B=C 我通过判断C-B是否存在map中,这一步直接就判定了A B是否存在
return new int[]{i,map.get(target-nums[i])};
}
map.put(nums[i], i);// 亮点1:利用map建立映射关系,储存映射关系,相比遍历,减少了大量的无用比较 **
}
return new int[0];
}
}
2 给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
滑动窗口:
// "abcabcbb";
class Solution {
public int lengthOfLongestSubstring(String s) {
int result = 0;
int start = 0;
Map<Character,Integer> map = new HashMap<>();
char[] chas = s.toCharArray();
for(int i = 0; i < chas.length;i++) {
char var = chas[i];
//map.put(var, map.get(var) == null ? 0 : map.get(var) + 1);
map.put(var, map.get(var) == null ? 1 : map.get(var) + 1);
// if( map.get(i) > 1) {
// start++;
// }
while(map.get(var) > 1) {
map.put(chas[start], map.get(chas[start]) -1);
//关键点:到达"abcabcbb"中abcb重复段时,a,b全部由1变为0了,
此时ab不参与重复计算了
start++;
}
result = Math.max(i - start +1, result);
}
return result;
}
}
作者:andy-7
链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/solution/by-andy-7-9ykm/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
3 快速排序
class Solution {
public int[] sortArray(int[] nums) {
quickSort(nums,0,nums.length-1);
return nums;
}
public void quickSort (int[] nums, int low, int high) {
if (low < high) {
int index = partition(nums,low,high);
quickSort(nums,low,index-1);
quickSort(nums,index+1,high);
}
}
public int partition (int[] nums, int low, int high) {
int pivot = nums[low];
while (low < high) {
//移动high指针
while (low < high && nums[high] >= pivot) {
hight--;
}
//填坑
if (low < high) nums[low] = nums[high];
while (low < high && nums[low] <= pivot) {
low++;
}
//填坑
if (low < high) nums[high] = nums[low];
}
//基准数放到合适的位置
nums[low] = pivot;
return low;
}
}
作者:chefyuan
链接:https://leetcode-cn.com/problems/sort-an-array/solution/dong-hua-mo-ni-yi-ge-kuai-su-pai-xu-wo-x-7n7g/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
4 归并排序
5 回文数
package TIMU3;
public class tumu3 {
public static boolean isPalindrome(int x) {
if (x < 0 || (x % 10 == 0 && x != 0))
return false;
int rev = 0;
while (rev < x) {
rev = x % 10 + rev * 10;
x /= 10;
}
return (rev == x || rev/10 == x);
}
public static void main(String[] args) {
System.out.println(isPalindrome(1221));
}
}
6 两数相加
先累加
再进位
在考虑细节:链表一个长一个短,首位进位
为什么merge不能直接对head1操作,而要对l1操作???????
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head1 = l1;
ListNode head2 = l2;
while(head1 != null) {
if(head2 != null) {
// 第一步 把链表1 和链表2 直接相加
head1.val += head2.val;
head2 = head2.next;
}
//如果链表2比链表1长 把链表2多的部分直接赋给链表1即可 因为我们是以链表1为基础算的。
if(head1.next == null && head2 != null) {
head1.next = head2;
break;
}
head1 = head1.next;
}
merge(l1);
//遇坑!!!如果这样 merge(head1); 结果会错误!
return l1;
}
public void merge(ListNode head) {
while(head != null) {
if(head.val >= 10) {
//第二步 判断第一步相加结果 如果有大于10的需要进一位
head.val = head.val%10;
// 如果第一位(反转后的实际数组比如 781 这里指的是7不是1)需要进位 那么需要新建个位置
if(head.next == null) {
head.next = new ListNode(0);
}
head.next.val += 1;
}
head = head.next;
}
}
}
作者:andy-7
链接:https://leetcode-cn.com/problems/add-two-numbers/solution/by-andy-7-jiyp/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
7 只出现一次的数字
class Solution {
public int singleNumber(int[] nums) {
int single = 0;
for(int num : nums) {
single ^= num;
}
return single;
}
}
如果允许引入其他容器 也可以这么解题
使用集合存储数组中出现的所有数字,并计算数组中的元素之和。由于集合保证元素无重复,因此计算集合中的所有元素之和的两倍,即为每个元素出现两次的情况下的元素之和。由于数组中只有一个元素出现一次,其余元素都出现两次,因此用集合中的元素之和的两倍减去数组中的元素之和,剩下的数就是数组中只出现一次的数字。
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/single-number/solution/zhi-chu-xian-yi-ci-de-shu-zi-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。