leetcode

LeetCode题解(12)--Integer to Roman

强颜欢笑 提交于 2020-01-14 04:35:05
https://leetcode.com/problems/integer-to-roman/ 原题: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 思路: 掌握罗马数字规则并实现即可。(具体规则见 LeetCode(13):Roman to Integer) 我的AC代码: 1 class Solution { 2 public: 3 string intToRoman(int num) { 4 string in[] = {"I","V","X","L","C","D","M"}; 5 int t,i=0,j=0; 6 string res="",tmp=""; 7 // cout<<(tmp += in[0]+in[1]); 8 while(num>0){ 9 t=num%10; 10 switch (t){ 11 case 0: break; 12 case 1: tmp = in[i]; break; 13 case 2: tmp = in[i]+in[i]; break; 14 case 3: tmp = in[i]+in[i]+in[i]; break; 15 case 4: tmp = in[i]

本周学习小结(13/01 - 19/01)

浪子不回头ぞ 提交于 2020-01-13 09:09:50
LeetCode 本周无进展。需要及时复习。 学习笔记之LeetCode - 浩然119 - 博客园 https://www.cnblogs.com/pegasus923/p/5528520.html Explore - LeetCode - Design https://leetcode.com/explore/interview/card/top-interview-questions-medium/112/design/ Explore - LeetCode - Math https://leetcode.com/explore/interview/card/top-interview-questions-medium/113/math/ 学习笔记之Problem Solving with Algorithms and Data Structures using Python - 浩然119 - 博客园 https://www.cnblogs.com/pegasus923/p/10454395.html C++ / Database / Git / Linux / Python / MISC 本周有进展。 学习笔记之C / C++ - 浩然119 - 博客园 https://www.cnblogs.com/pegasus923/p/10437163.html

LeetCode All in One 题目讲解汇总(持续更新中...)

做~自己de王妃 提交于 2020-01-12 00:51:24
Given n , how many structurally unique BST's (binary search trees) that store values 1 ... n ? Example: Input: 3 Output: 5 Explanation: Given n = 3, there are a total of 5 unique BST's: 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3 这道题实际上是 卡塔兰数 Catalan Numbe 的一个例子,如果对卡塔兰数不熟悉的童鞋可能真不太好做。话说其实我也是今天才知道的好嘛 -.-|||,为啥我以前都不知道捏?!为啥卡塔兰数不像斐波那契数那样人尽皆知呢,是我太孤陋寡闻么?!不过今天知道也不晚,不断的学习新的东西,这才是刷题的意义所在嘛! 好了,废话不多说了,赶紧回到题目上来吧。我们先来看当 n = 1 的情况,只能形成唯一的一棵二叉搜索树,n分别为 1,2,3 的情况如下所示: 1 n = 1 2 1 n = 2 / \ 1 2 1 3 3 2 1 n = 3 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3 就跟斐波那契数列一样,我们把 n = 0 时赋为1,因为空树也算一种二叉搜索树,那么 n = 1

LeetCode.1217-交换芯片(Play with Chips)

梦想与她 提交于 2020-01-11 02:23:04
这是小川的第 421 次更新,第 454 篇原创 看题和准备 今天介绍的是 LeetCode 算法题中 Easy 级别的第 270 题(顺位题号是 1217 )。There are some chips, and the i-th chip is at position chips[i] . You can perform any of the two following types of moves any number of times (possibly zero) on any chip: Move the i-th chip by 2 units to the left or to the right with a cost of 0. Move the i-th chip by 1 unit to the left or to the right with a cost of 1. There can be two or more chips at the same position initially. Return the minimum cost needed to move all the chips to the same position (any position). Example 1: Input: chips = [1,2,3] Output: 1

LeetCode All in One 题目讲解汇总(持续更新中...)

匆匆过客 提交于 2020-01-10 15:29:59
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (<=100). A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not). Example 1: s = "abc" , t = "ahbgdc" Return true . Example 2: s = "axc" , t = "ahbgdc" Return false .

[刷题]整数各位的积和之差

