[LeetCode 151] Reverse Words in a String

1.4 点云数据获取方式——结构光相机 与此同时,利用相机从特定角度捕捉变形后的图案,基于三角测量原理,通过精确计算相机与投影仪之间的几何关系以及图案的变形程度,就能准确计算出物体表面各点的三维坐标,从而获取高精度的三维点云数据。在高端制造业中,对于微小零部件的尺寸测量和缺陷检测,结构光相机能够提供极其精确的数据,确保产品质量符合严格的标准。时序编码:这类相机通过序列投射多幅不同图案,如格雷码、相移条纹等,通过对这一系列图案的时间序列进行解码分析,就能够获取亚像素级精度的三维信息,其单点误差能够控制在≤0.1mm,展现出极高的测量精度。 阅读详情

题目链接:reverse-words-in-a-string


import java.util.Stack;

/**
 * 
 * Given an input string, reverse the string word by word.
 * 
 * For example, Given s = "the sky is blue", return "blue is sky the".
 * 
 * Update (2015-02-12): For C programmers: Try to solve it in-place in O(1)
 * space.
 * 
 * 
 * Clarification: What constitutes a word? A sequence of non-space characters
 * constitutes a word. Could the input string contain leading or trailing
 * spaces? Yes. However, your reversed string should not contain leading or
 * trailing spaces. How about multiple spaces between two words? Reduce them to
 * a single space in the reversed string.
 * 
 */

public class ReverseWordsInAString {

	//解法一
	// 22 / 22 test cases passed.
	// Status: Accepted
	// Runtime: 410 ms
	// Submitted: 0 minutes ago

	//时间复杂度O(n) 空间复杂度O(n)
	
	static String reverseWords(String s) {
		String reverseString = "";
		int i = 0;
		while (i < s.length()) {
			while (i < s.length() && s.charAt(i) == ' ') {
				i++;
			}
			int begin = i;
			while (i < s.length() && s.charAt(i) != ' ') {
				i++;
			}
			if (begin != i && reverseString.length() != 0) {
				reverseString = " " + reverseString;
			}
			reverseString = s.substring(begin, i) + reverseString;
		}
		return reverseString;

	}

	//解法二: 用StringBuilder快很多
	// 22 / 22 test cases passed.
	// Status: Accepted
	// Runtime: 252 ms
	// Submitted: 0 minutes ago
	
	//时间复杂度O(n) 空间复杂度O(n)
	
	static String reverseWords1(String s) {
		StringBuilder sb = new StringBuilder();
		Stack<String> stack = new Stack<String>();
		int i = 0;
		while (i < s.length()) {
			while (i < s.length() && s.charAt(i) == ' ')
				++i;
			int begin = i;
			while (i < s.length() && s.charAt(i) != ' ')
				++i;
			if (begin != i && stack.size() != 0)
				stack.push(" ");
			stack.add(s.substring(begin, i));
		}
		while (!stack.isEmpty())
			sb.append(stack.pop());
		return sb.toString();
	}

	public static void main(String[] args) {
		System.out.println(reverseWords("the sky is blue"));

	}

}


SAP BOM批量反查代码分享 SAP BOM反查批量查询代码分享 阅读详情

相关推荐

SpringBoot+Vue企业级开源聚合支付系统!支持30+款支付平台,只需配置商户证书就能直接使用!

一款开源SAAS支付系统推荐

技术Z先生的博客 2727

Leetcode 151 Reverse Words in a String

