随笔分类 - LeetCode
摘要:Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative.思考:大数相乘,模拟算数运算。class Solution {public: string multiply(string num1, string num2) { if(num1=="0"||num2=="0") return "0&qu
阅读全文
摘要:Given a number represented as an array of digits, plus one to the number.思考:大数相加的思想。flag是进位标记。class Solution {public: vector plusOne(vector &digits) { int len=digits.size(); bool flag=false; digits[len-1]+=1; int i=len-1; if(digits[i]>9) flag=true; while(...
阅读全文
摘要:Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100".思考:大数相加的思想。class Solution {public: string addBinary(string a, string b) { if(a.length()==0) return b; if(b.length()==0) return a; int len=a.length()0;i--) { ...
阅读全文
摘要:Given a matrix ofmxnelements (mrows,ncolumns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]You should return[1,2,3,6,9,8,7,4,5].class Solution {public: vector spiralOrder(vector > &matrix) { vector ans; ...
阅读全文
摘要:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array[−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray[4,−1,2,1]has the largest sum =6.思考:最大字串和。class Solution {public: int maxSubArray(int A[], int n) { int last=A[...
阅读全文
摘要:You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?思考:两步,先对角线交换再上下交换。class Solution {public: void rotate(vector > &matrix) { int n=matrix.size(); int i,j; for(i=0;i<n;i++) { for(...
阅读全文
摘要:Givennnon-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.Fo...
阅读全文
摘要:Given an unsorted integer array, find the first missing positive integer.For example,Given[1,2,0]return3,and[3,4,-1,1]return2.Your algorithm should run inO(n) time and uses constant space.思考:不使用额外空间的一种途径就是利用已存在的空间。此题数组中的数放到A数组中对应的位置。class Solution {public: int firstMissingPositive(int A[], int n)...
阅读全文
摘要:Given a collection of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Each number inCmay only be usedoncein the combination.Note:All numbers (including target) will be positive integers.Elements in a combination (a1,a2, … ,ak) must
阅读全文
摘要:Given a set of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Thesamerepeated number may be chosen fromCunlimited number of times.Note:All numbers (including target) will be positive integers.Elements in a combination (a1,a2, … ,ak
阅读全文
摘要:Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode...
阅读全文
摘要:Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keysless thanthe node's key.The right subtree of a node contains only nodes with keysgreater thanthe node's key.Both the left and ri
阅读全文
摘要:Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.An example is the root-to-leaf path1->2->3which represents the number123.Find the total sum of all root-to-leaf numbers.For example, 1 / \ 2 3The root-to-leaf path1->2represents the number12.T
阅读全文
摘要:Given a binary tree, return the inorder traversal of its nodes' values.For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].Note: Recursive solution is trivial, could you do it iteratively?confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on
阅读全文
摘要:Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B aremandnrespectively.思考:3指针。从后往前遍历。class Solution {public: void merge(int A[], int m, int B[], ...
阅读全文
摘要:Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as1and0respectively in the grid.For example,There is one obstacle in the middle of a 3x3 grid as illustrated below.[ [0,0,0], [0,1,
阅读全文
摘要:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array[−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray[4,−1,2,1]has the largest sum =6.click to show more practice.More practice:If you have figured out the O(n) solution, try
阅读全文
摘要:Given an array withnobjects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.Note:You are not suppose to use the l
阅读全文
摘要:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofeverynode never differ by more than 1.思考:求二叉树高的变形,加上判断即可。/** * Definition for binary tree * struct TreeNode { * int val; * ...
阅读全文
摘要: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"isnota palindrome.Note:Have you consider that the string might be empty? This is a good question to
阅读全文