春招100天——第五天11.15

 今日题目

1、最长递增子序列

方法:动态规划

import java.util.*;


public class Solution {
    /**
     * retrun the longest increasing subsequence
     * @param arr int整型一维数组 the array
     * @return int整型一维数组
     */
    public int[] LIS (int[] arr) {
        int[] dp = new int[arr.length];
        int[] subset = new int[arr.length + 1];
        int len = 0;
        for (int i = 0; i < arr.length; i++) {
            if (subset[len] < arr[i]) {
                len += 1;
                subset[len] = arr[i];
                dp[i] = len;
            } else {
                int idx = Arrays.binarySearch(subset, 0, len, arr[i]);
                if (idx < 0) {
                    idx = -(idx + 1);
                }
                subset[idx] = arr[i];
                dp[i] = idx;
            }
        }
        int[] res = new int[len];
        for (int i = arr.length - 1; i >= 0; i--) {
            if (dp[i] == len) {
                res[--len] = arr[i];
            }
        }
        return res;
    }
}

 2、数字字符串相加

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 计算两个数之和
     * @param s string字符串 表示第一个整数
     * @param t string字符串 表示第二个整数
     * @return string字符串
     */
    public String solve (String s, String t) {
        // write code here
       //类似链表相加
        StringBuffer sb=new StringBuffer();
        int i=s.length()-1;
        int j=t.length()-1;
        int carry=0;
        while(i>=0||j>=0){
            int x=i<0?0:s.charAt(i) - '0';
            int y=j<0?0:t.charAt(j) - '0';
            int sum=x+y+carry;
            carry=sum/10;
            sb.append(sum%10);
            i--;
            j--;
        }
        if(carry==1){
            sb.append(carry);
        }
        return sb.reverse().toString();
    }
}

3、 字符串反转

方法1:直接使用api

    public String solve (String str) {
        // write code here
        //直接使用api
        StringBuffer sb=new StringBuffer(str);
        return sb.reverse().toString();
    }

方法2:尾部遍历 

    public String solve (String str) {
        // write code here
        //通过从尾部遍历全部
        StringBuffer sb=new StringBuffer();
        for(int i=str.length()-1;i>=0;i--){
            sb.append(str.charAt(i));
        }
        return sb.toString();
    }

方法3:双指针首尾交换 

    public String solve (String str) {
        // write code here
        //双指针首尾交换
        char[] ch=str.toCharArray();
        int j=str.length()-1;
        for(int i=0;i<str.length()/2;i++){
            char c=ch[i];
            ch[i]=ch[j];
            ch[j]=c;
            j--;
        }
        return new String(ch);
    }

 4、顺时针打印数组

import java.util.*;
public class Solution {
    public ArrayList<Integer> spiralOrder(int[][] matrix) {
        ArrayList<Integer> res=new ArrayList<Integer>();
        if(matrix.length == 0){
             return res;
        }
        int m=matrix.length;
        int n=matrix[0].length;
        int left=0,right=n-1;
        int top=0,bottom=m-1;
        while(true){
            for(int i=left;i<=right;i++) res.add(matrix[top][i]);
            top++;
            if(top>bottom) break;
            for(int i=top;i<=bottom;i++) res.add(matrix[i][right]);
            right--;
            if(left>right) break;
            for(int i=right;i>=left;i--) res.add(matrix[bottom][i]);
            bottom--;
            if(top>bottom) break;
            for(int i=bottom;i>=top;i--) res.add(matrix[i][left]);
            left++;
            if(left>right) break;
        }
        return res;
    }
}

5、斐波那契数列 

public class Solution {
    public int Fibonacci(int n) {
        if(n<2){
            return n;
        }
        int[] dp=new int[n+1];
        dp[1]=1;
        dp[2]=1;
        for(int i=3;i<n+1;i++){
            dp[i]=dp[i-1]+dp[i-2];
        }
        return dp[n];
    }
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值