Hash
1.两数之和
思路:1.运用hashmap,key储存数组值,value储存数组下标
2.target-nums[i]如果不在hashmap中就储存进去
3.在的话,取出,res【0】=map.get(target-nums【i】),res【1】=i
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
HashMap<Integer,Integer> map = new HashMap<>();
for(int i = 0;i < nums.length;i++) {
int temp = target - nums[i];
if(map.containsKey(temp)) {
res[0] = map.get(temp);
res[1] = i;
return res;
}
map.put(nums[i],i);
}
return res;
}
}
387. 字符串中的第一个唯一字符
思路:字符串第一个唯一字符,运用hashmap的不能储存相同key的原理(后续进来的会覆盖),key储存字符,value储存boolean值
先进行第一次遍历判断哪些是重复字符
第二次遍历,取出第一个唯一字符,即value为true的key;
class Solution {
public int firstUniqChar(String s) {
char[] chars = s.toCharArray();
HashMap<Character,Boolean> map = new HashMap<>();
for(int i = 0;i < chars.length;i++) {
map.put(chars[i],!map.containsKey(chars[i]));
}
for(int j = 0;j < chars.length;j++) {
if(map.get(chars[j])) return j;
}
return -1;
}
}
链表
2. 两数相加
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode pre= new ListNode(0);
ListNode cur=pre;
int carry=0;
while(l1!=null||l2!=null){
int x=l1==null?0:l1.val;
int y=l2==null?0:l2.val;
int sum=x+y+carry;
carry=sum/10;
sum=sum%10;
cur.next=new ListNode(sum);
cur=cur.next;
if(l1!=null){
l1=l1.next;
}
if(l2!=null){
l2=l2.next;
}
}
if(carry==1){
cur.next=new ListNode(carry);
}
return pre.next;
}
}
19. 删除链表的倒数第N个节点
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode pre=new ListNode(0);
pre.next=head;
ListNode start=pre;
ListNode end =pre;
while(n!=0){
start=start.next;
n--;
}
while(start.next!=null){
start=start.next;
end=end.next;
}
end.next=end.next.next;
return pre.next;
}
}
61. 旋转链表
class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head==null)
return head;
ListNode fast=head;
ListNode slow=head;
int len=1;
while(fast.next!=null){
fast=fast.next;
len++;
}
fast.next=head;
int num =len-k%len-1;
while(num>=1){
slow=slow.next;
num--;
}
ListNode temp=slow.next;
slow.next=null;
return temp;
}
}
138. 复制带随机指针的链表
class Solution {
HashMap<Node,Node> map=new HashMap<>();
public Node copyRandomList(Node head) {
if(head==null)
return head;
if(map.containsKey(head))
return map.get(head);
Node node =new Node(head.val);
map.put(head,node);
node.next=copyRandomList(head.next);
node.random=copyRandomList(head.random);
return node;
}
}
206. 翻转链表
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre=null;
ListNode cur=head;
ListNode temp=null;
while(cur!=null){
temp=cur.next;
cur.next=pre;
pre=cur;
cur=temp;
}
return pre;
}
}
双指针遍历(滑动窗口)
3. 无重复字符的最长子串
class Solution {
public int lengthOfLongestSubstring(String s) {
int n=s.length(),ans=0;
HashMap<Character,Integer> map=new HashMap<>();
for(int start=0,end=0;end<n;end++){
char chars=s.charAt(end);
if(map.containsKey(chars)){
start=Math.max(map.get(chars),start);
}
map.put(s.charAt(end),end+1);
ans=Math.max(end-start+1,ans);
}
return ans;
}
}
11. 盛最多水的容器
class Solution {
public int maxArea(int[] height) {
int i=0,j=height.length-1,ans=0;
while(i<j){
ans= height[i]<height[j]?Math.max(ans,(j-i)*height[i++]):
Math.max(ans,(j-i)*height[j--]);
}
return ans;
}
}
15. 三数之和
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if(nums == null || nums.length < 3) return res;
Arrays.sort(nums);
for(int i = 0;i < nums.length;i++) {
if(nums[i] > 0) break;
if(i > 0 && nums[i] == nums[i - 1]) continue;
int left = i + 1;
int right = nums.length - 1;
while(left < right) {
int sum = nums[i] + nums[left] + nums[right];
if(sum == 0) {
res.add(Arrays.asList(nums[i],nums[left],nums[right]));
while(left < right && nums[left] == nums[left + 1]) left++;
while(left < right && nums[right] == nums[right - 1]) right--;
left++;
right--;
} else if(sum > 0) right--;
else left++;
}
}
return res;
}
}
16.最接近三数之和
class Solution {
public int threeSumClosest(int[] nums, int target) {
List<Integer> ans=new ArrayList();
int len=nums.length;
Arrays.sort(nums);
int res=nums[0]+nums[1]+nums[2];
for(int i=0;i<len;i++){
int L=i+1;
int R=len-1;
while(L<R){
int temp=nums[i]+nums[L]+nums[R];
if(Math.abs(target-temp)<Math.abs(target-res)){
res=temp;
}
if(temp>target){
R--;
}
else{
L++;
}
}
}
return res;
}
}
26. 删除排序数组中的重复项
class Solution {
public int removeDuplicates(int[] nums) {
if(nums==null||nums.length==0) return 0;
int L=0,R=1;
while(R<nums.length){
if(nums[L]==nums[R]){
R++;
}else{
nums[L+1]=nums[R];
L++;
R++;
}
}
return L+1;
}
}
121.股票买卖的最佳时机
class Solution {
public int maxProfit(int[] prices) {
int minPrice=Integer.MAX_VALUE;
int res=0;
for(int i=0;i<prices.length;i++){
if(prices[i]<minPrice){
minPrice=prices[i];
}else{
res=Math.max(res,prices[i]-minPrice);
}
}
return res;
}
}
209 长度最小的子数组
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int n=nums.length;
if(n==0){
return 0;
}
int L=0;
int R=0;
int res=Integer.MAX_VALUE;
int sum=0;
while(R<n){
sum+=nums[R];
while(sum>=target){
res=Math.min(res,R-L+1);
sum-=nums[L];
L++;
}
R++;
}
return res==Integer.MAX_VALUE?0:res;
}
}