剑指Offer 10-38

class Solution {
    public int fib(int n) {
        final int MOD=1000000007;
        if(n<2) return n;
        int p=0,q=0,r=1;
        for(int i=2;i<=n;i++)
        {
            p=q;
            q=r;
            r=(p+q)%MOD;
        }
        return r;

    }
}

class Solution {
    public int minArray(int[] numbers) {
        if ( numbers.length == 1) return numbers[0];

        for ( int i = 1; i < numbers.length; i++){
            if ( numbers[i] < numbers[i - 1]){
                return numbers[i];
            }
        }
        // 没有找到这个转折点, 则数组为升序
        return numbers[0];

//        Arrays.sort(numbers);
//        return numbers[0];

    }
}

剑指 Offer 12. 矩阵中的路径

难度中等649收藏分享切换为英文接收动态反馈

给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false 。

单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。

例如,在下面的 3×4 的矩阵中包含单词 "ABCCED"(单词中的字母已标出)。

示例 1:

输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
输出:true

示例 2:

输入:board = [["a","b"],["c","d"]], word = "abcd"
输出:false

提示:

  • m == board.length
  • n = board[i].length
  • 1 <= m, n <= 6
  • 1 <= word.length <= 15
  • board  word 仅由大小写英文字母组成
class Solution {
    static int [][] move=new int[][]{{-1,0},{0,1},{1,0},{0,-1}};

    public static boolean dfs(char[][] board,String word,int u,int x,int y)
    {
        if(board[x][y]!=word.charAt(u)){
            return false;
        }
        if(u==word.length()-1)
            return true;
        char t=board[x][y];
        board[x][y]='.';

        for (int i=0;i<4;i++)
        {
            int tx=x+move[i][0];
            int ty=y+move[i][1];
            if(tx<0||tx>=board.length||ty<0||ty>=board[0].length)
                continue;
            if(board[tx][ty]=='.')
                continue;
            if(dfs(board,word,u+1,tx,ty))
            {
                return true;
            }
        }
        board[x][y]=t;
        return false;
    }
    public static boolean exist(char[][] board, String word) {
        for(int i=0;i<board.length;i++)
        {
            for(int j=0;j<board[i].length;j++)
            {
                if(dfs(board,word,0,i,j))
                {
                    return true;
                }
            }
        }

        return false;

    }

}

剑指 Offer 17. 打印从1到最大的n位数

输入数字 n,按顺序打印出从 1 到最大的 n 位十进制数。比如输入 3,则打印出 1、2、3 一直到最大的 3 位数 999。

示例 1:

输入: n = 1
输出: [1,2,3,4,5,6,7,8,9]

说明:

  • 用返回一个整数列表来代替打印
  • n 为正整数
class Solution {
    public int[] printNumbers(int n) {
        int end = (int)Math.pow(10, n) - 1;
        int[] res = new int[end];
        for(int i = 0; i < end; i++)
            res[i] = i + 1;
        return res;
    }
}

剑指 Offer 21. 调整数组顺序使奇数位于偶数前面

难度简单249收藏分享切换为英文接收动态反馈

输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数在数组的前半部分,所有偶数在数组的后半部分。

示例:

输入:nums = [1,2,3,4]
输出:[1,3,2,4] 
注:[3,1,2,4] 也是正确的答案之一。

提示:

  1. 0 <= nums.length <= 50000
  2. 0 <= nums[i] <= 10000
class Solution {
//    剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
    public static int[] exchange(int[] nums) {
        List<Integer> list1=new ArrayList<Integer>();
        List<Integer> list2=new ArrayList<Integer>();

        for(int i=0;i<nums.length;i++)
        {
            if(nums[i]%2==0)
            {
                list2.add(nums[i]);
            }
            else {
                list1.add(nums[i]);
            }
        }
        list1.addAll(list2);
        int a[]=new int[list1.size()];
        for (int i=0;i<a.length;i++) {
            System.out.println(list1.get(i));
            a[i]=list1.get(i);

        }
        Integer[] A=list1.toArray(new Integer[list1.size()]);
        System.out.println(A[0].intValue());

        ArrayList<Integer> b=new ArrayList<Integer>(Arrays.asList(A));
        System.out.println(b);

        int g=4;
        Integer k=(Integer) g;
        return a;


    }
}

