Leetcode 详解(ReverseWords)
Leetcode里面关于字符串的一些问题,描述如下:
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 inO(1) space.
Subscribe to see which companies asked this question
解决方案如下:
import java.util.*;
import java.io.*;
import java.lang.*; public class ReverseWords { public String reverseWords(String s) {
StringBuilder reversed = new StringBuilder(); //构建一个空的字符串构建器
int j = s.length();
for (int i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) == ' ') {
j = i;
}
else if (i == 0 || s.charAt(i - 1) == ' ') {
if (reversed.length() != 0) { //当输入只有一个字符的时候,如"a",这时候程序会运行到这一步,但注意此时构建器是没有内容的,因而reversed.length()为0,不满足条件
reversed.append(' '); // 空格也要输进去,但同时要避免两端是空格的情况,那时候是不要输入的。
}
reversed.append(s.substring(i, j)); // 注意此处,运用了字串substring,这个和我之前的想法是一致的,即连接在一起的字符串用substring截取下来。这里,截取下来后放入字符串构建器中
} // substring(int i,int j) 返回一个新字符串,这个新字符串包含原始字符串中的位置从i~j-1的部分,注意下标不包括j
}
return reversed.toString(); // 注意这才是字符串构建器的正确输出格式,返回的是一个与构建器(或称为缓冲器)内容相同的字符串
}
/*
private String ReWords(String s) {
String B=" ";
for(int i=0;i<s.length();i++)
{
//for(int j=i;s.charAt(j)!="";j++);
int j=i;
while(Character.isLetter(s.charAt(j))&&(j<s.length()))j++;
String A=s.substring(i,j);
B=A+B;
i=j;
}
return B;
}
*/
public static void main(String[] args){ ReverseWords Reverse = new ReverseWords();
String out_target=Reverse.reverseWords("ab cac cc");
System.out.printf("the output is "+out_target);
System.out.println();
}
}
总结:注释掉的部分,是我自己的思路,但是行不通,主要体现在:我是采用字符串连接的方式解决这个问题的,这样每次连接字符串都会产生一个心结的String对象,既耗时又浪费空间。并且,思路有点混乱。
cleancode 采用了StringBuilder类,这样就可以避免上诉的问题。可以认真阅读StringBuilder类中的相关内容,可以发现是很适合解决这个问题的。
解决上述问题,相当于用两个标志,两个标志之间就是一个单词,然后从后向前看,首先判断当前的是不是空格,如果是,则把两者定位在空格处(这时候i--,而j不用动,substring()的特性),如果当前不是空格,则判断当前的是字母的下一位
是不是空格,如果是则在字符串构建器末尾加空格(此处的i==0的判断,主要是和里面的判断一起用的,以防止只有一个字母的时候),如果上面条件都不满足,那么就把当前的字母加入到字符串构建器中。
Leetcode 详解(ReverseWords)的更多相关文章
- 由Leetcode详解算法 之 动态规划(DP)
因为最近一段时间接触了一些Leetcode上的题目,发现许多题目的解题思路相似,从中其实可以了解某类算法的一些应用场景. 这个随笔系列就是我尝试的分析总结,希望也能给大家一些启发. 动态规划的基本概念 ...
- Leetcode 详解(股票交易日)(动态规划DP)
问题描述: 在股市的交易日中,假设最多可进行两次买卖(即买和卖的次数均小于等于2),规则是必须一笔成交后进行另一笔(即买-卖-买-卖的顺序进行).给出一天中的股票变化序列,请写一个程序计算一天可以获得 ...
- Leetcode详解Maximum Sum Subarray
Question: Find the contiguous subarray within an array (containing at least one number) that has the ...
- Leetcode 详解(Substing without repeats character)
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- Leetcode 详解(Valid Number)
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- Leetcode 详解(Implement strstr)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Leetcode 详解(valid plindrome)
Question: Given a string, determine if it is a palindrome, considering only alphanumeric characters ...
- The Skyline Problem leetcode 详解
class Solution { public: vector<pair<int, int>> getSkyline(vector<vector<int>&g ...
- LeetCode(42.接雨水)多解法详解
接雨水解法详解: 题目: 基本思路:从图上可以看出要想接住雨水,必须是凹字形的,也就是当前位置的左右两边必须存在高度大于它的地方,所以我们要想知道当前位置最多能存储多少水,只需找到左边最高处max_l ...
随机推荐
- HTML 链接 - href
链接 在HTML的学习中,链接的标签发挥着很大的作用,HTML 使用超级链接与网络上的另一个文档相连.几乎可以在所有的网页中找到链接.点击链接可以从一张页面跳转到另一张页面. 比如说:实例 创建超级链 ...
- nfs 配置
服务端1.打印系统版本cat /etc/redhat-release2.检查并安装NFS服务rpm -aq nfs-utils portmap rpcbindyum grouplistyum grou ...
- PSR-1:基本的代码风格
PHP标签 必须把PHP代码放在<?php ?>或<?= ?>标签中.不得使用其他PHP标签句法. 编码 所有PHP文件都必须使用UTF-8字符集编码,而且不能有字节顺序标记( ...
- Using FreeMarker templates (FTL)- Tutorial
Lars Vogel, (c) 2012, 2016 vogella GmbHVersion 1.4,06.10.2016 Table of Contents 1. Introduction to F ...
- grep命令
grep (缩写来自Globally search a Regular Expression and Print)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来. grep ...
- angular 控制器的使用两种模式
angular.module("myApp",[]) .controller("firstCtrl",["$scope",function( ...
- 求System.arraycopy的用法
public class Shuzufuzhi { public static void main(String args[]) { int myArray[]={1,2,3,4,5,6}; in ...
- hihoCoder 1432 : JiLi Number(吉利数)
hihoCoder #1432 : JiLi Number(吉利数) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - 题目描述 Driver Ji l ...
- pyqt4学习笔记
信号与槽机制 信号与槽机制作为Qt最重要的特性,提供了任意两个Qt对象之间的通信机制.其中,信号会在某个特定情况或动作下被触发,槽是用于接收并处理信号的函数.例如,要将一个窗口中的变化情况通知给另一个 ...
- laravel old
最近做一个laravel框架下的一个网页.遇到了old 无法点击选中的问题,捉摸好久,原来,laravel下的old 是基于seesion下的. 如果想用old必须要在session有的情况下.