入门难度

反转字符串

写出一个程序,接受一个字符串,然后输出该字符串反转后的字符串。(字符串长度不超过1000)

示例一
输入:“abcd”
返回值:“dcba”

方法一:调用库函数
import java.util.*;
public class Solution {
    public String solve (String str) {
        StringBuffer sb =new StringBuffer(str);
        return sb.reverse().toString();
    }
}
方法二:原地交换
import java.util.*;
public class Solution {
    public String solve (String str) {
        char[] tmp = str.toCharArray();
        for(int i = 0; i < str.length()/2; i++){
            char t = tmp[i];
            tmp[i] = tmp[str.length() - i - 1];
            tmp[str.length() - i - 1] = t;
        }
        return new String(tmp);
    }
}
方法三:反转赋值
import java.util.*;
public class Solution {
    public String solve (String str) {
        char[] tmp = str.toCharArray();
        for(int i = 0; i < str.length(); i++ ){
            tmp[i] = str.charAt(str.length() - i - 1);
        }
        return new String(tmp);
    }
}

斐波那契数列

大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0,第1项是1)。
n ≤ 39

示例1
输入:4
返回值:3

方法一:递归
int Fibonacci(int n) {
    if (n==0 || n==1) return n;
    return Fibonacci(n-1) + Fibonacci(n-2);
}

方法二:递归优化,减少重复计算
int ans[] = new int[40];
        ans[0] = 0;
        ans[1] = 1;
        for(int i = 2; i <= n; i++)
            ans[i] = ans[i-1] + ans[i-2];
        return ans[n];
方法三:存储优化
 if(n == 0 || n == 1) return n;
        int a = 0, b = 1, c = 0;
        
        for(int i = 2; i <= n; i++){
            c = a + b;
            a = b;
            b = c;
        }
        
        return c;

螺旋矩阵

给定一个m x n大小的矩阵(m行,n列),按螺旋的顺序返回矩阵中的所有元素。

示例1
输入:[[1,2,3],[4,5,6],[7,8,9]]
返回值:[1,2,3,6,9,8,7,4,5]

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

旋转数组

一个数组A中存有N(N&gt0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0 A1 ……AN-1 )变换为(AN-M …… AN-1 A0 A1 ……AN-M-1 )(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?

示例1
输入:6,2,[1,2,3,4,5,6]
返回值:[5,6,1,2,3,4]
备注:
(1<=N<=100,M>=0)

三次反转
import java.util.*;


public class Solution {
    /**
     * 旋转数组
     * @param n int整型 数组长度
     * @param m int整型 右移距离
     * @param a int整型一维数组 给定数组
     * @return int整型一维数组
     */
    public int[] solve (int n, int m, int[] a) {
        // write code here
        int k = m % n;    //当m > n时,会出现右移位置超过一圈又循环回来了
        reverse(a, 0, n-1);
        reverse(a, 0, k-1);
        reverse(a, k, n-1);
        return a;
       
    }
    
    public void reverse(int[] a, int start, int end){
        while(start < end){
            int temp = a[start];
            a[start] = a[end];
            a[end] = temp;
            start++;
            end--;
        }
    }
}

判断回文

给定一个字符串,请编写一个函数判断该字符串是否回文。如果回文请返回true,否则返回false。

示例1
输入:“absba”
返回值:true

示例2
输入:“ranko”
返回值:false

示例3
输入:“yamatomaya”
返回值:false

示例4
输入:“a”
返回值:true

import java.util.*;


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

寻找峰值

山峰元素是指其值大于或等于左右相邻值的元素。给定一个输入数组nums,任意两个相邻元素值不相等,数组可能包含多个山峰。找到索引最大的那个山峰元素并返回其索引。

假设 nums[-1] = nums[n] = -∞。

示例1
输入:[2,4,1,2,7,8,4]
返回值:5

import java.util.*;


public class Solution {
    /**
     * 寻找最后的山峰
     * @param a int整型一维数组 
     * @return int整型
     */
    public int solve (int[] a) {
        // write code here
        
        if(a == null || a.length == 0)
            return -1;
        
         for(int i = a.length - 1; i >= 1; i--)   //找到一个索引最大的山峰元素
             if(a[i] >= a[i-1])                   //此时元素的索引已经最大,且该元素一定大于右边元素,(否则在上一步就return了)
                 return i;                        //即只要找到大于左边元素即可
        
        return 0;
    }
}

转圈打印矩阵

给定一个整型矩阵matrix,请按照顺时针转圈的方式打印它。

示例1
输入:[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
返回值:[1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10]

import java.util.*;


public class Solution {
    /**
     * 
     * @param matrix int整型二维数组 the matrix
     * @return int整型一维数组
     */
    public int[] printMatrix (int[][] matrix) {
        // write code here
       if(matrix.length == 0)
           return null;
        
        int left = 0;
        int right = matrix[0].length - 1;
        int top = 0;
        int bottom = matrix.length - 1;
        int[] a = new int[(right + 1) * (bottom + 1)];
        int x = 0;
        
        while(true){
            for(int i = left; i <= right; i++)
                a[x++] = matrix[top][i];
            if(++top > bottom)
                break;
            
            for(int i = top; i <= bottom; i++)
                a[x++] = matrix[i][right];
            if(left > --right)
                break;
            
            for(int i = right; i >= left; i--)
                a[x++] = matrix[bottom][i];
            if(top > --bottom)
                break;
            
            for(int i = bottom; i >= top; i--)
                a[x++] = matrix[i][left];
            if(++left > right)
                break;
        }
        
        return a;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值