1.题目描述 返回一个 string中最后一个单词的长度.单词定义为没有空格的连续的字符,比如 ‘a’,'akkk'. 2.问题分析 从后向前扫描,如果string是以空格‘  ’结尾的,就不用计数,现将空格最后的空格排除掉,直到找到最后一个出现的字符.从最后一个出现的字符开始计数,一直到下一次出现空格‘ ’. 特殊情况是 输入的字符串是空的,先排除掉. 3.代码 int lengthOfLastWord(string s) { ) // 排除字符长度为 0 的情况 ; ; ; ) // 排除掉…
1.问题描述 Reverse Vowels of a String Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Given s = "hello", return "holle". Example 2: Given s = "leetcode", return "leotcede&quo…
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 char…
题目描述: 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点. 现有一个链表 -- head = [4,5,1,9],它可以表示为: 示例 1: 输入: head = [4,5,1,9], node = 5 输出: [4,1,9] 解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9. 示例 2: 输入: head = [4,5,1,9], node = 1 输出: [4,5,9] 解释: 给定你链表…
1.问题描述 Given an array of integers sorted in ascending order, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]…
1.问题描述 Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. 给定一个排好序的数组和一个数,找出这个数在有…
[题目1] Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 判断是否存在重复数字 [思路] 1.想复杂了,用了HashSet/…
1.题目描述 2.问题分析 直接旋转字符串A,然后做比较即可. 3.代码 bool rotateString(string A, string B) { if( A.size() != B.size() ) return false; if( A.empty() && B.empty() ) return true; ; while( i < A.size() ){ ] ; A += c; A.erase( A.begin() ); if( A == B ) return true;…
leetcode: 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…
29.Divide Two Integers 题目 题目要求不用乘除和取模运算,实现两个整数相除: 我的第一想法就是把除法变成减法来做,这也是最初除法的定义,其实现代码如下: class Solution { public: int divide(int dividend, int divisor) { == divisor) return dividend; == divisor) -dividend; bool flag = false; &&divisor>)) { divid…