一、给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。如果节点总数不是 k 的整数倍,那么请将最后剩余的节点也翻转顺序。 1. 非递归解法 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) {
问题描述: Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have
问题描述: Write a function to find the longest common prefix string amongst an array of strings. 问题分析: 我们只需要从头到尾把每个字符串共同的字符前缀找出即可。 详见代码: class Solution {public:string longestCommonPrefix(vec
问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 问题分析: 我们根据罗马数的特点,从后往前循环字符串,若i代表的值小于i+1代表的值,sum值减小,否则就增加。 详见代码: class Solutio
问题描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 问题分析: 本题目目的是将一个整数转换为罗马数字的表现形式。我们可以将能用的到罗马数字存到数组里,然后再调用。 详见代码: class Solut
问题描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Fi
问题描述: Implement regular expression matching with support for '.' and '*'. 示例: '.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the ent
问题描述: Determine whether an integer is a palindrome. Do this without extra space. 求解一个数是不是回文数。 问题求解思路: 1.负数不是回文数 2.回文数正着读和反着读一样,故可将该数反转与原数比较大小 我的代码如下: class Solution {public:bool isPali
问题描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible i
问题描述: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H NA P
问题描述: Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return false. Note: You may a
问题描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. 示例: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit
问题描述: Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly
问题描述: Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node wit
问题分析: Given a binary tree, return all root-to-leaf paths. 示例: given the following binary tree: 1/ \2 3\5 All root-to-leaf paths are: ["1->2->5", "1->3"] 问题分析: 这里涉及到将整数转换为字符串的问题,还好新
问题描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element
问题描述: Given an integer, write a function to determine if it is a power of two. 问题分析: 每一个2的幂,都是大于0的整数,且转换成二进制时,里面1的个数只有一个。 过程详见代码: class Solution {public:bool isPowerOfTwo(int n) {int cou
问题描述: Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at m
问题描述: In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data. You're given a matrix represented by a