跳过正文
  1. Posts/

[018] Length Of Last Word

·1 分钟·
目录

题目:
#

Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space characters only. For example, Given s = “Hello World”, return 5.

题意:
#

难度不大,考虑所有边界情况即可。

解法:
#

C++版本实现方法1:

   // 从右往左依次遍历即可
  class Solution {
	public:
		int lengthOfLastWord(string s) {
			int length = 0;
			for (int index = s.length() - 1; index >= 0; index--){
				char c = s.at(index);
				if (c != ' '){
					length++;
				}
				else if (c == ' ' && length != 0){
					return length;
				}
			}
			return length;
		}
	}

leetCode Oj系统评判结果如下图所示:

leetCode C++1
;

C++版本实现方法2:


	// 直接利用现有轮子STL 
class Solution {
	public:
		int lengthOfLastWord(string s) {
			auto left = find_if(s.rbegin(), s.rend(), ::isalpha);
			auto right = find_if_not(left, s.rend(), ::isalpha);
			return distance(left, right);
		}
	}
	

leetCode Oj系统评判结果如下图所示:

leetCode C++2
;

相关文章

[151] Reverse Words in a String

·1 分钟
题目:Reverse Words in a String # Given an input string s, reverse the string word by word. For example, given s = “the sky is blue”, return “blue is sky the”.

Two Sum

·3 分钟
本文主要包括 leetCode 题集里的两个题目,Two Sum1 和 Two Sum2 题目1: 1. Two Sum 1 # Given an array of integers, find two numbers such that they add up to a specific target number.

[152] Maximum Product Subarray

·1 分钟
题目: # Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6.

iOS 远端推送部署详解

·10 分钟
最近几天被iOS的推送部署给搞懵了,现在特地整理下和大家进行分享。 iOS远端推送机制 # APNS,全称为Apple Push Notification service,是苹果通知推送服务中最重要的一环。它是苹果通知推送服务器,为所有iOS设备以及OS X设备提供强大并且可靠的推送通知服务。每个注册通知服务的设备都会和该服务器进行长连接,从而实时获取推送通知。即使当前APP不在运行状态,当通知到达的时候也会有提示发生,最常见的就是短信服务。