LeetCode 第一题剪彩自己的leetCode之路

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".

click to show clarification.

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 Solution {
    public String reverseWords(String s) {
		 // 判断 ""
		if(s.length()==0) return "";
		// 判断" "
		if(s.length()==1 && s.charAt(0)==' ')return "";

		int num = 0;
		boolean flag = false;
		// 得到字符串中单词的个数
		for(int i=0; i < s.length(); i++){
			if(s.charAt(i)!=' '){
				flag = true;
			}else{
				if(flag){
					num++;
					flag = false;
				}
			}
		}
		String str[] = new String[num+1];
		int len=0;

		// 分离出单词
		String tempStr = "";
		flag  = false;
		for(int i=0; i < s.length(); i++){
			if(s.charAt(i)!=' '){
				tempStr += s.charAt(i);
				flag = true;
			}else{
				if(flag){
					flag = false;
					str[len++] = tempStr;
					tempStr = "";
				}
			}
		}
		str[len] = tempStr;

		// 对单词进行组合,在单词之间加上空格
		String resu = "";
		for(int i=str.length-1; i>=0; i--){
			resu+=str[i];
			if(i!=0)
				resu += " ";
		}
		// 判断结果为""的情况
		if(resu.length()==0) return "";

		// 去除前导零和后置零
		int count=0;
		while(resu.charAt(count)==' '){
			resu = resu.substring(count+1);
		}
		count = resu.length()-1;
		while(resu.charAt(count)== ' '){
			resu = resu.substring(0, count);
			count--;
		}
		return resu;
	}
}

LeetCode 第一题剪彩自己的leetCode之路

时间: 2024-08-03 11:04:06

LeetCode 第一题剪彩自己的leetCode之路的相关文章

LeetCode 第一题,Two Sum

今天早上起来去机房的路上还在想,一直不做算法题老是觉得不踏实.做做题总是让自己觉得自己真的在做做学习.... 这也算是一种强迫症吧. 那就从今天开始做做LeetCode,因为好久没做过了,所以第一题还是看了别人的题解和思路,算是找找感觉. 总的来说第一题是个水.... 题目还原 Two Sum Given an array of integers, find two numbers such that they add up to a specific target number. The fu

LeetCode第一题:Evaluate Reverse Polish Notation

时间:2014.06.11 地点:基地 -------------------------------------------------------------------- 一.题目 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another express

leetcode第一题(easy)

第一题:题目内容 Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums = [2, 7

LeetCode第一题以及时间复杂度的计算

问题描述:给定一组指定整数数组,找出数组中加和等于特定数的两个数. 函数(方法)twoSum返回这两个数的索引,index1必须小于index2. 另外:你可以假设一个数组只有一组解. 一个栗子: Input: numbers={2, 7, 11, 15}, target=9 Output: index1=1, index2=2 算法实现如下: 1 /** 2 * 时间复杂度O(n) 3 * @param array 4 * @param target 5 * @return Map<Integ

leetcode第一题

Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums = [2, 7, 11, 15]

LeetCode 第一题 两数之和

题目描述 给定一个整数数组 nums?和一个目标值 target,请你在该数组中找出和为目标值的那?两个?整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 方法一:暴力法 暴力法很简单,遍历每个元素 xx,并查找是否存在一个值与 target - xtarget?x

LeetCode刷题:第一题 两数之和

从今天开始刷LeetCode 第一题:两数之和 题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 代码如下: 1 /** 2 * Note: The retur

leetcode中第一题twosum问题解答算法的可行性证明

leetcode中第一题twosum问题解答算法的可行性证明 一.引入 关于leetcode中第一题twosum问题,网上已有不少高人做出过解答,并提出了切实可行的算法实现.我在解答该题时参考了博客http://www.zixue7.com/article-9576-1.html的解答.为让读者更直观地阅读和理解本文,先简要摘录以上博客的内容如下: 题目还原 Two Sum Given an array of integers, find two numbers such that they a

新人第一帖 LeetCode做题开始

朋友介绍了leetcode 这个oj网站,包含了很多程序员的试题,我去看了下,对提高算法能力很有针对性,我决定尝试开始解题历程,并和大家分享我的解题,这是第一题: 1: 给定一个int数组,和 一个 int target 值,数组中有两个值的和为target ,给出这两个值的下标,你可以认为解只有一个. 水题,以下是我的代码: public class Solution { public int[] twoSum(int[] nums, int target) { int i=0,j = 0 ;