时光怂恿深爱的人放手 提交于 2020-01-10 10:35:59
[leetcode刷题]整数各位的积和之差 重新拾起C++的第一天,也开始重新在leetcode上刷题,第一帖比较简单,记录leetcode上一个简单的问题。 问题描述 给你一个整数 n,请你帮忙计算并返回该整数「各位数字之积」与「各位数字之和」的差。 输入:n = 234 输出:15 解释: 各位数之积 = 2 * 3 * 4 = 24 各位数之和 = 2 + 3 + 4 = 9 结果 = 24 - 9 = 15 实际代码 ```cpp class Solution { public : int subtractProductAndSum ( int n ) { int pro = 1 ; int sum = 0 ; int tmp ; int result ; while ( n != 0 ) { tmp = n % 10 ; pro * = tmp ; sum + = tmp ; n = n / 10 ; } return ( pro - sum ) ; } } ; 简单说明 一个简单的循环。用取余的方法得到一个整数的每一位(从个位开始),每次通过n/=10得到新的整数不断取余,终止条件是n==0。注意考虑整数本身为0的情况。 来源: CSDN 作者: 尚善之若水 链接: https://blog.csdn.net/weixin_40047306/article

LeetCode All in One 题目讲解汇总(持续更新中...)

匆匆过客 提交于 2020-01-08 00:45:32
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 not a palindrome. Note: Have you consider that the string might be empty? This is a good question to ask during an interview. For the purpose of this problem, we define empty string as valid palindrome. 验证回文字符串是比较常见的问题,所谓回文,就是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。但是这里,加入了空格和非字母数字的字符,增加了些难度,但其实原理还是很简单:只需要建立两个指针,left和right, 分别从字符的开头和结尾处开始遍历整个字符串,如果遇到非字母数字的字符就跳过,继续往下找,直到找到下一个字母数字或者结束遍历,如果遇到大写字母,就将其转为小写

动态规划题目(C语言)

百般思念 提交于 2020-01-07 21:59:04
70 . 爬楼梯 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数。 示例 1: 输入: 2 输出: 2 解释: 有两种方法可以爬到楼顶。 1. 1 阶 + 1 阶 2. 2 阶 示例 2: 输入: 3 输出: 3 解释: 有三种方法可以爬到楼顶。 1. 1 阶 + 1 阶 + 1 阶 2. 1 阶 + 2 阶 3. 2 阶 + 1 阶 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/climbing-stairs int climbStairs(int n){ if(n==1)return 1; int dp[n+1]; dp[1]=1; dp[2]=2; for(int i=3;i<n+1;i++){ dp[i]=dp[i-1]+dp[i-2]; } return dp[n]; } 62. 不同路径 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。 问总共有多少条不同的路径? 例如,上图是一个7 x 3 的网格。有多少可能的路径? 说明:m 和 n 的值均不超过 100。 示例 1:

Leetcode solution 226. Invert Binary Tree

喜欢而已 提交于 2020-01-07 12:06:13
Problem Statement Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem was inspired by this original tweet by Max Howell : Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f*** off. Problem link Video Tutorial You can find the detailed video tutorial here Youtube B站 Thought Process Simple question on understanding about recursion. It could be solved using pre-order and post-order traversal style recursion. It can also be solved iteratively using a queue.

VSCode+leetcode 刷题(1)

大兔子大兔子 提交于 2020-01-06 23:35:15
1 vscode 下载安装 vscode官网: https://code.visualstudio.com/ 双击下载后的文件,一直下一步安装。 下一步之前,检查电脑是否安装python环境:Win+R 打开运行,输入CMD 打开CMD运行窗口。输入Python后,回车显示当前计算机的Python版本。如果没有安装,参照 https://www.cnblogs.com/lvtaohome/p/11121377.html 2 注册leetcode账号 点击 https://leetcode-cn.com/ 注册,随便选择下边三个常用的作为第三方登录账号 补充信息,点击个人主页验证邮箱 添加邮箱,并在邮箱上验证成功 重置密码,后边在 vscode 上登录需要密码,这里需要设置一个密码。 记住自己账号(应该就是域名后缀)或者上边的那个验证的邮箱 3 vscode 安装 leetcode 插件 安装插件 安装成功后,会多一个 tab 点击这个tab就切换到了leetcode插件,首先切换地区为中国地区,点击那个“地球”的图标 切换为中国地区,如果不切换会登录不成功哟 点击这“进门”的图标,登录 输入 刚在 leetcode 官网记的 账号(域名后缀)和重置的密码 4开始刷题 选择一个题目点击,右下角再 点击 code now 选择熟悉的语言 这样可以离线写刷题啦 而且还可以提交和测试 来源