leetcode水题题解
344. Reverse String
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
- class Solution {
- public:
- string reverseString(string s) {
- string::size_type i, j;
- if (s.size() == 0)
- {
- return s;
- }
- for (i = 0, j = s.size() - 1; i < j; i++, j--)
- {
- char ch = s[i];
- s[i] = s[j];
- s[j] = ch;
- }
- return s;
- }
- };
记住size_type是unsigned的就可以了
412. Fizz Buzz
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 the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.
Example:
- n = 15,
- Return:
- [
- "1",
- "2",
- "Fizz",
- "4",
- "Buzz",
- "Fizz",
- "7",
- "8",
- "Fizz",
- "Buzz",
- "11",
- "Fizz",
- "13",
- "14",
- "FizzBuzz"
- ]
- class Solution {
- public:
- vector<string> fizzBuzz(int n) {
- vector<string> vec;
- for (int i = 1; i <= n; ++i)
- {
- if (i % 3 == 0 && i % 5 == 0)
- {
- vec.push_back("FizzBuzz");
- }
- else if (i % 3 == 0)
- {
- vec.push_back("Fizz");
- }
- else if(i % 5 == 0)
- {
- vec.push_back("Buzz");
- }
- else
- {
- stringstream ss;
- ss << i;
- vec.push_back(ss.str());
- }
- }
- return vec;
- }
- };
292. Nim Game
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.
Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.
For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.
Hint:
If there are 5 stones in the heap, could you figure out a way to remove the stones such that you will always be the winner?
最开始的想法:
保存先前的结果,也就是记忆化。
- 1:win
- 2:win
- 3:win
- 4:lost
- 5:win
- 6:win
- 7:win
- 8:lost
- 9:win
- 10:win
- 11:win
- 12:lost
- 13:win
- 14:win
- 15:win
- 16:lost
- 17:win
- 18:win
- 19:win
- 20:lost
- 21:win
- 22:win
- 23:win
- 24:lost
- 25:win
- 26:win
- 27:win
- 28:lost
- 29:win
- 30:win
- 31:win
- 32:lost
- [Finished in 1.5s]
2 3 4一组,3 4 5一组, 4 5 6一组,这样。
n减去每一组,和先前的结果建立联系。
- #include <iostream>
- #include <vector>
- #include <set>
- using namespace std;
- class Solution {
- public:
- Solution()
- {
- for (int j = 7; j <= 100; ++j)
- {
- canWinNim(j);
- }
- }
- bool canWinNim(int n) {
- int i;
- bool flag;
- s.insert(1);
- s.insert(2);
- s.insert(3);
- s.insert(5);
- s.insert(6);
- //1 2 3 4 5 6特殊处理
- if ((n >= 1 && n <= 3) || n == 5 || n == 6)
- {
- return true;
- }
- if (n == 4)
- {
- return false;
- }
- //1
- flag = true;
- for (i = 2; i <= 4; ++i)
- {
- if (s.count(n - i) == 0)
- {
- flag = false;
- break;
- }
- }
- if (flag)
- {
- s.insert(n);
- return true;
- }
- //2
- flag = true;
- for (i = 3; i <= 5; ++i)
- {
- if (s.count(n - i) == 0)
- {
- flag = false;
- break;
- }
- }
- if (flag)
- {
- s.insert(n);
- return true;
- }
- //3
- flag = true;
- for (i = 4; i <= 6; ++i)
- {
- if (s.count(n - i) == 0)
- {
- flag = false;
- break;
- }
- }
- if (flag)
- {
- s.insert(n);
- return true;
- }
- return false;
- }
- private:
- set<int>s;
- };
- int main()
- {
- Solution s;
- for (int i = 1; i <= 32; ++i)
- {
- if (s.canWinNim(i))
- {
- cout << i << ":win" << endl;
- }
- else
- {
- cout << i << ":lost" << endl;
- }
- }
- //int x = 1199886170;
- //2147483647
- return 0;
- }
超时,在这个点1199886170
必然,预处理得超过它。
思考下:但凡两人同时取过过后,剩下的是4,先取的就输了
由上图,对于8,先取的输了
由上图,对于12,先取的输了
16, 20, 24,必然都是先取的输了
- class Solution {
- public:
- bool canWinNim(int n) {
- if(n % 4 == 0)
- {
- return false;
- }
- return true;
- }
- };
367. Valid Perfect Square
Given a positive integer num, write a function which returns True if num is a perfect square else False.
Note: Do not use any built-in library function such as sqrt
.
Example 1:
- Input: 16
- Returns: True
Example 2:
- Input: 14
- Returns: False
二分查找,对于整数相乘溢出,得做个检测
。
- #include <iostream>
- #include <vector>
- #include <set>
- #include <algorithm>
- #include <string>
- #include <sstream>
- #include <cstring>
- using namespace std;
- class Solution {
- public:
- bool isPerfectSquare(int x) {
- if (x == 0 || x == 1)
- {
- return true;
- }
- return BinarySearch(x, x);
- }
- private:
- bool BinarySearch(int x, int target)
- {
- int left = 1;
- int right = x / 2;
- while(left <= right)
- {
- int mid = (left + right) / 2;
- //溢出
- if (mid * mid < 0)
- {
- right = mid - 1;
- }
- else if (mid * mid > target || target / mid < mid)
- {
- right = mid - 1;
- }
- else if(mid * mid < target)
- {
- left = mid + 1;
- }
- else if(mid * mid == target)
- {
- return true;
- }
- }
- return false;
- }
- };
- int main()
- {
- Solution s;
- cout << s.isPerfectSquare(24);
- return 0;
- }
leetcode水题题解的更多相关文章
- World Finals 2017 (水题题解)
看大佬做2017-WF,我这种菜鸡,只能刷刷水题,勉强维持生活. 赛后补补水题. 题目pdf链接,中文的,tls翻译的,链接在这里 个人喜欢在vjudge上面刷题. E Need for Speed ...
- bzoj usaco 金组水题题解(2)
续.....TAT这回不到50题编辑器就崩了.. 这里塞40道吧= = bzoj 1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害 比较经典的最小割?..然而 ...
- bzoj usaco 金组水题题解(2.5)
bzoj 2197: [Usaco2011 Mar]Tree Decoration 树形dp..f[i]表示处理完以i为根的子树的最小时间. 因为一个点上可以挂无数个,所以在点i上挂东西的单位花费就是 ...
- bzoj usaco 金组水题题解(1)
UPD:我真不是想骗访问量TAT..一开始没注意总长度写着写着网页崩了王仓(其实中午的时候就时常开始卡了= =)....损失了2h(幸好长一点的都单独开了一篇)....吓得赶紧分成两坨....TAT. ...
- Cmd2001的毒瘤水题题解
怕不是我再不写题解这题就该成没人做也没人会的千古谜题了...... T1: 仔细分析题面,发现相同就是广义SAM上节点相同,相似就是广义SAM上为从根到某个点路径的前缀..直接SAM上跑从根开始,每个 ...
- 2006-2007 ACM-ICPC | POJ3380 POJ3384 POJ3385 水题题解
// CF比赛链接:http://codeforces.com/gym/101650 // POJ链接:http://poj.org/searchproblem?field=source&ke ...
- leetcode水题(一)
Two Sum 1 public int[] twoSum(int[] numbers,int target){ Map<Integer,Integer> map = new HashMa ...
- LOJ6303:水题——题解
https://loj.ac/problem/6303 题目来自LOJ. 就记一个公式,设f(n,k)为n!里分解得到的k(k为质数)的个数,则f(n,k)=f(n/k,k)+n/k. 证明很好证,显 ...
- 看完互联网大佬的「LeetCode 刷题手册」, 手撕了 400 道 Leetcode 算法题
大家好,我是 程序员小熊 ,来自 大厂 的程序猿.相信绝大部分程序猿都有一个进大厂的梦想,但相较于以前,目前大厂的面试,只要是研发相关岗位,算法题基本少不了,所以现在很多人都会去刷 Leetcode ...
随机推荐
- [白话解析] 深入浅出一致性Hash原理
[白话解析] 深入浅出一致性Hash原理 0x00 摘要 一致性哈希算法是分布式系统中常用的算法.但相信很多朋友都是知其然而不知其所以然.本文将尽量使用易懂的方式介绍一致性哈希原理,并且通过具体应用场 ...
- sql计算上一周(解决了跨年会出错的问题)
1.问题描述: 使用YEARWEEK('时间字段')=YEARWEEK(NOW())-1来筛选上一周数据时,当遇到跨年的时候会出现计算错误的问题. eg: 如上图,当前日期为2020年1月6日.上图标 ...
- 【Magicodes.IE 2.0.0-beta1版本发布】已支持数据表格、列筛选器和Sheet拆分
为了更好的完善Magicodes.IE,春节期间我们会进行一次大的重构.由于精力有限,急缺文档和翻译(将文档翻译为英文文档)支持,诚邀各位加入.同时在功能方便也做了相关规划,有兴趣的朋友可以参与提交P ...
- 【LC_Lesson2】---整数反转练习
题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 1 ...
- PTA - 堆栈模拟队列
设已知有两个堆栈S1和S2,请用这两个堆栈模拟出一个队列Q. 所谓用堆栈模拟队列,实际上就是通过调用堆栈的下列操作函数: int IsFull(Stack S):判断堆栈S是否已满,返回1或0: in ...
- NETCore下IConfiguration和IOptions的用法
NETCore下IConfiguration和IOptions的用法 https://www.cnblogs.com/RainingNight/p/strongly-typed-options-con ...
- NetCore下的log4
https://www.cnblogs.com/zhangxiaoyong/p/9463791.html 这一篇也不错 .NET常用的日志组件有NLog.Log4net等,.NET CORE下微软也自 ...
- HTTP图解笔记(六)—— 第6章 HTTP首部
前言 为啥第一章直接跳到第六章呢,因为...博主当初看书的时候挑着看..只看了第一章和第六章┗( ▔, ▔ )┛ HTTP图解对于不熟悉HTTP的小伙伴来说是很好的书籍,建议入手! 一. HTTP报文 ...
- MySQL——DOS命令
翻开之前的笔记发现有这么一篇,于是整理了一下发出来加深记忆并分享交流,欢迎纠错,谢谢!!! 1.启动MySQL服务: net start mysql; 2.停止MySQL服务: net stop my ...
- 聊一聊 InnoDB 引擎中的索引类型
索引对数据库有多重要,我想大家都已经知道了吧,关于索引可能大家会对它多少有一些误解,首先索引是一种数据结构,并且索引不是越多越好.合理的索引可以提高存储引擎对数据的查询效率. 形象一点来说呢,索引跟书 ...