写出一个程序,接受一个字符串,然后输出该字符串反转后的字符串。(字符串长度不超过1000)
数据范围:
要求:空间复杂度 ,时间复杂度
import java.util.*; public class Solution { /** * 反转字符串 * @param str string字符串 * @return string字符串 */ public String solve (String str) { char[] chars = str.toCharArray(); StringBuffer sb = new StringBuffer(); for (int i = 0; i <chars.length ; i++) { sb.append(chars[chars.length-1-i]); } return sb.toString(); } }
char* solve(char* str ) { // write code here int len = strlen(str); static char res[1000] = { 0 }; char *ans = res; for (int i = len - 1; i >= 0; i--) { sprintf(ans, "%c", str[i]); ans++; } return res; }
# # 反转字符串 # @param str string字符串 # @return string字符串 # class Solution: def solve(self , str ): # write code here return str[::-1]
import java.util.*; public class Solution { /** * 反转字符串 * @param str string字符串 * @return string字符串 */ public String solve (String str) { // write code here char[] s=str.toCharArray(); char[] res=new char[s.length]; int j=0; for(int i=s.length-1;i>=0;i--){ res[j++]=s[i]; } return String.valueOf(res); } }
public class Solution { /** * 反转字符串 * @param str string字符串 * @return string字符串 */ public String solve (String str) { return new StringBuilder(str).reverse().toString(); } }
#include<string> class Solution { public: /** * 反转字符串 * @param str string字符串 * @return string字符串 */ string solve(string str) { // write code here reverse(str.begin(),str.end()); return str; } };
#define _CRT_SECURE_NO_WARNINGS 1//vs2019 #include <stdio.h> #include <string.h> #define max 100//最大字符串长度 //方法一,原数组元素调换 char* fun1(char* str) { int len_str = strlen(str); char tmp; for (int i = 0; i < len_str / 2; i++) { tmp = str[i]; str[i] = str[len_str - 1 - i]; str[len_str - 1 - i] = tmp; } return str; } //方法二,指针移动 char* fun2(char* str) { int len_str = strlen(str); char* star=str;//指向首地址 char* end=star+len_str-1;//指向最后 char temp; while (star<end)//当指针p存储的地址 小于 指针q存储的地址时 循环,即当两个指针指向同一个元素时停止 { temp = *star; *star = *end; *end = temp; star++; end--; } return str; } //方法三,申请一个新的数组存倒序的原数组 char* fun3(char *str) { int len_str = strlen(str); char arr[max] = {0}; for (int i = 0; i < len_str; i++) arr[i] = str[len_str - 1 - i];//倒序保存 return arr; } void main() { char str1[max]; gets(str1); //fun1(str1); //fun2(str1); //puts(fun3(str1)); puts(str1); }
char* solve(char* str ) { // write code here int len = strlen(str); char *p = str; char *q = p + len - 1; while (p < q) { char temp = *p; *p = *q; *q = temp; p++; q--; } return str; }
#include <stdbool.h> #include <errno.h> #define OK 1 #define ERROR -1 #define MEM_OVERFLOW -2 #define DEFAULT_CAPACITY 8 #define InitStack(S) __InitStack(S, DEFAULT_CAPACITY); typedef int Status; // ----------- 顺序栈的数据存储结构的表示与实现 ------------ typedef char SElemType; typedef struct { SElemType* base; // 顺序栈存储空间基地址 SElemType* top; // 栈顶指针 int capacity; } SqStack; Status __InitStack(SqStack* S, int initialCapacity) { if (initialCapacity < 1) { printf("InitStack ERROR: The initialCapacity %d Must be > 0!", initialCapacity); return ERROR; } if (!(S->base = (SElemType*) malloc(initialCapacity * sizeof(SElemType)))) { printf("InitStack OVERFLOW: %s\n", strerror(errno)); exit(MEM_OVERFLOW); } S->top = S->base; S->capacity = initialCapacity; return OK; } bool StackEmpty(SqStack* S) { return S->top == S->base; } bool StackFull(SqStack* S) { return (S->top - S->base) == S->capacity; } size_t StackLength(SqStack* S) { return S->top - S->base; } void __largeCapacity(SqStack* S) { // 优先执行策略1: 在原先空间的尾部追加空间。(不需要移动元素) if ((S->base = (SElemType*) realloc(S->base, S->capacity << 1))) { // resize S->top = S->base + S->capacity; // important! S->capacity <<= 1; return; } puts("strategy 2"); // 策略1在原先空间后找不到一整段连续空间时,执行策略2。 SElemType* new_base = (SElemType*) malloc((S->capacity << 1) * sizeof(SElemType)); if (!new_base) { printf("__largeCapacity OVERFLOW: %s\n", strerror(errno)); exit(MEM_OVERFLOW); } memcpy(new_base, S->base, S->capacity * sizeof(SElemType)); free(S->base); S->base = new_base; S->top = new_base + S->capacity; // 栈顶指针指向新空间的栈顶位置 S->capacity <<= 1; } Status Push(SqStack* S, SElemType e) { if (StackFull(S)) __largeCapacity(S); *S->top++ = e; return OK; } Status Pop(SqStack* S, SElemType* e) { if (StackEmpty(S)) { puts("Pop ERROR: The stack is empty!"); return ERROR; } *e = *--S->top; return OK; } Status DestroyStack(SqStack* S) { free(S->base); S->base = S->top = NULL; return OK; } // ----------- 顺序栈的数据存储结构的表示与实现 ------------ /** * 反转字符串 * @param str string字符串 * @return string字符串 */ char* solve(char* str) { SqStack S; InitStack(&S); char* p = str; while (*p) Push(&S, *p++); p = str; while (!StackEmpty(&S)) Pop(&S, p++); *p = '\0'; return str; }
function solve(str) { // 双指针 let left = 0; let right = str.length - 1; let strArr = [...str]; while (left < right) [strArr[left++], strArr[right--]] = [strArr[right], strArr[left]]; return strArr.join(""); }
function solve( str ) { // write code here let res = "" for (let i in str) { res = str[i] + res } return res }
string solve(string str) { if (str.empty()) return str; int beg = 0; int end = str.size()-1; while (beg < end) { str[beg] ^= str[end]; str[end] ^= str[beg]; str[beg] ^= str[end]; beg++; end--; } return str; }
import java.util.*; public class Solution { /** * 反转字符串 * @param str string字符串 * @return string字符串 */ public String solve (String str) { char[] chars = str.toCharArray(); for (int i = 0, limit = chars.length - 1, j = limit; i < chars.length / 2; i++, j--) { char temp = chars[i]; chars[i] = chars[j]; chars[j] = temp; } return new String(chars); } }