344. Reverse String(Java/C++)
来源:互联网 发布:淘宝网最新版本 编辑:程序博客网 时间:2024/06/15 08:49
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = “hello”, return “olleh”.
Java1:
public class Solution { public String reverseString(String s) { char[] word = s.toCharArray(); int i = 0; int j = s.length() - 1; while (i < j) { char temp = word[i]; word[i] = word[j]; word[j] = temp; i++; j--; } return new String(word); }}
Java2:
public class Solution { public String reverseString(String s) { return new StringBuilder(s).reverse().toString(); }}
完整cpp程序:
#include<iostream>#include<cstring>#include<algorithm>using namespace std;class Solution {public: string reverseString(string s) { int i = 0; int j = s.size() - 1; while (i < j) swap(s[i++], s[j--]); return s; }};int main() { string str; getline(cin, str); Solution sol; cout << sol.reverseString(str); return 0;}
阅读全文
0 0
- 344. Reverse String(Java/C++)
- 344. Reverse String(C++)
- 344. Reverse String(C语言)
- LeetCode-344. Reverse String-Java
- LeetCode 344. Reverse String(Java)
- LeetCode 344. Reverse String C语言
- [LeetCode]-344. Reverse String(Easy)(C)
- LeetCode 344. Reverse String 题解(C++)
- String:344. Reverse String
- 【Leetcode】:344. Reverse String 问题 in JAVA
- Reverse String JAVA
- leetcode344 Reverse String Java
- java string reverse
- reverse a C-Style String
- leetcode_344_ Reverse String(C++)(easy)
- 【LeetCode-344】 Reverse String(C++)
- [LeetCode-344]Reverse String(c++)
- 344.Reverse String(String-Easy)
- 运行mysql时,提示Table ‘performance_schema.session_variables’ doesn’t exist
- ArrayList原理及源码解析
- boost
- Android的UI布局以及常用属性
- ionic3应用的Android打包签名发布步骤
- 344. Reverse String(Java/C++)
- Python Pandas 快速学习笔记
- 重拾Java(4)-线程
- 查看LINUX进程内存占用情况
- metasploit 使用search报错
- 用ButterKnife出现空指针
- 在表格中隔行换色(js)
- SideBarEnhancements
- LinkedList原理及源码解析