解法专题

力扣刷题 杨辉三角(使用c++ vector解法)

杨辉三角 题目描述示例1示例2提示:代码 题目描述 给定一个非负整数 numRows,生成「杨辉三角」的前 numRows 行。 在「杨辉三角」中,每个数是它左上方和右上方的数的和。 示例1 输入: numRows = 5 输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]] 示例2 输入: numRows = 1

【java编程(在线笔试)】【链表】两道k个一组翻转链表题目(包含非递归和递归两种解法)

一、给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。如果节点总数不是 k 的整数倍,那么请将最后剩余的节点也翻转顺序。 1. 非递归解法 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) {

每天刷个算法题20160525:快速排序的递归转非递归解法

版权所有。所有权利保留。 欢迎转载,转载时请注明出处: http://blog.csdn.net/xiaofei_it/article/details/51524798 为了防止思维僵化,每天刷个算法题。已经刷了几天了,现在发点代码。 我已经建了一个开源项目,每天的题目都在里面: https://github.com/Xiaofei-it/Algorithms

每天刷个算法题20160524:阿克曼函数的递归转非递归解法

版权所有。所有权利保留。 欢迎转载,转载时请注明出处: http://blog.csdn.net/xiaofei_it/article/details/51524754 为了防止思维僵化,每天刷个

LeetCode第21题之Generate Parentheses(两种解法)

C++代码: 解法一(在LeetCode上运行效率高于解法二): #include <vector>#include <iostream>#include <string>using namespace std;class Solution {private:vector<string> res;public: //leftRemain保存还可以放左括号的数目,rightRemain

反转链表的解法分享

1、双指针法 ListNode* addInList(ListNode* head1, ListNode* head2) {// write code hereListNode* ReverseList(ListNode* pHead){if(pHead == NULL)return NULL;ListNode* cur = pHead;ListNode* pre =NULL;while(cu

3Sum Closest问题及解法

问题描述: 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

Longest Common Prefix问题及解法

问题描述: Write a function to find the longest common prefix string amongst an array of strings. 问题分析: 我们只需要从头到尾把每个字符串共同的字符前缀找出即可。 详见代码: class Solution {public:string longestCommonPrefix(vec

Roman to Integer问题及解法

问题描述: 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

Integer to Roman问题及解法

问题描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 问题分析: 本题目目的是将一个整数转换为罗马数字的表现形式。我们可以将能用的到罗马数字存到数组里,然后再调用。 详见代码: class Solut

Container With Most Water问题及解法

问题描述: 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

Regular Expression Matching问题及解法

问题描述: 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

Palindrome Number问题及解法

问题描述: Determine whether an integer is a palindrome. Do this without extra space. 求解一个数是不是回文数。 问题求解思路: 1.负数不是回文数 2.回文数正着读和反着读一样,故可将该数反转与原数比较大小 我的代码如下: class Solution {public:bool isPali

String to Integer (atoi)问题及解法

问题描述: 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

ZigZag Conversion题目及解法

问题描述: 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

Reverse Integer问题及解法

问题描述: Reverse digits of an integer. 示例: Example1: x = 123, return 321 Example2: x = -123, return -321 话不多说,代码很清楚 class Solution {public:int reverse(int x) {long long rev = 0;while(x != 0){r

Valid Anagram问题及解法

问题描述: 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

Add Digits问题及解法

问题描述: 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

Ugly Number问题及解法

问题描述: 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

Delete Node in a Linked List问题及解法

问题描述: 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

Binary Tree Paths问题及解法

问题分析: 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 Queue using Stacks问题及解法

问题描述: 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

Power of Two问题及解法

问题描述: 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

Invert Binary Tree问题及解法

问题描述: Invert a binary tree. 4/ \2 7/ \ / \1 3 6 9to 4/ \7 2/ \ / \9 6 3 1 过程详见代码: /*** Definition for a binary tree node.* struct TreeNode {* int val;

Contains Duplicate II问题及解法

问题描述: 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

Reshape the Matrix问题及解法

问题描述: 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