
积少成多
Shingle_
Done is better than perfect.
展开
-
2-sum问题
2-sum问题问题描述输入一个整数数组和一个整数,在数组中查找一对数,满足他们的和正好是输入的那个整数。分析求解暴力求解法——从数组中任意选两个数,判定他们的和是否等于输入的那个数。时间复杂度O(n^2)。双层循环,检查所有元素对:public static int count(int[] a, int sum){ N = a.length(); int cnt = 0; f原创 2016-06-26 14:44:12 · 1124 阅读 · 0 评论 -
荷兰国旗
现有n个红白蓝三种不同颜色的小球,乱序排列在一起,请通过两两交换任意两个球,使得从左到右依次为红球、白球、蓝球。可以借鉴快速排序的思路,先看三路快速排序的实现三路快速排序法适用于有大量重复元素的排序。public class Quick3Way { public static void sort(Comparable[] a) { sort(a, 0, a.lengt原创 2016-08-26 16:21:16 · 607 阅读 · 0 评论 -
奇偶数排序
给定一个整数数组,请调整数组中数的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分。一头一尾指针往中间扫描import java.util.Arrays;public class OddEvenSortTest { private static boolean IsOddNumber(int data) { return (data & 1) == 1;原创 2016-08-26 15:51:18 · 715 阅读 · 0 评论 -
查找出现次数超过一半的数
数组中有一个数的出现次数超过了数组长度的一半,找出这个数。排序排序后输出n/2位置的数,时间复杂度O(nlogn)散列表时间复杂度O(n),空间复杂度O(n).记录两个值public int findOneNumber(int a[]) { int candidate = a[0]; int nTimes = 1; for(int i = 1;原创 2016-09-01 14:21:00 · 1091 阅读 · 0 评论 -
跳台阶问题
一个台阶总共有n级,如果一次可以跳1级,也可以跳2级,求总共有多少种跳法。递归法long long Fibpnacci(unsighed int n){ int result[3] = {0, 1, 2}; if(n <= 2) { return result[n]; } teturn Fibonacci(n - 1) + Fibonacci原创 2016-08-04 11:25:05 · 483 阅读 · 0 评论 -
Remove Duplicates from Sorted Array II
Follow up for “Remove Duplicates”: What if duplicates are allowed at most twice?For example, Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five ele原创 2016-08-30 15:58:11 · 350 阅读 · 0 评论 -
Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with cons原创 2016-08-30 15:54:55 · 332 阅读 · 0 评论 -
Search in Rotated Sorted Array II
Follow up for “Search in Rotated Sorted Array”: What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the array.允原创 2016-08-30 15:15:04 · 267 阅读 · 0 评论 -
Search in Rotated Sorted Array
**Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array return its ind原创 2016-08-30 14:44:47 · 353 阅读 · 0 评论 -
Tfidf
Tfidf(词频-反转文档频率)TF代表统计部分, 而IDF把权重折扣考虑了进去。>>> import scipy as sp>>> def tfidf(term, doc, docset):··· tf = float(doc.count(term) / sum(doc.count(term) for doc in docset)··· idf = math.log(floa原创 2016-06-13 15:59:40 · 968 阅读 · 0 评论 -
最大连续子数组和
题目描述给出一个长度为n的序列A1, A2,···, An, 求最大连续和。换句话说,要求找到1<=i<=j<=n, 使得Ai + Ai+1 + ···+ Aj 尽量大。蛮力枚举int MaxSubArray(int* A, int n){ int maxSum = a[0]; int currSum = 0; for(int I = 0; I < n; I++)原创 2016-06-26 21:31:13 · 797 阅读 · 0 评论 -
判断一个数是否是素数
根据定义,被1和它自身整除的、大于1的整数称为素数 (Java)public static boolean isPrime(int N){ if(N < 2) return false; for(int I=2; I*I <= N; I++) if(N % I == 0) return false; return true;}改进版: (C)int原创 2016-06-21 10:48:40 · 670 阅读 · 0 评论