leetcode

[leetcode] 486. Predict the Winner

匿名 (未验证) 提交于 2019-12-03 00:32:02
题目: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins. Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score. Example 1: Input : [ 1 , 5 , 2 ] Output : False Explanation : Initially , player 1 can choose between 1 and 2. If he

LeetCode 107. Binary Tree Level Order Traversal II

匿名 (未验证) 提交于 2019-12-03 00:32:02
LeetCode 107. Binary Tree Level Order Traversal II Solution1:我的答案 比102那道题多了一行代码。。。 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public : vector < vector < int > > levelOrderBottom(TreeNode* root) { vector < vector < int > > res; if (!root) return res; vector < int > temp; queue <TreeNode* > my_que; //队列用来暂时存储二叉树节点 my_que.push(root); int cur = 1 , next = 0 ; while (!my_que.empty()) { temp.push_back(my_que.front()->val); cur--; if (my_que.front()->left)

Leetcode题解 0019期

匿名 (未验证) 提交于 2019-12-03 00:30:01
我要调作息! 题目: 给定两个二进制字符串,返回他们的和(用二进制表示)。 输入为非空字符串且只包含数字 1 和 0。 示例: 输入: a = "11" , b = "1" 输出: "100" 输入: a = "1010" , b = "1011" 输出: "10101" 题目相对严谨 无需注意太多 解题思路: 这题就是进位而已,python3可以尝试使用eval函数来解决,2333,如此简单,不post代码。 或者可以使用python3中int函数可以传入的另一个参数base,直接转化。另外python3的 bin 、 oct 、 hex 这些函数的返回值都属于str类型,所以可以直接slicing。 题目: 给定一个单词数组和一个长度 maxWidth,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本。 你应该使用 “贪心算法” 来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格 ' ' 填充,使得每行恰好有 maxWidth 个字符。 要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配, 则左侧放置的空格数要多于右侧的空格数。 文本的最后一行应为左对齐,且单词之间不插入额外的空格。 说明: 单词是指由非空格字符组成的字符序列。 每个单词的长度大于 0,小于等于 maxWidth。 输入单词数组 words

LeetCode 45. Jump Game II 跳跃游戏II,求最少跳跃次数 (贪心)

匿名 (未验证) 提交于 2019-12-03 00:30:01
https://leetcode.com/problems/jump-game-ii/description/ 给定一个非负整数数组,你最初位于数组的第一个位置。 数组中的每个元素代表你在该位置可以跳跃的最大长度。 你的目标是使用最少的跳跃次数到达数组的最后一个位置。 示例: 输入: [2,3,1,1,4] 输出: 2 解释: 跳到最后一个位置的最小跳跃数是 2。 从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。 说明: 假设你总是可以到达数组的最后一个位置。 题解: 在 跳跃问题1 中求能否到达最后位置,用一个 reach 变量 记录能够到达的最远的下标,每走一步比较更新该值,到达最终位置前,如果当前下标大于reach,说明失败。如果能到达最后,说明成功。 而这道题,需要求的是最少的步数。因此需要添加 count 变量记录最少步数。什么时候 count++ 是问题的关键?答案是当前的位置 i 超过了上一跳所能到达的最远位置。所以需要引入变量 last 记录上一跳可达最远坐标。此题代码在跳跃问题1 代码基础上修改。 class Solution { public int jump( int [] nums) { if (nums == null || nums.length == 0 ) { return - 1 ; } int reach

LeetCode 145. Binary Tree Postorder Traversal

匿名 (未验证) 提交于 2019-12-03 00:30:01
LeetCode 145. Binary Tree Postorder Traversal Solution1:我的答案 二叉树的后序遍历递归版是很简单的,关键是迭代版的代码既难理解又难写! 迭代版链接: https://blog.csdn.net/allenlzcoder/article/details/79837841 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public : vector < int > postorderTraversal(TreeNode* root) { vector < int > res; my_Postordertraversal(root, res); return res; } void my_Postordertraversal(TreeNode* root, vector < int > & res) { if (root) my_Postordertraversal(root->left, res); if

Leetcode――638. Shopping Offers

匿名 (未验证) 提交于 2019-12-03 00:29:01
https://leetcode.com/problems/shopping-offers/description/ Example 1: Input: [2,5], [[3,0,5],[1,2,10]], [3,2] Output: 14 Explanation: There are two kinds of items, A and B. Their prices are 2 a n d 2 a n d 5 respectively. In special offer 1, you can pay 5 f o r 3 A a n d 0 B I n s p e c i a l o f f e r 2 , y o u c a n p a y 5 f o r 3 A a n d 0 B I n s p e c i a l o f f e r 2 , y o u c a n p a y 10 for 1A and 2B. You need to buy 3A and 2B, so you may pay 4 for 2A. Example 2: Input: [2,3,4], [[1,1,0,4],[2,2,1,9]], [1,2,1] Output: 11 Explanation: The price of A is 2 , a n d 2 , a n d 3 for B, 4 f

动态规划之Word Break详解

匿名 (未验证) 提交于 2019-12-03 00:29:01
之前刷LeetCode。碰到了这道题。 一开始没想到要用动态规划来解。 后来看了一下答案给的代码,又仔细研究了一下,发现确实是动态规划。 中途看了很多其他人的博客解释,都没把动态规划讲清楚。 所以干脆就自己来写一份详解吧。 首先,把题目贴上来 s dict s For example, given s "leetcode" , dict ["leet", "code"] . "leetcode" "leet code" . 简单来说,就是s能不能由dict中存储的单词拼出来。 注意了,这里有陷阱。不能简单的从前往后substr递增的检查是否包含在dict中,否则会有问题。 下面开始介绍怎么用动态规划来讲。 该说的都说了,接下来到实际问题里面去体会。 先贴出官网给出的标准答案 bool wordBreak(string s, unordered_set<string> &dict) { vector<bool> flag(s.length()+1,false); flag[0] = true; for (int i = 1; i < s.length()+1; ++i) { for (int j = i - 1; j >= 0; j--) { if (flag[j] && dict.count(s.substr(j, i-j)) != 0) { flag[i] = true;

认证授权那点事儿 ―― OAuth 2.0

匿名 (未验证) 提交于 2019-12-03 00:26:01
OAuth 2.0 ―― 开放授权协议,对应的规范文件 RFC-6749 早在2012年便成形,所以这并不是一个新的技术(你问我为啥研究这个,我也想吟一首诗啊。。。组织上就是这样决定的),但由于其必不可少的价值,在今天的网络上已经得到了广泛的应用。 OAuth2.0认证是要在不同的应用之间打通互信,互信的目的是为了实现一定程度上的用户数据分享,没数据的一方到有数据的一方拿数据,并且在时间尺度上,数据的分享是受控的。 比如你在微信上打开小程序,小程序会向微信索要你的基本信息;比如你用马克飞象作为印象笔记的客户端,马克飞象会向印象笔记索要你的账户信息,以及阅读、创建、删除、修改笔记等的权限;比如你用github账号登录leetcode,后者会去前者索要你的账户信息 …… 在这些场景里都涉及三方:用户、没数据的应用、有数据的应用(授权、资源)。OAuth2.0规范就规定了三方如何交互,完成权限的授予,获取数据的过程。整体逻辑过程如下图 整个授权框架中包含了4种授权模式: 授权码模式 隐式许可模式 密码模式 客户端模式 在详述4种模式之前,首先需要注意,第三方应用需要先到资源持有应用处注册身份,提交回调URI,注册成功后,得到标识身份的 Client ID 和 Client Secret。资源持有者也可以趁这个阶段对第三方应用进行安全审核。 Client Type ,

Leetcode――678. Valid Parenthesis String(匹配括号)

匿名 (未验证) 提交于 2019-12-03 00:22:01
https://leetcode.com/problems/valid-parenthesis-string/description/ 给定一个字符串,字符串中可以有 (、)、* 左括号,有括号和星号,星号可以表示左括号或右括号或空字符。返回该字符串是否可以左右括号完全匹配。 示例一: 递归 class Solution { public boolean checkValidString(String s) { if (s == null ) return true ; return isValid(s, 0 , 0 ); } private boolean isValid(String s, int index , int count ) { if ( count < 0 ) return false ; for ( int i = index ; i < s.length(); i++) { if (s.charAt(i) == '(' ) count ++; else if (s.charAt(i) == ')' ) if ( count <= 0 ) return false ; else count --; else if (s.charAt(i) == '*' ) return isValid(s, i + 1 , count + 1 ) || isValid(s, i

Leetcode――543. Diameter of Binary Tree

匿名 (未验证) 提交于 2019-12-03 00:21:02
https://leetcode.com/problems/diameter-of-binary-tree/description/ Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Example: Given a binary tree 1 / \ 2 3 / \ 4 5 Return 3 , which is the length of the path [ 4 , 2 , 1 , 3 ] or [ 5 , 2 , 1 , 3 ]. Note: The length of path between two nodes is represented by the number of edges between them. 求二叉树中两个节点距离最远的长度。 采用递归。 递归方法体: - 如果树为空,则返回0 - 获取当前节点左子树的深度; - 获取当前节点右子树的深度; -