leetcode344
public class Solution {
public string ReverseString(string s) {
var list = s.Reverse();
StringBuilder sb = new StringBuilder();
foreach (var c in list)
{
sb.Append(c);
}
//Console.WriteLine(sb.ToString());
return sb.ToString();
}
}
https://leetcode.com/problems/reverse-string/#/description
下面是使用C++的解决方案,不实用api:
class Solution {
public:
string reverseString(string s) {
string temp = "";
for (int i = s.length() - ; i >= ; i--)
{
temp += s[i];
}
return temp;
}
};
这道题目有所变化,原来的要求是返回string,现在的要求是不返回任何值,而是在原来的字符串上直接修改。 下面使用python实现新的要求:
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
n = len(s)
i,j = ,n-
while i < j:
s[j],s[i] = s[i],s[j]
i +=
j -=
leetcode344的更多相关文章
- 【算法训练营day8】LeetCode344. 反转字符串 LeetCode541. 反转字符串II 剑指Offer05. 替换空格 LeetCode151. 翻转字符串里的单词 剑指Offer58-II. 左旋转字符串
[算法训练营day8]LeetCode344. 反转字符串 LeetCode541. 反转字符串II 剑指Offer05. 替换空格 LeetCode151. 翻转字符串里的单词 剑指Offer58- ...
- LeetCode344:Reverse String@Python
Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...
- 每天一道LeetCode--344. Reverse String
Write a function that takes a string as input and returns the string reversed. Example:Given s = &qu ...
- leetcode344——Reverse String(C++)
Write a function that takes a string as input and returns the string reversed. Example:Given s = &qu ...
- [Swift]LeetCode344. 反转字符串 | Reverse String
Write a function that takes a string as input and returns the string reversed. Example 1: Input: &qu ...
- 面试题:反转字符串(leetcode344)
编写一个函数,其作用是将输入的字符串反转过来.输入字符串以字符数组 char[] 的形式给出. 不要给另外的数组分配额外的空间,你必须原地修改输入数组.使用 O(1) 的额外空间解决这一问题. 你可以 ...
- leetcode344 反转字符串 c++实现
编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh" 示例 2: 输入: "A man, a p ...
- LeetCode-344:Reverse String
This is a "Pick One" Problem :[Problem:344-Reverse String] Write a function that takes a ...
- LeetCode344 反转字符串
编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh" 示例 2: 输入: "A man, a p ...
随机推荐
- hdu1059 dp(多重背包二进制优化)
hdu1059 题意,现在有价值为1.2.3.4.5.6的石头若干块,块数已知,问能否将这些石头分成两堆,且两堆价值相等. 很显然,愚蠢的我一开始并想不到什么多重背包二进制优化```因为我连听都没有听 ...
- poj2387 最短路
题意:给出一堆双向路,求从N点到1点的最短路径,最裸的最短路径,建完边之后直接跑dij或者spfa就行 dij: #include<stdio.h> #include<string. ...
- java泛型学习(2)
一:深入泛型使用.主要是父类和子类存在泛型的demo /** * 父类为泛型类 * @author 尚晓飞 * @date 2014-7-15 下午7:31:25 * * * 父类和子类的泛型. * ...
- 南阳oj-ASCII码排序-用了一个晚上
#include <iostream> #include <sstream> #include <stdio.h> #include <string> ...
- MySQL--Alter Table注意事项
======================================================================== ALTER TABLE 和FLUSH TABLE导致的 ...
- mockito框架
2016-04-09 15:56:26 参考自 http://www.cnblogs.com/silence-hust/p/5017233.html http://blog.csdn.net/sdyy ...
- opencart安装和使用PHPMailer
一.安装PHPMailer 1)先给opencart项目安装vqmod 下载最新版本: http://code.google.com/p/vqmod (目前最新版本是vqmod-2.5.1-stand ...
- JVM监控
jconsole 说明: 首先JConsole这个是JDK里面自带的工具 在JAVA_HOME/bin目录下,今天主要测试远程监控JVM 第一步:设置好需要远程机器的Tomcat 修改Tomcat下 ...
- CC2530低功耗设置(针对终端设备)
一. 终端设备只完成发送数据 1.开启POWER_SAVING 预编译 2.f8wConfig_cfg 中的RFD_RCVC_ALWAYS_ON=FALSE 3.f8wConfig_cfg中四个DPo ...
- Django ORM基本的单表增删改查
创建表 步骤: 1.app下models.py里创建类(继承models.Model) from django.db import models class UserInfo(models.Model ...