2020-11-02

螺旋矩阵

import java.util.*;

public class Solution {
    public ArrayList<Integer> spiralOrder(int[][] matrix) {
        ArrayList<Integer> result=new ArrayList<>();
        if(matrix.length==0) return result;
        
        int n=matrix.length;
        int m=matrix[0].length;
        int left=0,right=m-1;
        int top=0,bottom=n-1;
        
        //一圈算一轮啊
        while(true){
            for(int i=left ;i<=right;i++) result.add(matrix[top][i]);
            top++;
            if (left>right||top>bottom) break;
            
            for(int i=top;i<=bottom;i++) result.add(matrix[i][right]);
            right--;
            if(left>right||top>bottom) break;
            
            for (int i=right;i>=left;i--)result.add(matrix[bottom][i]);
            bottom--;
            if(left>right||top>bottom) break;
            
            for(int i=bottom;i>=top;i--)result.add(matrix[i][left]);
            left++;
            if(left>right||top>bottom) break;
            
        }
        
        return result;
    }
}

加起来和为目标值的组合

标准的dfs+回溯模板了

import java.util.*;
public class Solution {
    ArrayList<ArrayList<Integer>> result=new ArrayList<ArrayList<Integer>>();
    public ArrayList<ArrayList<Integer>> combinationSum2(int[] num, int target) {
        if(num==null || num.length==0|| target<=0) return result;
        ArrayList<Integer> visited=new ArrayList<Integer>();
        Arrays.sort(num);
        dfs(num,0,num.length,target,visited);
        return result;
        
    }
    
    public void dfs(int[] num,int start,int end,int target,ArrayList<Integer> visited){
        if(target==0){
            ArrayList<Integer> list=new ArrayList<>(visited);
            result.add(list);
            return;
        }
        //从每一个点作为起点(循环),出发开始,做深度遍历
        for(int i=start;i<end;i++){
            if(i>start && num[i]==num[i-1]) continue;
            if(num[i]<=target){
                visited.add(num[i]);
                dfs(num,i+1,num.length,target-num[i],visited);
                visited.remove(visited.size()-1);
            }
            else return;
        }
    }
}

删除链表的倒数第n个节点

一开始没有写dummy=new listnode,就错了
为什么一定要在head前面加一个dummy节点呢?

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @param n int整型 
     * @return ListNode类
     */
    public ListNode removeNthFromEnd (ListNode head, int n) {
        // write code here
        ListNode dummy=new ListNode(0);
        dummy.next=head;
        ListNode fast=dummy;
        ListNode slow=dummy;
        if(head==null) return head;
        
        for (int i=0;i<=n;i++){
            fast=fast.next;
        }
        while (fast!=null){
            fast=fast.next;
            slow=slow.next;
        }
        slow.next=slow.next.next;
        return dummy.next;
    }
}

判断字符串是否为回文

前后同步往中间靠的双指针

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 
     * @param str string字符串 待判断的字符串
     * @return bool布尔型
     */
    public boolean judge (String str) {
        // write code here
        char[] arr=str.toCharArray();
        //前后双指针
        int i=0;
        int j=arr.length-1;
        while(i<j){
            if (arr[i]!=arr[j]) return false;
            i++;
            j--;
        }
        return true;
            
           
    }
}

= =

坚持啊!我!虽然周一和周日对我来说真的太累了…算了当做补偿多刷两道题吧,教程今天就不看了
要去投那个补录吗,我有点害怕留坏面评,明早面试完查查它还春招不…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值