网站首页 > 基础教程 正文
package org.learn.str;
/**
* 将字符数组进行翻转
*
* @author holly
* @link <a href="https://leetcode.cn/problems/reverse-string/">翻转字符串</a>
* <p>
* 编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。
*
* 不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。
*
* 你可以假设数组中的所有字符都是 ASCII 码表中的可打印字符。
*
* 示例 1: 输入:["h","e","l","l","o"] 输出:["o","l","l","e","h"]
*
* 示例 2: 输入:["H","a","n","n","a","h"] 输出:["h","a","n","n","a","H"]
*
* #
* </p>
*/
public class ReverseString {
public static void main(String[] args) {
String str = "abc";
char[] chars = str.toCharArray();
reverseString(chars);
System.out.println(chars);
}
/**
* 翻转字符串
*
* @param chars char数组
*/
public static void reverseString(char[] chars) {
int l = 0;
int r = chars.length - 1;
while (l < r) {
char temp = chars[l];
chars[l] = chars[r];
chars[r] = temp;
l++;
r--;
}
}
}
猜你喜欢
- 2024-11-18 三石说:java基础之 基本数类型
- 2024-11-18 2023全国计算机一级考试历年真题节选及答案解析(一)
- 2024-11-18 三菱 PLC的串口通讯案例|RS232
- 2024-11-18 巧用输入法 办公也提效
- 2024-11-18 python数据类型(一):字符串
- 2024-11-18 零基础学C语言——变量、常量与数据类型
- 2024-11-18 计算机的那些事
- 2024-11-18 这篇 Linux 总结的很棒啊
- 2024-11-18 C语言数据的表示
- 2024-11-18 软网推荐:请个多功能纯文本管理助手
- 最近发表
- 标签列表
-
- jsp (69)
- gitpush (78)
- gitreset (66)
- python字典 (67)
- dockercp (63)
- gitclone命令 (63)
- dockersave (62)
- linux命令大全 (65)
- pythonif (86)
- location.href (69)
- dockerexec (65)
- tail-f (79)
- queryselectorall (63)
- location.search (79)
- bootstrap教程 (74)
- deletesql (62)
- linuxgzip (68)
- 字符串连接 (73)
- html标签 (69)
- c++初始化列表 (64)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)