当前位置:网站首页>优雅地翻转数组
优雅地翻转数组
2022-07-01 11:04:00 【tuhoooo】
引言
原文地址:优雅地翻转数组
欢迎访问我的博客: http://blog.duhbb.com/
感觉自己的代码写的不简洁, 而且容易出错, 搞得每次都很赶一样.
翻转的写法
题目很简单, 但是有个地方可以学习下: 就是数组翻转.
之前我喜欢这么写:
for (int k = j; k <= (i+j)/2; k++) {
char tmp = s[k];
s[k] = s[i - (k - j)];
s[i-(k-j)] = tmp;
}
今天看了 leetcode 上的解答, 原来这么写更优雅, for 循环写的看上去就比较复杂, 还容易出错.
int left = start, right = i - 1;
while (left < right) {
swap(s[left], s[right]);
left++;
right--;
}
#include <iostream>
#include <vector>
#include <climits>
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
string reverseWords(string s) {
int i = 0, j = 0;
while(true) {
while (s[i+1] != ' ' && s[i+1] != '\0') {
i++;
}
// 这个 for 循环写的就很丑, 而且还容易出错
for (int k = j; k <= (i+j)/2; k++) {
char tmp = s[k];
s[k] = s[i - (k - j)];
s[i-(k-j)] = tmp;
}
if (! s[i+1]) {
break;
}
i += 2;
j = i;
}
return s;
}
};
int main() {
Solution s;
cout << s.reverseWords("hello world") << endl;
return 0;
}
结束语
原文地址:优雅地翻转数组
欢迎访问我的博客: http://blog.duhbb.com/
写了再多的业务不注意提炼和总结, 感觉越写越迷糊, 其实停下来看看别人写的代码也许收获会更多.
边栏推荐
- 云上“视界” 创新无限 | 2022阿里云直播峰会正式上线
- 谷歌新论文-Minerva:用语言模型解决定量推理问题
- 2022年现在在网上开通股票账户安全吗?会不会有什么危险?
- PHP realizes lottery function
- Google's new paper Minerva: solving quantitative reasoning problems with language models
- 華為設備配置大型網絡WLAN基本業務
- CVPR22 |CMT:CNN和Transformer的高效结合(开源)
- 十年磨一剑:蚂蚁集团可观测性平台 AntMonitor 揭秘
- Have the bosses ever done the operation of sink shunting and writing to Clickhouse or other databases.
- 获取键代码
猜你喜欢
Value 1000 graduation project campus information publishing platform website source code
[MPC] ① quadratic programming problem matlab solver quadprog
[.NET6]使用ML.NET+ONNX预训练模型整活B站经典《华强买瓜》
Intel Labs announces new progress in integrated photonics research
[.net6] use ml.net+onnx pre training model to liven the classic "Huaqiang buys melons" in station B
商城小程序源码开源版-可二开
. Net 5.0+ does not need to rely on third-party native implementation of scheduled tasks
个人商城二开逍遥B2C商城系统源码-可商用版/拼团拼购优惠折扣秒杀源码
y48.第三章 Kubernetes从入门到精通 -- Pod的状态和探针(二一)
MIT's latest paper, "the need for interpretable features: motivation and classification": building interpretability in the constituent elements of machine learning models
随机推荐
[MPC] ② quadprog solves positive definite, semi positive definite and negative definite quadratic programming
How does MySQL copy table data from one database to another (two databases are not linked to the same database)
Can I choose to open an account on CICC securities? Is it safe?
LeetCode 438. 找到字符串中所有字母异位词__滑动窗口
英特尔实验室公布集成光子学研究新进展
Huawei Equipment configure les services de base du réseau WLAN à grande échelle
Guys, how to export iceberg data to MySQL? What tools are there? Neither sqoop nor dataX
12款大家都在用的產品管理平臺
Handling distributed transactions with powerful dbpack (PHP tutorial)
The first anniversary of the data security law, which four major changes are coming?
《百年巨匠》数字藏品中奖名单公布
Graduation season · advanced technology er
NeurIPS 2022 | 细胞图像分割竞赛正式启动!
Sqlachemy common operations
[paper reading] trajectory guided control prediction for end to end autonomous driving: a simple yet strong Ba
Ten years of sharpening a sword: unveiling the secrets of ant group's observability platform antmonitor
The idea runs with an error command line is too long Shorten command line for...
移动硬盘驱动器读到,但不显示盘符
LeetCode 438. Find all letter ectopic words in the string__ sliding window
JS基础--数据类型