leetcode

【OJ】【Leetcode】【数学】136. 只出现一次的数字

只谈情不闲聊 提交于 2019-12-03 03:01:49
题目 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。 说明: 你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗? 示例 1: 输入: [2,2,1] 输出: 1 示例 2: 输入: [4,1,2,1,2] 输出: 4 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/single-number 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 官方题解 ( 方法4太优秀了,没想到异或还能这么用! ) 方法 4:位操作 概念 如果我们对 0 和二进制位做 XOR 运算,得到的仍然是这个二进制位 a 异或 0 = a 如果我们对相同的二进制位做 XOR 运算,返回的结果是 0 a 异或a = 0 XOR 满足交换律和结合律 a ⊕ b ⊕ a =( a ⊕ a )⊕ b =0⊕ b = b 所以我们只需要将所有的数进行 XOR 操作,得到那个唯一的数字。 class Solution(object): def singleNumber(self, nums): """ :type nums: List[int] :rtype: int """ a = 0 for i in nums: a ^= i return a 复杂度分析 时间复杂度: O

[LeetCode] 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加

自作多情 提交于 2019-12-03 00:56:52
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')' , and in any positions ) so that the resulting parentheses string is valid. Formally, a parentheses string is valid if and only if: It is the empty string, or It can be written as AB ( A concatenated with B ), where A and B are valid strings, or It can be written as (A) , where A is a valid string. Given a parentheses string, return the minimum number of parentheses we must add to make the resulting string valid. Example 1: Input: "())" Output: 1 Example 2: Input: "(((" Output: 3 Example 3: Input

LeetCode 406.Queue Reconstruction by Height

匿名 (未验证) 提交于 2019-12-03 00:43:02
我们先来看题目描述: (h, k) h k h . Write an algorithm to reconstruct the queue. Note : The number of people is less than 1,100. Example: Input: [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]] Output: [[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]] 意思很清楚,以二维数组的形式给出一组数对(h,k),对这组数对重新排列使得对于这个排列中任意一个数对A(h,k),排在A之前的 h>= A.h 的数对数目,与A..k相等。 但是怎么选择数对呢,我们这里可以采用贪心的思想,因为h 比较大的数对需要考虑的数对就越少,又要考虑到k也应该尽量小。所以我们优先选择h尽量大,而且k尽量小的数对。更通俗一点的理解,如果我们没有选择h最大的数对,先考虑了h较小的数据对。那么我们再插入较大的数据对的时候,如果插入到这个比较小的数对之前,我们就要去考虑是否还匹配之前小一些数对的k值。为了避免这种重复,我们选择max(h),min(k)的贪心方法。 确定如何选取数对之后,处理就变得简单了。只需要从前向后遍历,找到应该插入的位置就好了。 缺点就是时间复杂度比较高

LeetCode 872. 叶子相似的树

