Leetcode——344. Reverse String

本文介绍了两种字符串反转的方法:一种使用StringBuilder实现,另一种通过字符数组完成。后者具有更好的运行效率。

目前为止唯一一个直接写出来没出错的。。

But时间略长,runtime只打败了9.14%的人

 public String reverseString(String s) {
		 int length=s.length();
		 StringBuilder tString;
		 char a[]=new char[length];
		 for(int i=0;i<length;i++)
			 tString.append(s.charAt(i));
			return tString.toString();
		
	 }

  更好地runtime打败了55%的人,大概是append有些耗时,但是是第一次尝试使用StringBuilder还算开心啦哈哈

  public String reverseString(String s) {  
        char[] chars = new char[s.length()];  
        int index = 0;  
        for (int i = s.length() - 1; i >= 0; i--) {  
            chars[index++] = s.charAt(i);  
        }  
        return new String(chars);  
    }  

  

转载于:https://www.cnblogs.com/Cherrylalala/p/6550961.html

### C++ 常用函数及其在 LeetCode 中的应用 #### 数学运算相关 C++ 提供了丰富的数学库函数,这些函数通常位于 `<cmath>` 头文件中。以下是几个常见的例子: - **绝对值计算** `abs` 函数用于求解整数或浮点数的绝对值[^1]。例如: ```cpp #include <iostream> #include <cmath> using namespace std; int main() { double a = -3.5; cout << abs(a) << endl; // 输出 3.5 return 0; } ``` - **取整操作** 取整函数包括 `ceil`, `floor`, 和 `round`,分别表示向上取整、向下取整和四舍五入。例如: ```cpp #include <iostream> #include <cmath> using namespace std; int main() { double b = 2.7; cout << ceil(b) << " " << floor(b) << " " << round(b) << endl; // 输出 3 2 3 return 0; } ``` --- #### 排序与自定义比较器 LeetCode 题目经常涉及数组或向量的排序问题。标准模板库 (STL) 的 `sort` 函数非常强大,可以通过传递第三个参数来自定义排序逻辑[^2]。 - 默认升序排列: ```cpp #include <vector> #include <algorithm> #include <iostream> using namespace std; int main() { vector<int> nums = {6, 5, 9, 1}; sort(nums.begin(), nums.end()); for(auto n : nums){ cout << n << " "; } // 输出: 1 5 6 9 return 0; } ``` - 自定义降序排列: ```cpp #include <vector> #include <algorithm> #include <functional> #include <iostream> using namespace std; int main(){ int num[10] = {6,5,9,1,2,8,7,3,4,0}; sort(num, num+10, greater<int>()); for(int i=0;i<10;i++) cout<<num[i]<<" "; // 输出: 9 8 7 6 5 4 3 2 1 0 return 0; } ``` --- #### 字符串处理 字符串反转是一个常见需求,可以使用 STL 提供的 `reverse` 函数来实现。 - 示例代码: ```cpp #include <string> #include <algorithm> #include <iostream> using namespace std; int main() { string str = "hello"; reverse(str.begin(), str.end()); cout << str << endl; // 输出 olleh return 0; } ``` --- #### 容器操作 LeetCode 经常考察各种容器的操作技巧,比如队列 (`queue`) 和栈 (`stack`) 的基本功能[^3]。 - **队列基础** 队列遵循先进先出原则 (FIFO),其主要成员函数如下所示: ```cpp #include <queue> #include <iostream> using namespace std; int main() { queue<int> q; q.push(1); q.push(2); while(!q.empty()){ cout << q.front() << " "; // 输出 1 2 q.pop(); } return 0; } ``` - **栈基础** 栈是一种后进先出结构 (LIFO),常用成员函数有 `push`, `top`, 和 `pop` 等。 ```cpp #include <stack> #include <iostream> using namespace std; int main() { stack<string> stk; stk.push("world"); stk.push("hello"); while (!stk.empty()) { cout << stk.top() << " "; // 输出 hello world stk.pop(); } return 0; } ``` --- #### 动态规划与正则表达式匹配 某些复杂题目需要用到动态规划或者正则表达式的概念。例如,LeetCode 上的经典问题——正则表达式匹配[^4]。 - 示例代码(简化版): ```cpp class Solution { public: bool isMatch(string s, string p) { int m = s.length(), n = p.length(); vector<vector<bool>> dp(m + 1, vector<bool>(n + 1, false)); dp[0][0] = true; for(int j = 2; j <= n && p[j - 1] == '*'; j += 2){ dp[0][j] = dp[0][j - 2]; } for(int i = 1; i <= m; ++i){ for(int j = 1; j <= n; ++j){ if(p[j - 1] != '*'){ dp[i][j] = dp[i - 1][j - 1] && (s[i - 1] == p[j - 1] || '.' == p[j - 1]); } else{ dp[i][j] = dp[i][j - 2] || ((s[i - 1] == p[j - 2] || '.' == p[j - 2]) && dp[i - 1][j]); } } } return dp[m][n]; } }; ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值