lc151 Reverse Words in a String 利用split(),将String分成一个String[],可以用正则表达式"\\s+"(第一个\转义,\s表示空格,+表示多个空格),也可以直接用" ",第二种方法快一些 然后按题意翻转即可,从后面遍历String,一次将元素放入新String(这里可以用StringBuilder,会比直接用+连接字符串快,最后sb.toStr...

weixin_30609331的博客 103

HWSD全球土壤数据下载处理

下载地址: http://www.fao.org/soils-portal/soil-survey/soil-maps-and-databases/harmonized-world-soil-database-v12/en/

江北20190411的博客 3万+

【leetcode】【medium】【每日一题】151. Reverse Words in a String​​​​​​​

151. Reverse Words in a String Given an input string, reverse the string word by word. Example 1: Input: "the sky is blue" Output:"blue is sky the" Example 2: Input: " hello world! " Output...

lemonade13的博客 146

【Ragflow】9.问答为什么比搜索响应慢?从源码角度深入分析

前几天看到有群友提到一个现象:Ragflow的搜索界面的问答速度很快,但聊天界面的问答速度很慢,究竟是什么原因?在深入挖掘源码之后,逐渐发现了问题的答案。

兴趣使然的创作者 2914

(R,线性回归)R语言里的模型诊断图(Residuals vs Fitted,Normal QQ , Scale-Location ,Residuals Leverage)

线性回归,是概率统计学里最重要的统计方法,也是机器学习中一类非常重要的算法。线性模型简单理解非常容易,但是内涵是非常深奥的。尤其是线性回归模型中的Diagnostics plot的阅读与理解一直被认为是线性回归中的一个难点。 在任何线性模型中,能够直接“lm”(模型有意义),既要考虑各个参数的t-test所得出的p-value,也要考虑总体模型F-检验得出的p-value。在这之后,还要清楚一个...

qq_35837578的博客 7万+

Task4:LeetCode1:twoSum; LeetCode344:Reverse String;LeetCode151:Reverse Words in a String

1.Two Sum(两个数求和) class Solution: def twoSum(self, nums: 'List[int]', target: int) -> 'List[int]': dicti = {} for i, n in enumerate(nums): if n in dicti: ...

m0_37548423的博客 169

C++学习(四六六)Multiple parse contexts are available for this file

Note: Multiple parse contexts are available for this file. Choose the preferred one from the editor menu 参考: "Multiple parse contexts" in Qt creator | Qt ForumHi Do you use sessions? https://bugreports.qt.io/browse/QTCREATORBUG-18521 Anyway, its not.

hankern的专栏 4618

LeetCode(151)Reverse Words in a String

题目如下: Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". 分析如下:         很基础的题目,需要考虑各种corner cases,比如下列: 1 "

BUILD 4469

基于Matlab的MIMO通信系统仿真_Simulink实现

瑞利衰落信道中使用空时分组码的BPSK调制的BER 1、2发1收 参数设置: 双极性信号元+1和-1,每符号含1bit; 采样时间=符号时间=1s; 采用基于数据帧的仿真计算模式,每帧中有2个采样值; 仿真时间=出现100个误码后停止仿真; 随机整数种子=22; Jakes衰落模型中每个信道上的多普勒频移=0.01Hz; 输人信号功率=2W; Alamouti码:两根发送天线,一根接收天线(分集阶= 2); 平均信道信噪比= 18dB; 仿真得出的误码率统计BER =0. 00017. 2、2发

太阳帅帅的博客 9692

(持续更新中!)详解【计算机类&面试真题】军队文职考试 ——第一期(真题+解析)| TCP、UDP的区别;程序局部性;ICMP协议及主要功能;重定位所需的计算机硬件

在当今信息化时代,军队中对于计算机专业人才的需求与日俱增。因此,了解面试和复试中常见的问题和要求对于准备应聘者来说至关重要。本文将围绕面试的准备要点、常见问题以及面试技巧进行探讨,并提供一些建议和经验。通过了解面试和复试的准备要点、常见问题以及技巧,读者将能够提升应对能力和成功率。本文为准备军队文职面试和计划考研复试的读者提供了有价值的参考和指导。通过阅读本篇博客,读者将能够获得关于军队文职计算机类面试和计算机考研复试的全面了解,提升自身的应对能力和成功率。

追光者♂:记录、分享、总结、提升,现象级专栏《Python从入门到人工智能》作者,无惧黑暗,坚信曙光 1691

【LeetCode】 151. Reverse Words in a String 翻转字符串里的单词(Medium)(JAVA)每日一题

【LeetCode】 88. Merge Sorted Array 翻转字符串里的单词(Medium)(JAVA) 题目地址: https://leetcode.com/problems/merge-sorted-array/ 题目描述: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one s...

qq_16927853的博客 201

用 Custom Entity 打造 RAP Value Help:从能跑到好用的深度踩坑笔记

本文介绍了在RAP项目中实现Value Help(Fiori输入帮助)的完整流程。通过Custom Entity方式,开发者可以灵活构建不依赖数据库实体的值帮助功能,但需手动实现CDS原本提供的过滤、排序、分页等能力。文章以一个典型场景为例,详细说明了如何为Partner维护界面的City字段创建可搜索的值帮助,包括创建查询实现类、绑定CDS Custom Entity、服务发布等关键步骤,并特别指出了处理排序分页请求等常见问题。最后强调在ABAP Cloud环境下需要自行实现查询能力,而非依赖传统工具类。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 148

小练习 - LeetCode151 Reverse Words in a String的C语言解法

LeetCode 151. Reverse Words in a String 很适合练习C语言基础,练习一个OJ题目除了找最佳解法,还可以多想想刻意去练习一些数据结构等其他知识点,让练习举一反三。 Given an input string, reverse the string word by word. Example: Input: “the sky is blue”, Output: ...

Life runs on code 674

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

【LeetCode】151. Reverse Words in a String

LeetCode151 传送门 一种解法: 容易想到两次遍历的方法。第一次遍历分词,第二次遍历反向拼接单词成为句子。然后可以将两次遍历改进为一次遍历。首先初始化一个空字符串s_rev,初始化指针end为原字符串最后一个位置加一。倒序遍历原字符串s,若s[i]为空格,则赋值end为i。否则,如果i - 1位置为空格,或i为0位置,则令s_rev拼接s[i:j]。但该方法没有使用pytho...

weixin_42344158的博客 265

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

LeetCode 151 Reverse Words in a String

思路1 不用split和trim函数。使用一个stack来从前往后存储单词,最后再依次pop出来即可。 复杂度 时间复杂度O(n), 空间复杂度O(n) 代码 class Solution { public String reverseWords(String s) { if(s.length() == 0) return ""; S...

我要上岸的博客 212

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

[Leetcode] 151. Reverse Words in a String

link: https://leetcode.com/problems/reverse-words-in-a-string/ Solution: split + reverse class Solution { public String reverseWords(String s) { if(s.length() == 0) return s; Strin...

Renoler的博客 142

leetcode 151 Reverse Words in a String (python)

Python Reverse Words in a String

快递小可的博客 2013

Leetcode 151题 Reverse Words in a String

Leetcode 151题 Reverse Words in a String

《致青春》——云梦泽 1103

[每日算法] leetcode第151题 Reverse Words in a String

原题目描述 Given an input string, reverse the string word by word. Example: Input: “the sky is blue”, Output: “blue is sky the”. Note: A word is defined as a sequence of non-space characters. Input stri...

大葱敏的博客 3560

【leetcode】151. Reverse Words in a String

解题思路: 双指针 提交代码: class Solution { public String reverseWords(String s) { if(s==null||s.length()==0) return s; String res=""; int p1=s.length()-1,p2; while(p1&gt;=0&amp;&amp;s.charAt(p1)=='...

AXIMI的博客 386

[Leetcode] 151. Reverse Words in a String 解题报告

题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". Update (2015-02-12): For C programmers: Try to solve it in

魔豆(Magicbean)的博客 886

LeetCode 151. Reverse Words in a String

题目来源:https://leetcode.com/problems/reverse-words-in-a-string/ 问题描述 151.Reverse Words in a String Medium Given an input string, reverse the string word by word. Example 1: Input: "the sky is blu...

da_kao_la的博客 151

[leetcode] 151. Reverse Words in a String 解题报告

题目链接:https://leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". U

小榕流光的专栏 3052

LeetCode 151. Reverse Words in a String

题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". Clarification: What constitutes a word? A sequence

雪水 1372
上一篇: [LeetCode 142] Linked List Cycle II
下一篇: [LeetCode 135] Candy
xiaoo_gan
博客等级 码龄16年 20粉丝 165原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值