三个入门算法题


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

import java.util.*;


public class Solution {
    /**
     * 反转字符串
     * @param str string字符串 
     * @return string字符串
     */
    public String solve (String str) {
        // write code here
        char[] ans = str.toCharArray();
        for(int i = 0;i<str.length();i++){
            ans[i] = str.charAt(str.length()-1-i);
            //从最后一个-i,表示从后往前走
        }
        return new String(ans);//字符串形式输出
    }
}

import java.util.*;


public class Solution {
    /**
     * 反转字符串
     * @param str string字符串 
     * @return string字符串
     */
    public String solve (String str) {
        // write code here
        StringBuilder sb = new StringBuilder();
        //从后往前遍历
        for(int i = str.length()-1;i>=0;i--){
            char c = str.charAt(i);
            sb.append(c);//拼接到StringBuilder中
        }
        return sb.toString();//输出
    }
}

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

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 
     * @param str string字符串 待判断的字符串
     * @return bool布尔型
     */
    public boolean judge (String str) {
        // write code here
        int i = 0;
        int j = str.length()-1;
        while(i<j){
            if(str.charAt(i) != str.charAt(j)){
                return false;
                //不相等返回false
            }
            //相等走到这,执行下面的语句,分别往前走
            i++;
            j--;
        }
        return true;
    }
}

import java.util.*;


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

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

import java.util.*;
public class Solution {
    public ArrayList<Integer> spiralOrder(int[][] matrix) {
        //123
        //456
        //789
        //输出123698745
        ArrayList<Integer> list = new ArrayList<>();
        if(matrix.length == 0){
            return list;
        }
        int top = 0;//上
        int bot = matrix.length-1;//下
        int left = 0;//左
        int right = matrix[0].length-1;//右
        //matrix.length表示的是行数
        //matrix[0].length表示的是列数
        while(top < (matrix.length+1)/2 && left <(matrix[0].length+1)/2){
            for(int i = left;i <= right;i++){
                list.add(matrix[top][i]);
            }//1->2->3
            for(int i = top +1;i <= bot;i++){//top+1表示向下一行
                list.add(matrix[i][right]);
            }//3->6->9
            for(int i = right -1;top != bot && i >= left;i--){//right -1表示往左移一列
                list.add(matrix[bot][i]);
            }//9->8->7
            for(int i = bot-1;left != right && i >= top+1;i--){//bot-1表示往上一行
                list.add(matrix[i][left]);
            }//7->4
            top++;
            bot--;
            left++;
            right--;
        }
        return list;
    }
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值