当前位置:网站首页>Flip the array gracefully
Flip the array gracefully
2022-07-01 11:07:00 【tuhoooo】
introduction
Original address : Flip the array gracefully
Welcome to my blog : http://blog.duhbb.com/
I feel that my code is not concise , And it's easy to make mistakes , It's the same every time .
Flipped writing
The subject is very simple , But there is a place to learn : Array flipping .
I liked to write this before :
for (int k = j; k <= (i+j)/2; k++) {
char tmp = s[k];
s[k] = s[i - (k - j)];
s[i-(k-j)] = tmp;
}
I saw it today leetcode The answer on , It turns out that this is more elegant , for Circular writing looks more complicated , It's easy to make mistakes .
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++;
}
// This for Circular writing is ugly , And it's easy to make mistakes
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;
}
Conclusion
Original address : Flip the array gracefully
Welcome to my blog : http://blog.duhbb.com/
No matter how much business you write, you don't pay attention to refining and summarizing , The more I write, the more confused I feel , In fact, it may be more rewarding to stop and look at the code written by others .
边栏推荐
- (POJ - 1456) supermarket
- [.net6] use ml.net+onnx pre training model to liven the classic "Huaqiang buys melons" in station B
- Dotnet console uses microsoft Maui. Getting started with graphics and skia
- 華為設備配置大型網絡WLAN基本業務
- I'd like to know where I can open an account in Guangzhou? Is it safe to open an account online now?
- Combination of Oracle and JSON
- Ask everyone in the group about the fact that the logminer scheme of flick Oracle CDC has been used to run stably in production
- flutter Uint8List格式的图片和File格式图片的互相转换
- 12 product management platforms that everyone is using
- 关于Keil编译程序出现“File has been changed outside the editor,reload?”的解决方法
猜你喜欢

LeetCode. 515. Find the maximum value in each tree row___ BFS + DFS + BFS by layer

Oracle和JSON的結合
![[encounter Django] - (II) database configuration](/img/23/aed472757f7e238a146b043c0405d7.png)
[encounter Django] - (II) database configuration

Node version manager NVM installation and switching

CVPR22 |CMT:CNN和Transformer的高效结合(开源)

2022年6月编程语言排行,第一名居然是它?!

Combinaison Oracle et json

名创拟7月13日上市:最高发行价22.1港元 单季净利下降19%

Technology sharing | introduction to linkis parameters

BAIC bluevale: performance under pressure, extremely difficult period
随机推荐
达梦数据冲刺科创板:拟募资24亿 冯裕才曾为华科教授
Node version manager NVM installation and switching
“目标检测”+“视觉理解”实现对输入图像的理解及翻译(附源代码)
Internal control of fund managers
kubernetes之ingress探索实践
名创拟7月13日上市:最高发行价22.1港元 单季净利下降19%
. Net 5.0+ does not need to rely on third-party native implementation of scheduled tasks
Numpy的矩阵
"Target detection" + "visual understanding" to realize the understanding and translation of the input image (with source code)
LeetCode 438. 找到字符串中所有字母异位词__滑动窗口
华为设备配置大型网络WLAN基本业务
mysql如何把 一个数据库中的表数据 复制到 另一个数据库中(两个数据库不在同一个数据库链接下)
Google's new paper Minerva: solving quantitative reasoning problems with language models
云上“视界” 创新无限 | 2022阿里云直播峰会正式上线
node版本管理器nvm安装及切换
[AI information monthly] 350 + resources! All the information and trends that can't be missed in June are here! < Download attached >
十年磨一剑:蚂蚁集团可观测性平台 AntMonitor 揭秘
What are the advantages and disadvantages of PHP
Want to open an account, is it safe to open an account of Huatai Securities online?
I'd like to know where I can open an account in Guangzhou? Is it safe to open an account online now?