LeetCode
炼丹师
刚入坑计算机视觉的菜鸟
展开
-
LeetCode 2 Keys Keyboard
Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step:Copy All: You can copy all the characters present on the notepad (partial copy ...原创 2019-12-27 16:41:48 · 88 阅读 · 0 评论 -
无聊之作-LeetCode Solve the Equation
Solve a given equation and return the value ofxin the form of string "x=#value". The equation contains only '+', '-' operation, the variablexand its coefficient.If there is no solution for the e...原创 2019-12-25 21:22:13 · 179 阅读 · 0 评论 -
[LeetCode]263. Ugly Number
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly sinc原创 2017-04-20 23:22:20 · 147 阅读 · 0 评论 -
[LeetCode]264. Ugly Number II
Write a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first原创 2017-04-20 23:44:06 · 216 阅读 · 0 评论 -
[LeetCode]268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example,Given nums = [0, 1, 3] return 2.Note:Your algorithm sho原创 2017-04-21 23:32:32 · 140 阅读 · 0 评论 -
[LeedCode]274. H-Index
Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.According to the definition of h-index on Wikipedia: "A原创 2017-04-21 23:57:41 · 153 阅读 · 0 评论 -
[LeetCode]275. H-Index II
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?思路:这个就是给你从小到大排序过的数组了,可以直接用上一个算法解决了。。。让h先等于n(能取到的最大数),然后用nums[n-h]与h相比较,因为原创 2017-04-22 00:04:47 · 145 阅读 · 0 评论 -
[LeetCode]283. Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling you原创 2017-04-22 21:51:25 · 220 阅读 · 0 评论 -
[LeetCode]279. Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n =原创 2017-04-23 00:32:06 · 152 阅读 · 0 评论 -
[LeetCode]287. Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number,原创 2017-04-23 10:56:48 · 128 阅读 · 0 评论 -
[LeetCode]290. Word Pattern
Given a pattern and a string str, find if str follows the same pattern.Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.原创 2017-04-23 17:27:22 · 168 阅读 · 0 评论 -
[LeetCode]292. Nim Game
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the原创 2017-04-23 22:47:15 · 168 阅读 · 0 评论 -
[LeetCode]300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence.For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], ther原创 2017-04-24 22:02:42 · 143 阅读 · 0 评论 -
[数据结构]表达式的计算与转换-C++
学习完栈可以进行表达式的计算与转换本文实现:中缀转前缀中转转后缀根据中缀计算根据后缀计算根据前缀计算等将来复习到树之后,有时间把前缀后缀相互转换,以及前缀后缀转中缀实现ExpressionCalcuate.h:#pragma once#include#include#include using namespace std;原创 2017-05-25 22:54:41 · 714 阅读 · 0 评论 -
[LeetCode]303. Range Sum Query - Immutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Example:Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1sumRange(2, 5) -> -1sumRan原创 2017-04-26 00:19:34 · 156 阅读 · 0 评论 -
[LeetCode]304. Range Sum Query 2D - Immutable
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).The above rectangle (with the red bo原创 2017-04-26 00:21:09 · 190 阅读 · 0 评论 -
[LeetCode]257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are:["1->2->5", "1->3"]原创 2017-04-20 22:38:59 · 134 阅读 · 0 评论 -
[LeetCode]242. Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.思路:很简单,使用两个数组存原创 2017-04-20 22:23:27 · 148 阅读 · 0 评论 -
[LeetCode]235. Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined betw原创 2017-04-18 21:59:22 · 128 阅读 · 0 评论 -
[LeetCode]236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two node原创 2017-04-19 00:29:04 · 203 阅读 · 0 评论 -
[LeetCode]237. Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value原创 2017-04-19 00:40:13 · 144 阅读 · 0 评论 -
[LeetCode]222. Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes.Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely fille原创 2017-04-14 23:31:28 · 245 阅读 · 0 评论 -
[LeetCode]223. Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane.Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.Assume that the tota原创 2017-04-15 00:09:52 · 171 阅读 · 0 评论 -
[LeetCode]224. Basic Calculator
Implement a basic calculator to evaluate a simple expression string.The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and em原创 2017-04-16 09:45:19 · 325 阅读 · 0 评论 -
[LeetCode]226. Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1思路:这个很简单,判断root是否为空,不为空则将左右子节点交换,然后递归进行左右子节点/** * Definition for a bin原创 2017-04-16 10:07:44 · 138 阅读 · 0 评论 -
[LeetCode]225. Implement Stack using Queues
Implement the following operations of a stack using queues.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Return whet原创 2017-04-16 13:21:58 · 130 阅读 · 0 评论 -
[LeetCode]238. Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and in O原创 2017-04-19 23:42:20 · 113 阅读 · 0 评论 -
[LeetCode]239. Sliding Window Maximum
Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window原创 2017-04-20 00:09:45 · 147 阅读 · 0 评论 -
[LeetCode]240. Search a 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted in ascending from left to right.Integers in原创 2017-04-20 00:26:31 · 176 阅读 · 0 评论 -
[LeetCode]228. Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges.For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].思路:很简单的一道题,遍历一遍找到连续的存下就行了public c原创 2017-04-16 22:07:23 · 190 阅读 · 0 评论 -
[LeetCode]230. Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ? k ? BST's total elements.思路:从最小的开始进行从1编号,直到等原创 2017-04-16 22:29:59 · 183 阅读 · 0 评论 -
[LeetCode]229. Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.public class Solution { public List majorit原创 2017-04-16 22:47:02 · 162 阅读 · 0 评论 -
[LeetCode]232. Implement Queue using Stacks
Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.empty(原创 2017-04-16 23:21:07 · 166 阅读 · 0 评论 -
[LeetCode]234. Palindrome Linked List
Given a singly linked list, determine if it is a palindrome.思路:快慢指针,慢指针一次跳一个,快指针一次跳两个,这样快指针跳完慢指针就是中心点这样还是要用到n/2的栈空间,使用反转链表法可以实现1空间/** * Definition for singly-linked list. * public clas原创 2017-04-18 21:45:27 · 125 阅读 · 0 评论