随笔分类 - LeetCode
Keep track of practicing coding
摘要:Given an array of integers, every element appearsthreetimes except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?no extra memory solution:class Solution {public: int singleNumber(int A[], int n) { ...
阅读全文
摘要:Given an array of integers, every element appearstwiceexcept for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?class Solution {public: int singleNumber(int A[], int n) { int sinNum; for(int i ...
阅读全文
摘要:Given an arraySofnintegers, are there elementsa,b,cinSsuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet (a,b,c) must be in non-descending order. (ie,a≤b≤c)The solution set must not contain duplicate triplets. For example, given array S...
阅读全文
摘要:Given a binary tree, return thepreordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,2,3].Note:Recursive solution is trivial, could you do it iteratively?/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left;...
阅读全文
摘要:Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete at mosttwotransactions.Note:You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).class Solut
阅读全文
摘要:Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at
阅读全文
摘要:Say you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.Solution:class Solution {public: int maxProfit(vector &
阅读全文
摘要:Pow(x, n)Implement pow(x,n). 1 public class Solution { 2 private double power(double x, int n) { 3 if (n == 0) 4 return 1; 5 double v = power(x, n / 2); 6 if (n % 2 == 0) { 7 return v * v; 8 } else { 9 return v * v * x;10 ...
阅读全文
摘要:Given a stringsand a dictionary of wordsdict, determine ifscan be segmented into a space-separated sequence of one or more dictionary words.For example, givens="leetcode",dict=["leet", "code"].Return true because"leetcode"can be segmented as"leet code&quo
阅读全文
摘要:Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:getandset.get(key)- Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(key, value)- Set or insert the value if the key is not
阅读全文
摘要:Givennpoints on a 2D plane, find the maximum number of points that lie on the same straight line.Solution:/** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(int a, int b) : x(a), y(b) {} * }; */class Solution {public: int maxPoints(v...
阅读全文
摘要:Evaluate the value of an arithmetic expression inReverse Polish Notation.Valid operators are+,-,*,/. Each operand may be an integer or another expression.Some examples: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13&qu
阅读全文
摘要:Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are
阅读全文
摘要:Given a binary tree, return thepostordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[3,2,1].Note:Recursive solution is trivial, could you do it iteratively?/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode...
阅读全文
摘要:Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1 / \ 2 3Return6./** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * Tree...
阅读全文
摘要:Problem:Minimum Depth of Binary TreeGiven a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.Solution one based on recursive algrithm. It looks clear, but not optimal .//Solution one /** * Definition
阅读全文