LeetCode 344. Reverse String
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
题目标签:String
题目给了我们一个string,要求我们reverse string 并且返回。
可以利用two pointers 来 互换 左边的char 和右边的 char。具体看Code。
Java Solution:
Runtime beats 55.59%
完成日期:03/07/2017
关键词:two pointers
关键点:swap left and right chars
1 class Solution 2 { 3 public String reverseString(String s) 4 { 5 char [] st = s.toCharArray(); 6 7 for(int i = 0; i < st.length / 2; i++) 8 { 9 int rc_index = st.length - i - 1; 10 11 // swap left and right characters 12 char lc = st[i]; 13 st[i] = st[rc_index]; 14 st[rc_index] = lc; 15 16 } 17 18 return new String(st); 19 } 20 }
参考资料:n/a
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步