匿名 (未验证) 提交于 2019-12-03 00:43:02
最近刷LeetCode题目的一些思路,题目信息 叶值序列 (6, 7, 4, 9, 8) 叶相似的 。 root1 root2 true false 提示: 1 100 找到各自树的叶值队列,然后进行比较 1 寻找叶值队列 private void getTreeChild(List<Integer> list, TreeNode root){ if(root.left == null && root.right == null){ list.add(root.val); } else { if(root.left != null){ getTreeChild(list,root.left); } if(root.right != null){ getTreeChild(list,root.right); } } } 2:主方法体,将两个树的叶值队列都找到,然后挨个比较 public boolean leafSimilar(TreeNode root1, TreeNode root2) { boolean retBoolean = false; List<Integer> leftRootList = new ArrayList<>(); List<Integer> rightRootList = new ArrayList<>(); getTreeChild

leetcode 7. 反转整数 C#

匿名 (未验证) 提交于 2019-12-03 00:40:02
给定一个 32 位有符号整数,将整数中的数字进行反转。 示例 1: 输入: 123 输出: 321 示例 2: 输入: - 123 输出: - 321 示例 3: 输入: 120 输出: 21 注意: 31 , 2 31 public class Solution { public int Reverse(int x) { int res = 0; while (x != 0) { int t = res * 10 + x % 10; if ((t - x % 10) / 10 != res) return 0; //不加也行 res = t; x /= 10; } return res;; } } 转载请标明出处: leetcode 7. 反转整数 C# 文章来源: leetcode 7. 反转整数 C#

Leetcode 801. Minimum Swaps To Make Sequences Increasing

匿名 (未验证) 提交于 2019-12-03 00:39:02
思路1: 暴力解。dfs,每次如果A[i] > A[i - 1]并且B[i] > B[i - 1],则继续调用;如果A[i] > B[i - 1] 并且 B[i] > A[i - 1] 则说明交换后也可以满足条件,交换然后继续调用。不断update global variable。会TLE。 class Solution { TLE int res = Integer . MAX_VALUE ; public int minSwap ( int [] A , int [] B ) { helper ( 1 , A , B , 0 ); return res ; } private void helper ( int s , int [] A , int [] B , int count ) { if ( s == A . length ) { res = Math . min ( res , count ); return ; } if ( A [ s ] >= B [ s - 1 ] && B [ s ] >= A [ s - 1 ]) { swap ( A , B , s ); helper ( s + 1 , A , B , count + 1 ); swap ( A , B , s ); } if ( A [ s ] > A [ s - 1 ] && B [ s ] > B

[LeetCode]34. Search for a Range

匿名 (未验证) 提交于 2019-12-03 00:39:02
34. Search for a Range https://leetcode.com/problems/search-for-a-range/description/ 与上一题很相似,原理很清晰,两次二分搜索,一次找前边界一次找后边界,不必在找到target后直接break。但我真的是太讨厌二分搜索了,边界条件实在头疼。 class Solution { public : vector < int > searchRange( vector < int > & nums, int target) { int l = 0 , r = nums.size()- 1 ; vector < int > res( 2 ,- 1 ); if (nums.empty()) return res; if (nums.size()== 1 && nums[ 0 ] == target) return ( vector < int > ){ 0 , 0 }; while (l < r){ int mid = (l+r)/ 2 ; if (nums[mid]<target) l = mid+ 1 ; else r = mid; } if (nums[r]!=target) return res; res[ 0 ] = r; r = nums.size(); ///!!! while (l < r){

LeetCode第6题:将字符串 &quot;PAYPALISHIRING&quot; 以Z字形排列成给定的行数

匿名 (未验证) 提交于 2019-12-03 00:39:02
public class LeetCode6 { public static void main(String[] args) { /** * LeetCode 第6题:将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数: P A H N A P L S I I G Y I R 之后从左往右,逐行读取字符:"PAHNAPLSIIGYIR" 样例: 输入: s = "PAYPALISHIRING", numRows = 4 输出: "PINALSIGYAHRPI" 解释: P I N A L S I G Y A H R P I *解题思路:要得到解释后的二维数组 conStr[numRows][len]; len未知? * 找规律, len = s.length()/ (2*numRows - 2) * (numRows - 1); * 解释一下将一个有完整列的如:PAYP这一列到ISHI这一列,有 numRows - 2个字符,加上完整的一列则为 2*numRows -2个字符 * 而这种规律递进的行长为 numRows - 1, 当然 len != s.length() / 2; 计算机运算取整!! * 当然还有不规律的,如最后的NG这一列,其判读 otherstr = s.length()% (2*numRows - 2) *

动态规划Leetcode 746 Min Cost Climbing Stairs

匿名 (未验证) 提交于 2019-12-03 00:38:01
Leetcode 746 Min Cost Climbing Stairs Problem Description: 爬楼梯:一次可以爬1-2层楼梯,每爬一层楼梯会消耗对应的能量,求出爬到最高一层的楼梯时所消耗的最少能量。 具体的题目信息: https://leetcode.com/problems/min-cost-climbing-stairs/description/ Example: Solution: class Solution { public : int minCostClimbingStairs ( vector < int > & cost ) { if ( cost . size () == 2 ) return cost [ 1 ]; int a = 0 , b = 0 ; for ( int i = 0 ; i < cost . size (); i ++) { int t = min ( a , b )+ cost [ i ]; a = b ; b = t ; } return min ( a , b ); } }; 转载请标明出处: 动态规划Leetcode 746 Min Cost Climbing Stairs 文章来源: 动态规划Leetcode 746 Min Cost Climbing Stairs

Leetcode 4 - Median of Two Sorted Arrays

匿名 (未验证) 提交于 2019-12-03 00:37:01
原题: nums1 nums2 Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 大致题意就是: 对于两个排好序的数组nums1和nums2,size分别为m和n,找到两个数组合并后的中位数。要求时间复杂度在O(log(m + n)) 其中要注意的是:如果数组size和是偶数的话则中位数是中间两个数的平均值,且函数返回值为double。 这个题见过两个思路,一个是经典的findKth,九章上有解答,复杂度是 O ( l o g ( r a n g e ) ( l o g ( n ) + l o g ( m ) ) ) : https://www.jiuzhang.com/solution/median-of-two-sorted-arrays/ 下面这个思路是top solution里面的一个方法,本质也是二分法。不失一般性,先假设数组nums1长度小于等于nums2,且总长度为奇数。如果找到了中位数nums1[i - 1]或nums2[j - 1],根据定义可知nums1和nums2一定会分别被分为如下情况: 使得nums1[i - 1] <= nums2[j] && nums2[j - 1] <= nums1[i], 且i + j =