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 constit…
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". 题解:用栈就行了 代码: class Solution { public: void reverseWords(string &s) { stack<string>st; string temp = &qu…
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题:设定一个变量sum存放反转后的答案,每次取输入x的最后一位n,并用sum = sum*10+n更新sum. 例如题设的数字123,初始sum为0,过程如下: 取末尾的3,sum=3,x=12 取末尾的2,sum=32,x=1 取末尾的1,sum=321,x=0 特别注意x一开始就是0的处理,直接返回0就可以了.…
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod…
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt…
题目: 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…
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 =…
题目: Toeplitz Matrix A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now given an M x N matrix, return True if and only if the matrix is Toeplitz. Example 1: Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]Out…
昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日截止,共有204人参加,根据投票结果显示,大多数人希望通过刷LeetCode能够提高代码水平,在工作中和面试中有帮助,然而有一半人没有接触过Leetcode,一半人接触过,但只刷了一点.几乎所有投票的人刷题的数量都小于100题,说明大家对刷题的认识和热情都还比较浅,可能是因为刷题比较枯燥,又不能马上…
本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历.矩阵位置的旋转.矩阵行或列次序的交换.空间复杂度为O(1)等.本期共12道题,2道简单题,8道中等题,2道困难题. 例1是杨辉三角的一个延申题,是一道非常经典的矩阵习题,本题理想解法是动态规划,但是也可以采用递归来求解. 例2是一道顺时针访问矩阵元素的习题,在不少面试题中有见到. 例3.例4和例5则强调如果…