
编程之美
文章平均质量分 77
Yadoer
这个作者很懒,什么都没留下…
展开
-
编程之美-求二进制1的个数
一个在常见的题目,但是看到编程之美的时候才发现,方法真多,今天来总结一下:解法一可以举出一个八位的二进制例子来进行分析。对于一个二进制操作,我们知道,除以一个2,原来的数字将会减少一个0,如果除的过程中有余,那么就表示当前位置有一个1.以10 100 010为例:第一次除以2时,商为 1 010 001,余为 0。第二次除以2时,商为 101 000,余为1。因此可以根据整原创 2015-01-30 10:20:35 · 1098 阅读 · 0 评论 -
编程之美-不要被阶乘吓到
题目1、给定一个整数N,那么阶乘N!末尾有多少个0呢?2、求N!的二进制表示中最低位1的位置?先来看怎么计算阶乘,当然可以是循环,也可以是递归,上代码:public long factorial1(int n) { long sum = 1; for (int i = 1; i <= n; i++) { sum *= i; } r原创 2015-01-30 12:53:52 · 1056 阅读 · 0 评论 -
二叉树遍历(前序、中序、后序、层次遍历、深度优先、广度优先)
二叉树是一种非常重要的数据结构,很多其它数据结构都是基于二叉树的基础演变而来的。对于二叉树,有深度遍历和广度遍历,深度遍历有前序、中序以及后序三种遍历方法,广度遍历即我们平常所说的层次遍历。因为树的定义本身就是递归定义,因此采用递归的方法去实现树的三种遍历不仅容易理解而且代码很简洁,而对于广度遍历来说,需要其他数据结构的支撑,比如堆了。所以,对于一段代码来说,可读性有时候要比代码本身的效率要重要的原创 2015-02-03 16:10:52 · 423909 阅读 · 41 评论 -
编程之美-分层遍历二叉树
问题:给定一个二叉树,要求按分层遍历该二叉树,即从上到下按层次访问该二叉树(每一层将单独输出一行),每一层要求访问的顺序为从左到右,并将节点依次编号。那么分层遍历如图的二叉树,正确的输出应该为:原创 2015-02-03 17:10:08 · 2017 阅读 · 0 评论 -
LeetCode-Two Sum(编程之美-快速寻找满足条件的两个数字)
Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, whe原创 2015-02-06 20:28:06 · 1540 阅读 · 0 评论 -
LeetCode - Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:原创 2015-08-27 20:02:41 · 845 阅读 · 0 评论 -
LeetCode-Number of Digit One(编程之美-1的数目)
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.For example:Given n = 13,Return 6, because digit 1 occurred in the followin原创 2015-08-29 18:05:33 · 2338 阅读 · 1 评论