2013年11月26日

Divide Two Integers

摘要: Divide two integers without using multiplication, division and mod operator.思路:直接的想法是用减法代替,但是减法在极端情况下肯定会超时,最多可能会有2^31。然后考虑用移位,对除数左移位,直到将要大于被除数时为止,然后记录当前值,并更新被除数,如此循环。需要注意的是,因为int的范围是-2^32到2^32,所以如果最小的负数取整数就会越界,所以使用unsigned int或者long long代替。代码: 1 int divide(int dividend, int divisor) { 2 ... 阅读全文

posted @ 2013-11-26 23:18 waruzhi 阅读(165) 评论(0) 推荐(0) 编辑

Candy

摘要: There areNchildren standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least one candy.Children with a higher rating get more candies than their neighbors.What is the minimum candies you mu 阅读全文

posted @ 2013-11-26 22:12 waruzhi 阅读(165) 评论(0) 推荐(0) 编辑

Word Break

摘要: 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 @ 2013-11-26 21:30 waruzhi 阅读(187) 评论(0) 推荐(0) 编辑

Longest Valid Parentheses

摘要: Given a string containing just the characters'('and')', find the length of the longest valid (well-formed) parentheses substring.For"(()", the longest valid parentheses substring is"()", which has length = 2.Another example is")()())", where the longest 阅读全文

posted @ 2013-11-26 20:17 waruzhi 阅读(167) 评论(0) 推荐(0) 编辑

Multiply Strings

摘要: 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.思路:大整数乘法,转换成大整数加法来算。代码: 1 string add(string num1, string num2){ 2 int l1 = num1.length(), l2 = num2.length(); 3 if(l1 > l2) 4... 阅读全文

posted @ 2013-11-26 13:17 waruzhi 阅读(274) 评论(0) 推荐(0) 编辑

Sudoku Solver

摘要: Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character'.'.You may assume that there will be on... 阅读全文

posted @ 2013-11-26 11:01 waruzhi 阅读(156) 评论(0) 推荐(0) 编辑

Simplify Path

摘要: Given an absolute path for a file (Unix-style), simplify it.For example,path="/home/", =>"/home"path="/a/./b/../../c/", =>"/c"Corner Cases:Did you consider the case wherepath="/../"?In this case, you should return"/".Another corner ca 阅读全文

posted @ 2013-11-26 00:02 waruzhi 阅读(206) 评论(0) 推荐(0) 编辑

导航