• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
UsSam
博客园 | 首页 | 新随笔 | 新文章 | 联系 | 订阅 订阅 | 管理

2014年3月25日

【Leetcode】Merge Sorted Array
摘要: 题目: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 tom+n) to hold additional elements from B. The number of elements initialized in A and B aremandnrespectively.解题思路1:设置一个数组C,依次取A的元素i与B的元素j比较,如果B[j 阅读全文
posted @ 2014-03-25 18:59 UsSam 阅读(118) 评论(0) 推荐(0)
 
【Leetcode】Sum Root to Leaf Numbers
摘要: 题目: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 number1 阅读全文
posted @ 2014-03-25 18:13 UsSam 阅读(105) 评论(0) 推荐(0)
 
【Leetcode】Path Sum II
摘要: 题目:Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1return[ [5,4,11... 阅读全文
posted @ 2014-03-25 16:42 UsSam 阅读(107) 评论(0) 推荐(0)
 
【Leetcode】Convert Sorted Array to Binary Search Tree
摘要: 题目:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.解题思路:要建立平衡二叉查找树,只需要采用二分法,将每段数组的中间节点的值存入树节点即可,中间节点左右两段的数组的中间结点又作为该树节点的左右子树节点。代码:/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * Tr... 阅读全文
posted @ 2014-03-25 16:11 UsSam 阅读(159) 评论(0) 推荐(0)
 
【Leetcode】Valid Parentheses
摘要: 题目:Given a string containing just the characters'(',')','{','}','['and']', determine if the input string is valid.The brackets must close in the correct order,"()"and"()[]{}"are all valid but"(]"and"([)]"are not. 阅读全文
posted @ 2014-03-25 11:06 UsSam 阅读(117) 评论(0) 推荐(0)
 
 

2014年3月24日

【Leetcode】Count and Say
摘要: 题目:The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1is read off as"one 1"or11.11is read off as"two 1s"or21.21is read off as"one 2, thenone 1"or1211.Given an integern, generate thenthsequence.Note: The sequence of i 阅读全文
posted @ 2014-03-24 19:46 UsSam 阅读(129) 评论(0) 推荐(0)
 
【Leetcode】Roman to Integer
摘要: 题目:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.解题思路:我们首先来明确一下罗马数字与阿拉伯数字的换算规则:如果当前数字比前一个大,说明这一段的值应该是当前这个值减去上一个值,比如IV = 5 – 1;否则,将当前值加入到结果中,然后开始下一段记录,比如VI = 5 + 1, II=1+1。而罗马数字与阿拉伯数字对应变换是:I对应1,V对应5,X对应10,L对应50,C对应100,D对应500,M对应1000。因此,只需要从前 阅读全文
posted @ 2014-03-24 19:08 UsSam 阅读(108) 评论(0) 推荐(0)
 
【Leetcode】Longest Common Prefix
摘要: 题目:Write a function to find the longest common prefix string amongst an array of strings.解题思路:先找出最短长度的字符串,然后将所有字符串与最短长度的字符串做比较,遇到不同的字符就返回最短长度字符串的前一部分子串。代码:class Solution {public: string longestCommonPrefix(vector &strs) { int MinSize=INT_MAX; int MinIndex=0; if(strs.empty())r... 阅读全文
posted @ 2014-03-24 16:36 UsSam 阅读(141) 评论(0) 推荐(0)
 
【Leetcode】Add Binary
摘要: 题目:Given two binary strings, return their sum (also a binary string).For example,a ="11"b ="1"Return"100".解题思路1:判断当前字符所表示的数字,产生输出和进位。缺点:程序比较复杂。代码1:class Solution {public: string addBinary(string a, string b) { char carry='0'; string c; if(a.empty())return b; if( 阅读全文
posted @ 2014-03-24 16:06 UsSam 阅读(109) 评论(0) 推荐(0)
 
【Leetcode】String to Integer
摘要: 题目:Implementatoito convert a string to an integer.Hint:Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.Notes:It is intended for this problem to be specified vaguely (ie, no given input specs). You are re 阅读全文
posted @ 2014-03-24 11:06 UsSam 阅读(123) 评论(0) 推荐(0)
 
 
下一页

公告


博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3