leetcode 【LeetCode】205. Isomorphic Strings 问题描述 Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must
leetcode 【LeetCode】204. Count Primes 问题描述 Description: Count the number of prime numbers less than a non-negative number, n. 点击查看问题 [https://leetcode.com/problems/count-primes/] 问题分析 筛选法求素数,参考这篇文章【算法】普通方法和筛选法求素数 [http://www.zgljl2012.com/suan-fa-pu-tong-fang-fa-he-shai-xuan-fa-qiu-su-shu/] ,这道题用普通方法求素数会超时,只能用筛选法。 代码
leetcode 【LeetCode】206. Reverse Linked List 问题描述 Reverse a singly linked list. 问题分析 链表反转问题。从第二个节点开始,不断把next节点加到head前面。 代码 class Solution { public: ListNode* reverseList(ListNode* head) { ListNode* p = head; ListNode* q = 0; if(head==0) return 0; while(p->next) { q
leetcode 【LeetCode】203. Remove Linked List Elements 问题描述 Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 Return: 1
leetcode 【LeetCode】202. Happy Number 问题描述 Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum
leetcode 【LeetCode】198. House Robber 问题描述 You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is
leetcode 【LeetCode】190. Reverse Bits 问题描述 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000). Follow up: If this function is
leetcode 【LeetCode】189. Rotate Array 问题描述 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to
leetcode 【LeetCode】172. Factorial Trailing Zeroes 问题描述 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 阶乘末尾0的个数 分析 偶数乘以5末尾才会出现0,计算1-n出现含有5的个数即可。 代码 class Solution { public: int trailingZeroes(int
leetcode 【LeetCode】171. Excel Sheet Column Number 问题分析 Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C ->
leetcode 【LeetCode】169. Majority Element 问题描述 Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and
leetcode 【LeetCode】168. Excel Sheet Column Title 问题描述 Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA
leetcode 【LeetCode】165. Compare Version Numbers 问题描述 Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only
leetcode 【LeetCode】160. Intersection of Two Linked Lists 问题描述 Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1
leetcode 【LeetCode】155. Min Stack 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. * push(x) -- Push element x onto stack. * pop() -- Removes the element on top of
leetcode 【LeetCode】141. Linked List Cycle 问题描述 Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 代码 class Solution { public: bool hasCycle(ListNode *head) { ListNode* p
leetcode 【LeetCode】136. Single Number 问题描述 Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra
leetcode 【LeetCode】11. 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,
leetcode 【LeetCode】125. Valid Palindrome 问题描述 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is
leetcode 【LeetCode】121. Best Time to Buy and Sell Stock 问题描述 Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie,
leetcode 【LeetCode】119. Pascal's Triangle II 问题描述 Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(
leetcode 【LeetCode】118. Pascal's Triangle 问题描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 分析 设f(
leetcode 【LeetCode】112. Path Sum 问题描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given
leetcode 【LeetCode】111. Minimum Depth of Binary Tree 问题描述 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 二叉树最短路径问题。 问题分析
leetcode 【LeetCode】110. Balanced Binary Tree 问题描述 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every