Leetcode 题目整理-8 Count and Say】的更多相关文章

38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as "one 2, then one 1&quo…
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Note: Given n will be a positive integer. 对这种题目开始没有什么思路,借鉴博客http://blog.csdn.net/ken…
9. Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the res…
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + n…
368. Largest Divisible Subset 题意:找到所有元素都不同的数组中满足以下规则的最大子集,规则为:子集中的任意两个元素a和b,满足a%b=0或者b%a=0. 解答:利用动态规划法求解.先给数组排好序.定义dp[i]表示以nums[i]结尾的子集的大小,则dp[i+1]=dp[j]+1,if nums[i+1]%nums[j]==0, 0<=j<=i,或者dp[i+1]=1.时间复杂度为O(n2). 代码如下: public List<Integer> la…
Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt(7)这种情况,思路一就是应用二分法进行查找.每次给出中间值,然后比对cur的平方与目标值的大小.需要先设定两个变量用来存放左右游标. 这里要考虑一下整型溢出的问题,另外,即使不能开出整数的也要近似给出整数部分,不能忽略. 代码如下: int Solution::mySqrt(int x) { //…
27. Remove Element Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be changed.…
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the…
20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]&quo…
14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. 注:这题竟然连个示例都没有,说明特殊情况并不多,就是要找出所有字符串的最长公共前缀.他应该不会超过所有字符串中最短的那个,可以试着找出最短长度n,然后每个都读取和比较n个,并根据情况不断减小那个n.\ ) { return ""; } int n{int(strs…
今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if…
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + n…
2016-09-24,开始刷leetcode上的算法题,下面整理一下leetcode题目清单.Github-leetcode 1.基本数学 Two Sum Palindrome Number Container With Most Water (数学思想) 2.基本数据结构 3.字符串操作 Longest Substring Without Repeating Characters(滑动窗口) ZigZag Conversion Reverse Integer(整数与字符串之间的转换) Stri…
LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复杂度上根本就不是最优解,有的写的太啰嗦,有的则用了一些过于tricky的方法.我没有为了这个再更新,就让它们去吧. LeetCode最近很火,我以前不太知道有这么一个很方便练习算法的网站,直到大概数周前同事和我说起,正好我老婆要找工作,而根据同事的理论,LeetCode的题目是必须攻破的第一道关卡.…
目录 Leetcode题目解答 1. 删除最外层的括号 2. 两数之和 3. 宝石与石头 4. 移除元素 5.删除排序数组中的重复项 6.寻找两个有序数组的中位数 7.盛最多水的容器 8.存在重复元素 Leetcode题目解答 1. 删除最外层的括号 有效括号字符串为空("")."(" + A + ")" 或 A + B,其中 A 和 B 都是有效的括号字符串,+ 代表字符串的连接.例如,"","()",&…
题目标签:HashMap 题目给了我们一组域名,让我们把每一个域名,包括它的子域名,计数. 遍历每一个域名,取得它的计数,然后把它的所有子域名和它自己,存入hashmap,域名作为key,计数作为value. 最后遍历keyset,把计数和域名重新组合加入result,具体请看code. Java Solution: Runtime: 7 ms, faster than 99.54% Memory Usage: 36.4 MB, less than 99.04% 完成日期:03/15/2019…
面试算法题 dfs相关 全排列 #include<bits/stdc++.h> using namespace std; const int N = 10; //用一个path数组来存储每次到底层的路径 int path[N]; //用一个布尔数组来存储每次已经遍历的点,默认是false bool st[N]; int n; //u表示当前的层数 void dfs(int u) { //当已经到达最底层了,溯回并输出路径 if( u == n ) { for(int i = 0 ; i <…
LeetCode 题目总结/分类 利用堆栈: http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ http://oj.leetcode.com/problems/longest-valid-parentheses/ (也可以用一维数组,贪心) http://oj.leetcode.com/problems/valid-parentheses/ http://oj.leetcode.com/problems/large…
题目: Description: Count the number of prime numbers less than a non-negative number, n. 思路: 题意:求小于给定非负数n的质数个数 西元前250年,希腊数学家厄拉多塞(Eeatosthese)想到了一个非常美妙的质数筛法,减少了逐一检查每个数的的步骤,可以比较简单的从一大堆数字之中,筛选出质数来,这方法被称作厄拉多塞筛法(Sieve of Eeatosthese). 具体操作:先将 2~n 的各个数放入表中,然…
一道LeetCode题目 今天刷一道LeetCode的题目,要求是这样的: Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should ret…
Noip往年题目整理 张炳琪 一.历年题目 按时间倒序排序 年份 T1知识点 T2知识点 T3知识点 得分 总体 2016day1 模拟 Lca,树上差分 期望dp 144 挺难的一套题目,偏思维难度,代码实现到没有多麻烦除了一个很长的转移方程 2016day2 杨辉三角 组合数 模拟,思路 状压dp 160 2015day1 模拟 Tarjan,拓扑 Dfs,贪心或dp 180 难度比去年要高,第二天的难度较大,dp和lca较难写,不过都有部分分 2015day2 二分答案 Dp Lca树上差…
NOIp初赛题目整理 这个 blog 用来整理扶苏准备第一轮 csp 时所做的与 csp 没 有 关 系 的历年 noip-J/S 初赛题目,记录了一些我从不知道的细碎知识点,还有一些憨憨题目,不定期更新. 1.(07senior,5) 在C 语言中,表达式 \(23~\mid~2~\land^~5\) 的值是( ) ​ A. \(23\) B.\(1\) C.\(18\) D.\(32\) E.\(24\) Answer:A Solution:这题一看就是要考运算符优先级来着.在 \(C\)…
LeetCode题目总结-滑动窗口法 滑动窗口法:此方法首先建立一个长度为零的窗口,把右侧窗口向右移动,当新的元素与原来窗口中的元素不重复时,把新的元素加入其中,并更新窗口长度:当新的元素与原集合中的元素重复时,把窗口的左侧向右移动,直至原窗口中的元素不含新的元素. 3.  无重复字符的最长子串 class Solution: def lengthOfLongestSubstring(self, s: str) -> int: if not s: return 0 start, end = 0,…
一.数组 8) 双指针 ---- 滑动窗口 例题: 3. Longest Substring Without Repeating Characters 描述:Given a string, find the length of the longest substring without repeating characters. 题解:时间:92.67%,空间:87.02% public int lengthOfLongestSubstring(String s) { // check if(s…
3.无重复字符的最长子串 /** * @param {string} s * @return {number} */ var lengthOfLongestSubstring = function(s) { var ans = [], vis = [], max = 0; for(var i = 0; i < 256; i++){ vis.push(-1); } for(var i = 0; i < s.length; i++){ var t = s[i].charCodeAt() - 0;…
题目要求 A website domain like "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com", and at the lowest level, "discuss.leetcode.com". When we…
bzoj 500题纪念 总结一发题目吧,挑几道题整理一下,(方便拖板子) 1039:每条线段与前一条线段之间的长度的比例和夹角不会因平移.旋转.放缩而改变,所以将每条轨迹改为比例和夹角的序列,复制一份翻转后的序列,直接上AC自动机即可.注意特判 1125:hash+splay 1183:digit-product只可能是2,3,5,7的积,枚举digit-product进行dp即可 1301:每个点和每个边只能被删除一次,随便搞 1313:上下界最大流 1471:考虑容斥,枚举两条路径相交的位置…
(记忆线:当时一刷完是1-205. 二刷88道.下次更新记得标记不能bug-free的原因.)   88-------------Perfect Squares(完美平方数.给一个整数,求出用平方数来相加得到最小的个数) public class Solution{ public static void main(String[] args){ System.out.println(numSquares(8)); } public static int numSquares(int n){ //…
图论的常见题目有两类,一类是求两点间最短距离,另一类是拓扑排序,两种写起来都很烦. 求最短路径: 127. Word Ladder Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that: Only one letter can be…
94. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree [1,null,2,3], 1 \ 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 非递归前序遍历:…