leetcode 【LeetCode】219. 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
leetcode 【LeetCode】217. Contains Duplicate 问题描述 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
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/] ,这道题用普通方法求素数会超时,只能用筛选法。 代码
算法 【算法】普通方法和筛选法求素数 素数指的是因子只有1和本身的数(1不是素数),求解素数在数学上应用非常广泛,而求解n以内的素数也是我们编程时常遇到的问题,在这个问题上,筛选法求解素数运行得非常快。下面首先介绍如何判断一个是不是素数,然后介绍用普通方法求n以内的素数,接着是筛选法求n以内的素数,最后是两种算法的运行时间比较 判断一个数是不是素数 算法思想:判断小于等于一个数的平方的所有大于1的整数是不是能整除这个数,如果能,则表明这个数不是素数;反之,则是素数。 //判断一个数是否为素数 bool isPlain(int value){ int m = sqrt(value); if (value < 2) return false; for (int
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
算法 常用算法思想 1. 分治法 将复杂问题分解成两个或多个子问题,再将子问题又分为两个或多个更小的子问题……直到到达了有解的基本问题。最后,将子问题的解合并成原文题的解。如排序算法、傅里叶变换等。 2. 动态规划 每次决策依赖于当前的状态,然后会引起状态的转移。一个决策序列就是在变化的状态中产生出来的,这种多阶段最优化决策解决问题的过程就称之为动态规划。如最短路径等。 3. 贪心算法 求解问题时,不考虑全局情况,只贪心于当前的最好的解,所做出的仅仅是某种意义上局部最优解。如Prim算法等。 4. 回溯算法 回溯算法实际上是一个类似枚举的搜索尝试过程,主要是在搜索尝试过程中寻找问题的解,当发现已不满足求解条件时,就“回溯”返回到上一步,尝试别的路径。如八皇后问题。
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(