`
superobin
  • 浏览: 9321 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
计算byte中1的个数 算法, java 计算byte表示的二进制数据中,1出现的次数
public class CountBit {
	public static void main(String[] args) {
		CountBit cnt = new CountBit();
		long start = System.currentTimeMillis();
		int times = 100000000;
		cnt.testAdvanceVersion(times);
		long p0 = System.currentTimeMillis();
		cnt.testLoopVersion(times);
		long p1 = System.currentTimeMillis();
		cnt.testJDKVersion(times);
		long p2 = System.currentTimeMillis();
		System.out.println(""+times+"次均匀分布");
		System.out.println("使用编程之美算法,单位(毫秒):");
		System.out.println(p0-start);

		System.out.println("使用循环移位算法,单位(毫秒):");
		System.out.println(p1-p0);

		System.out.println("使用JDK改版算法,单位(毫秒):");
		System.out.println(p2-p1);
	}

	/**
	 * jdk的算法改byte版,纯粹使用位操作
	 * @param b byte value
	 * @return count of 1
	 */
	public int bitCountJDK(byte b) {
		int i = (b>>>31<<7) + (b&0x7F);
		i = (i&0x55)+(i>>>1&0x55);
		i = (i&0x33)+(i>>>2&0x33);
		return (i&0x0F)+(i>>>4);
	}
	
	/**
	 * 编程之美的算法
	 * @param b byte value
	 * @return count of 1
	 */
	public int bitCountAdvance(byte b) {
		int i = (b>>>31<<7) + (b&0x7F);
		int result = 0;
		while(i!=0) {
			result++;
			i&=i-1;
		}
		return result;
	}
	/***
	 * 移位、循环的算法
	 * @param b byte value
	 * @return count of 1
	 */
	public int bitCountLoop(byte b) {
		int i = (b>>>31<<7) + (b&0x7F);
		int result = 0;
		while(i!=0) {
			result+= i&1;
			i>>>=1;
		}
		return result;
	}
	

	private void testAdvanceVersion(int times) {
		for(int i=0;i<times;i++) {
			bitCountAdvance((byte)((i&255)-128));
		}
	}
	private void testLoopVersion(int times) {
		for(int i=0;i<times;i++) {
			bitCountLoop((byte)((i&255)-128));
		}
	}
	private void testJDKVersion(int times) {
		for(int i=0;i<times;i++) {
			bitCountJDK((byte)((i&255)-128));
		}
	}
}
获取阶乘中0的个数 java, 算法
	static int getZeroCount(int num) {
		int i ,pow=1,result=0;
		while((i=(int) Math.pow(5,pow++))<num) result+=num/i;
		return result;
	}
Spellcheck python 与君共品代码: Spelling Corrector
import re, collections

def words(text): return re.findall('[a-z]+', text.lower()) 

def train(features):
    model = collections.defaultdict(lambda: 1)
    for f in features:
        model[f] += 1
    return model

NWORDS = train(words(file('big.txt').read()))

alphabet = 'abcdefghijklmnopqrstuvwxyz'

def edits1(word):
   splits     = [(word[:i], word[i:]) for i in range(len(word) + 1)]
   deletes    = [a + b[1:] for a, b in splits if b]
   transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]
   replaces   = [a + c + b[1:] for a, b in splits for c in alphabet if b]
   inserts    = [a + c + b     for a, b in splits for c in alphabet]
   return set(deletes + transposes + replaces + inserts)

def known_edits2(word):
    return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in NWORDS)

def known(words): return set(w for w in words if w in NWORDS)

def correct(word):
    candidates = known([word]) or known(edits1(word)) or known_edits2(word) or [word]
    return max(candidates, key=NWORDS.get)
判断大括号是否匹配,最简 java, 算法 华为面试题!
 public static boolean judge2(String s) {
		int k =0,c,i=0,l=s.length();
		for(;i<l&&k<=0;k+=(Math.abs(c=s.charAt(i++)-'|')==1?c:0));
		return k==0;
	}
步进公式 最近面试时候碰到的算法题目,自己写一下,顺便和大家交流下groovy的语法糖
			x += (2 - d) * (d & 1);
			y += (1 - d) * ((d + 1) & 1);
Global site tag (gtag.js) - Google Analytics