摘要: 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 (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.class Solution {public: void ... 阅读全文
posted @ 2014-03-06 21:08 andyqee 阅读(103) 评论(0) 推荐(0) 编辑
摘要: 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) { ... 阅读全文
posted @ 2014-03-06 17:13 andyqee 阅读(136) 评论(0) 推荐(0) 编辑
摘要: 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 ... 阅读全文
posted @ 2014-03-06 15:35 andyqee 阅读(78) 评论(0) 推荐(0) 编辑
摘要: 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... 阅读全文
posted @ 2014-03-05 22:06 andyqee 阅读(102) 评论(0) 推荐(0) 编辑
摘要: 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;... 阅读全文
posted @ 2014-03-05 21:18 andyqee 阅读(110) 评论(0) 推荐(0) 编辑
摘要: 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 阅读全文
posted @ 2014-03-03 16:31 andyqee 阅读(158) 评论(0) 推荐(0) 编辑
摘要: 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 阅读全文
posted @ 2014-03-03 14:43 andyqee 阅读(96) 评论(0) 推荐(0) 编辑
摘要: 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 & 阅读全文
posted @ 2014-03-03 12:30 andyqee 阅读(124) 评论(0) 推荐(0) 编辑
摘要: 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 ... 阅读全文
posted @ 2014-03-02 20:42 andyqee 阅读(91) 评论(0) 推荐(0) 编辑
摘要: 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 阅读全文
posted @ 2014-03-01 16:06 andyqee 阅读(157) 评论(0) 推荐(0) 编辑