随笔分类 -  Data Structure & Algorithm

摘要:References: 1. Stanford University CS97SI by Jaehyun Park 2. Introduction to Algorithms 3. Kuangbin's ACM Template 4. Data Structures by Dayou Liu 5. 阅读全文
posted @ 2018-01-26 15:00 rgvb178 阅读(297) 评论(0) 推荐(0)
摘要:1. 思路 这题实际上是个几何问题——两个外相切的圆,由勾股定理,他们的纵坐标有以下的规律: 则有$$y_{n+1} = y_{n} + \sqrt{(2r)^2 - (x_{n} - x_{n+1})^2}$$ 其中x代表横坐标,y代表纵坐标,r代表半径。 简略画了个图: 2. 容易错的地方 非常 阅读全文
posted @ 2018-01-25 10:25 rgvb178 阅读(301) 评论(0) 推荐(0)
摘要:1. 题目描述Description Link: https://leetcode.com/problems/majority-element/description/ Given an array of size n, find the majority element. The majority 阅读全文
posted @ 2017-12-12 09:27 rgvb178 阅读(384) 评论(0) 推荐(0)
摘要:1) Link to the problem: http://codeforces.com/contest/888/problem/C 2) Description: You are given a string s consisting of lowercase Latin letters. Ch 阅读全文
posted @ 2017-11-13 13:18 rgvb178 阅读(437) 评论(0) 推荐(0)
摘要:1. 问题 231. Power of Two: 判断一个整数是否是2的n次方,其中n是非负整数 342. Power of Four: 判断一个整数是否是4的n次方,其中n是非负整数 326. Power of Three: 判断一个整数是否是3的n次方,其中n是非负整数 2. 思路 1)2的n次 阅读全文
posted @ 2017-08-30 01:46 rgvb178 阅读(629) 评论(0) 推荐(0)
摘要:1. 题目:https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/ 反转字符串中的所有单词。 2. 思路: 这题主要是要注意空格的影响。比方说,string首尾和单词之间可能有一或多个空格。看到有人逐个对空格判断,但是 阅读全文
posted @ 2017-08-27 02:51 rgvb178 阅读(724) 评论(0) 推荐(0)
摘要:1. 题目链接:https://leetcode.com/problems/hamming-distance/description/ 2.思路 常规做法做完看到评论区一个非常有意思的做法。用了n&=(n-1),这个地方的意思是,将最右边的1变成0。比方说: 最简单的例子: 原数字: 101011 阅读全文
posted @ 2017-08-26 07:04 rgvb178 阅读(639) 评论(0) 推荐(0)
摘要:1. 题目大意 根据给定矩阵,重塑一个矩阵,r是所求矩阵的行数,c是所求矩阵的列数。如果给定矩阵和所求矩阵的数据个数不一样,那么返回原矩阵。否则,重塑矩阵。其中两个矩阵中的数据顺序不变(先行后列)。 2. 思路 由于矩阵中数据顺序不变,因此我们考虑按顺序做。原矩阵中的第i行第j列(从0开始)的数据可 阅读全文
posted @ 2017-08-26 06:43 rgvb178 阅读(614) 评论(1) 推荐(0)
摘要:原题 [原题链接][1] Description: Count the number of prime numbers less than a non negative number, n. 计算小于非负数n的素数个数。 思路 这题用[埃拉托斯特尼筛法][2]来做效果比较好,普通的方法基本会TLE。 阅读全文
posted @ 2017-01-10 01:08 rgvb178 阅读(946) 评论(0) 推荐(0)
摘要:原题 [原题链接][1] You are given a map in form of a two dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected ho 阅读全文
posted @ 2016-12-01 11:41 rgvb178 阅读(1030) 评论(0) 推荐(0)
摘要:题目 [题目链接][1] Given a non empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring togeth 阅读全文
posted @ 2016-11-19 23:33 rgvb178 阅读(2182) 评论(0) 推荐(0)
摘要:题目 [题目链接][1] Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements th 阅读全文
posted @ 2016-11-17 09:44 rgvb178 阅读(2464) 评论(0) 推荐(0)
摘要:1.题目大意 Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. For examp 阅读全文
posted @ 2016-10-29 12:19 rgvb178 阅读(722) 评论(0) 推荐(0)
摘要:1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Subscribe to see which companies 阅读全文
posted @ 2016-10-28 22:51 rgvb178 阅读(817) 评论(0) 推荐(0)
摘要:1.题目大意 Write a program that outputs the string representation of numbers from 1 to n. But for multiples of three it should output “Fizz” instead of th 阅读全文
posted @ 2016-10-15 15:13 rgvb178 阅读(2989) 评论(0) 推荐(0)
摘要:1.题目大意 Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime 阅读全文
posted @ 2016-10-13 22:47 rgvb178 阅读(1798) 评论(0) 推荐(0)
摘要:1.题目大意 求一个长度不超过80的字符串的最小周期. 2.思路 非常简单,基本就是根据周期的定义做出来的,几乎不需要过脑. 3.应该注意的地方 (1) 最后输出的方式要注意,不然很容易就PE了.不过个人认为,其实这题Sample Output给的不好 (2) 注意输出的要求是最小周期 4.代码 参 阅读全文
posted @ 2016-10-11 22:57 rgvb178 阅读(2220) 评论(1) 推荐(0)
摘要:1.题目大意 把前n$(n\le 10000)$个整数顺次写在一起:12345678910111213……计算0~9各出现了多少次。 2.思路 第一想法是打表,然而觉得稍微有点暴力。不过暂时没有想到更好的办法了,写完看了一下其它人的思路好像也差不多是打表的思路。 3.应注意的问题 (1)首先是格式问 阅读全文
posted @ 2016-10-11 20:29 rgvb178 阅读(1355) 评论(0) 推荐(0)
摘要:关键在于判断数字是两位数还是单位数,其他部分没有难度。 参考书目:算法竞赛入门经典(第2版) 刘汝佳 编著 阅读全文
posted @ 2016-10-11 15:01 rgvb178 阅读(1196) 评论(0) 推荐(0)
摘要:1.题目大意 给出一个由O和X组成的字符串(长度为80以内),每个O的得分为目前连续出现的O的数量,X得分为0,统计得分。 2.思路 实在说不出了,这题没过脑AC的。直接贴代码吧。=_= 3.代码 参考书目:算法竞赛入门经典(第2版) 刘汝佳 编著 阅读全文
posted @ 2016-10-11 14:06 rgvb178 阅读(1526) 评论(0) 推荐(0)