2014年3月12日

求比正整数N大的最小正整数M,且M与N的二进制表示中有相同数目的1

摘要: 转自http://blog.csdn.net/ligt0610/article/details/7262757一般最容易想到的方法就是先计算正整数N用二进制表示时1的个数count1,然后不停地计算N++用二进制表示时1的个数count2,直到碰到count1 == count2成立,代码如下:[cpp]typedefunsignedintuint;//解法一:uintcount1Bits(uintn){uintcount=0;while(0!=n){n&=n-1;count++;}returncount;}uintgetNextN_1(uintn){uintcount=count1B 阅读全文

posted @ 2014-03-12 21:30 pengyu2003 阅读(724) 评论(0) 推荐(0)

Set Matrix Zeroes

摘要: Given amxnmatrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up:Did you use extra space?A straight forward solution using O(mn) space is probably a bad idea.A simple improvement uses O(m+n) space, but still not the best solution.Could you de 阅读全文

posted @ 2014-03-12 14:44 pengyu2003 阅读(109) 评论(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"click to show corner cases.Corner Cases:Did you consider the case wherepath="/../"?In this case, you should return&quo 阅读全文

posted @ 2014-03-12 14:23 pengyu2003 阅读(124) 评论(0) 推荐(0)

Minimum Path Sum

摘要: Given amxngrid filled with non-negative numbers, find a path from top left to bottom right whichminimizesthe sum of all numbers along its path.Note:You can only move either down or right at any point in time.简单的动态规划题。刚开始又写成路径和了……class Solution {public: int minPathSum(vector > &grid) { vect... 阅读全文

posted @ 2014-03-12 10:48 pengyu2003 阅读(173) 评论(0) 推荐(0)

Unique Paths II

摘要: 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, 阅读全文

posted @ 2014-03-12 10:39 pengyu2003 阅读(105) 评论(0) 推荐(0)

Rotate List

摘要: Given a list, rotate the list to the right bykplaces, wherekis non-negative.For example:Given1->2->3->4->5->NULLandk=2,return4->5->1->2->3->NULL.题意描述很模糊,经过多次失败的尝试,总算明白大概是啥意思了……/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * Li 阅读全文

posted @ 2014-03-12 09:53 pengyu2003 阅读(103) 评论(0) 推荐(0)

Spiral Matrix II

摘要: Given an integern, generate a square matrix filled with elements from 1 ton2in spiral order.For example,Givenn=3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]写一个回旋的矩阵class Solution {public: vector > generateMatrix(int n) { vector >re; for(int i = 0 ... 阅读全文

posted @ 2014-03-12 09:25 pengyu2003 阅读(142) 评论(0) 推荐(0)

导航