随笔分类 -  OJ编程算法练习

摘要:Search in Rotated Sorted Array I 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 阅读全文
posted @ 2016-09-27 16:32 godjob 阅读(187) 评论(0) 推荐(0)
摘要:不用加减法计算两个整数的和。这道题其实是考察一些基本的布尔代数知识。我们知道,二进制表示时: 0 + 0 = 00 1 + 0 = 01 0 + 1 = 01 1 + 1 = 10 所以,两个二进制整数 a 和 b,如果相加的过程中如果没有进位,那么 a+b=a⊗b,这里 ⊗ 表示异或。那么 a+b 阅读全文
posted @ 2016-08-24 14:19 godjob 阅读(240) 评论(0) 推荐(0)
摘要:Given a nested list of integers, implement an iterator to flatten it. Each element is either an integer, or a list -- whose elements may also be integ 阅读全文
posted @ 2016-06-01 23:36 godjob 阅读(213) 评论(0) 推荐(0)
摘要:LeetCode 第 342 题(Power of Four) Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, retu 阅读全文
posted @ 2016-06-01 19:57 godjob 阅读(184) 评论(0) 推荐(0)
摘要:Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum pro 阅读全文
posted @ 2016-05-20 23:45 godjob 阅读(163) 评论(0) 推荐(0)
摘要:Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Not 阅读全文
posted @ 2016-05-17 23:09 godjob 阅读(262) 评论(0) 推荐(0)
摘要:Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Given s = "hello", return "holle". Example 2: Given 阅读全文
posted @ 2016-05-16 21:50 godjob 阅读(264) 评论(0) 推荐(0)
摘要:Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and r 阅读全文
posted @ 2016-05-14 14:39 godjob 阅读(223) 评论(0) 推荐(0)
摘要:题目描述: Given a non-empty array of integers, return the k most frequent elements. For example, Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You ma 阅读全文
posted @ 2016-05-13 22:42 godjob 阅读(216) 评论(0) 推荐(0)
摘要:算法一:快速排序算法 快速排序是由东尼·霍尔所发展的一种排序算法。在平均状况下,排序 n 个项目要Ο(n log n)次比较。在最坏状况下则需要Ο(n2)次比较,但这种状况并不常见。事实上,快速排序通常明显比其他Ο(n log n) 算法更快,因为它的内部循环(inner loop)可以在大部分的架 阅读全文
posted @ 2016-03-25 23:24 godjob 阅读(2453) 评论(0) 推荐(0)
摘要:题目描述:输入n个整数,输出其中最小的k个元素。 例如:输入1,2,3,4,5,6,7,8这8个数字,则最小的4个数字为1,2,3,4。 思路1:最容易想到的方法:先对这个序列从小到大排序,然后输出前面的最小的k个数即可。如果选择快速排序法来进行排序,则时间复杂度:O(n*logn) 思路2:在思路 阅读全文
posted @ 2016-03-25 22:52 godjob 阅读(2187) 评论(0) 推荐(0)
摘要:Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and r 阅读全文
posted @ 2016-03-19 00:01 godjob 阅读(237) 评论(0) 推荐(0)
摘要:The Broken PedometerThe ProblemA marathon runner uses a pedometer with which he is having problems. In the pedometer the symbols are represented by seven segments (or LEDs):But the pedometer does not work properly (possibly the sweat affected the batteries) and only some of the LEDs are active. The 阅读全文
posted @ 2012-12-01 22:48 godjob 阅读(429) 评论(0) 推荐(0)
摘要:题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=233题目类型: 数据结构, 二叉树题意与背景:同二叉树一样,四叉树也是一种数据结构,是一种每个节点... 阅读全文
posted @ 2012-10-20 23:12 godjob 阅读(1745) 评论(0) 推荐(0)
摘要:转的题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=489递归求解,可以不用建树。#include<iostream>#include<sstream>#include<string>#include<algorithm>usingnamespacestd;constintminn=100000005;intinorder[10002];i 阅读全文
posted @ 2012-10-20 23:10 godjob 阅读(282) 评论(0) 推荐(0)
摘要:#include <stdio.h>int main(){ int t,i,j,n,m,h,sum,a[4000]; scanf("%d",&t); while (t--) { scanf("%d",&n); for (i=1;i<=n;i++) a[i]=0; sum=0; scanf("%d",&m); for (i=1;i<=m;i++) {scanf("%d",&h); for (j=1;j*h<=n;j++) a[j*h]=1; } for ( 阅读全文
posted @ 2012-10-17 20:46 godjob 阅读(180) 评论(0) 推荐(0)
摘要:很久不做题了,做也做水题,做水题也做不出,想很久没想出来,看了一下别人的blog,可以这么想,要产生一个交点至少要上下各两个点,这样答案出来了,上面有(a-1)*a/2种,同理下面也是,这样就得到答案了 1 #include <cstdio> 2 #include <iostream> 3 using namespace std; 4 5 int 6 main ( int argc, char *argv[] ) 7 { 8 freopen("data","r",stdin); 9 long long int a,b;10 int 阅读全文
posted @ 2012-10-13 21:36 godjob 阅读(161) 评论(0) 推荐(0)
摘要:在网上找到一个很好的算法,仔细观察可以发现,目标序列的开头几个乌龟都是经过移动后形成的,只要从原始序列中对比查找目标序列是否相同就能很快得到答案 ,代码够短了吧,呵呵 题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=1093 首先找到需要移动的字符串,方法如下:以初始序列为准,设初始序列下标为i, 目的序列下标为j, 从n-1开始,如果两下标对应的字符串相等,下标同时减一,否则仅初始序列 阅读全文
posted @ 2012-08-15 22:38 godjob 阅读(240) 评论(0) 推荐(0)
摘要:找规律1: 12 1 13 1 1 14: 1 2 1 5 1 2 1 16 1 2 2 17 1 2 2 1 18 1 2 2 2 19: 1 2 3 2 110 1 2 3 2 1 111 1 2 3 2 2 112 1 2 3 3 2 113 1 2 3 3 2 1 114 1 2 3 3 2 2 115 1 2 3 3 3 2 116: 1 2 3 4 3 2 1n*n:2*n-1N*N-(N-1)*(N-1)-12*n-2因为完全平方数的最小step是确定的所以,以完全平方数为界限。n*n: 1......n......1 minstep:2*n-1..n*(n+1) 1...... 阅读全文
posted @ 2012-08-15 17:17 godjob 阅读(149) 评论(0) 推荐(0)
摘要:必然存在整数x(1=<x <= n)满足:当 s1 = 1+2+3+...+x+..+n>= k时,有s2 = 1+2+3+...-x+..+n== k,即多出的x肯定在1~n之间。s1-s2 = s1 - k = 2x所以,我们想求最小的n,也就是求最小的满足条件的s1,而它与k的差必为偶数,剩下的暴力找就可以了。#include<iostream>#include<string>#include<cstring>#include<cstdio>#include<queue>#include<stack> 阅读全文
posted @ 2012-08-14 21:12 godjob 阅读(347) 评论(0) 推荐(0)