剑指 Offer 29. 顺时针打印矩阵

难度简单437收藏分享切换为英文接收动态反馈

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。

示例 1:

输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,3,6,9,8,7,4,5]

示例 2:

输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
输出:[1,2,3,4,8,12,11,10,9,5,6,7]

限制:

  • 0 <= matrix.length <= 100
  • 0 <= matrix[i].length <= 100
class Solution {
    public int[] spiralOrder(int[][] matrix) {
                if(matrix.length==0) return new int[0];
        int l=0,r=matrix[0].length-1,t=0,b=matrix.length-1,x=0;
        int[] res=new int[matrix.length*matrix[0].length];
        while (true){
            for (int i=l;i<=r;i++)
            {
                res[x++]=matrix[t][i];//left to right
            }
            if(++t >b) break;
            for (int i=t;i<=b;i++) res[x++]=matrix[i][r]; //top to bottom
            if(--r <l) break;
            for(int i=r;i>=l;i--) res[x++]=matrix[b][i]; //right to left
            if(--b <t) break;
            for(int i=b;i>=t;i--) res[x++]=matrix[i][l]; //bottom to top
            if(++l >r) break;
        }
        
        for (int i=0;i<res.length;i++)
        {
            System.out.println(res[i]);
        }
        return res;

    }
}

剑指 Offer 31. 栈的压入、弹出序列

难度中等363收藏分享切换为英文接收动态反馈

输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如,序列 {1,2,3,4,5} 是某栈的压栈序列,序列 {4,5,3,2,1} 是该压栈序列对应的一个弹出序列,但 {4,3,5,1,2} 就不可能是该压栈序列的弹出序列。

示例 1:

输入:pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
输出:true
解释:我们可以按以下顺序执行:
push(1), push(2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1

示例 2:

输入:pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
输出:false
解释:1 不能在 2 之前弹出。

提示:

  1. 0 <= pushed.length == popped.length <= 1000
  2. 0 <= pushed[i], popped[i] < 1000
  3. pushed 是 popped 的排列。
class Solution {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
                Stack<Integer> stack=new Stack<Integer>();
        int i=0;
        for (int num:pushed) {
            stack.push(num);
            while (!stack.empty() && stack.peek()==popped[i])
            {
                stack.pop();
                i++;
            }

        }
        return stack.isEmpty();

    }
}

剑指 Offer 38. 字符串的排列

难度中等588收藏分享切换为英文接收动态反馈

输入一个字符串,打印出该字符串中字符的所有排列。

你可以以任意顺序返回这个字符串数组,但里面不能有重复元素。

示例:

输入:s = "abc"
输出:["abc","acb","bac","bca","cab","cba"]

限制:

1 <= s 的长度 <= 8

class Solution {
    
     HashSet<String> res = new HashSet<>();
     
     LinkedHashSet<Integer> set = new LinkedHashSet<>();

    public  String[] permutation(String s) {

        char[] c = s.toCharArray();
        char[] newchar = new char[c.length];

        dfs(0,c,newchar);
        return res.toArray(new String[res.size()]);

    }
    public  void dfs(int x,char[] c,char[] newchar )
    {
        if( x==c.length)
        {
            res.add(String.valueOf(newchar));
            
            return;
        }

        for(int i=0;i<c.length;i++)
        {
            if(set.contains(i))
                continue;
            else {
                set.add(i);
                newchar[x]=c[i];
                dfs(x+1,c,newchar);
                set.remove(set.toArray()[set.size()-1]);
            }

        }

        return;
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值