【LeetCode】水题(刚开始重新刷题找感觉用的)
[9] Palindrome Number [Easy]
给一个数字,用不转化成字符串的方式判断它是否是回文。
先求数字长度,然后把数字的后半段做翻转(就是不断地取模,除10这种方式),然后判断前后半段是否相等
有特殊情况,负数,10的倍数, 和零
class Solution {
public:
bool isPalindrome(int x) {
if (x < || x % == && x != ) {
return false;
}
//=============[1]先求数字长度
int len = ;
int tmpx = x;
while (tmpx / != ) {
tmpx /= ;
len++;
}
cout << len << endl; //=============[2]然后翻转后面一半数字
int reverse = ;
tmpx = x;
for (int i = ; i < len / + ; ++i) {
reverse = reverse * + (tmpx % );
tmpx /= ;
}
cout << tmpx << " " << reverse << endl; //=============[3]根据长度的奇偶性判断是否需要再除以10
if (len % == ) {
tmpx /= ;
}
if (tmpx != reverse) {
return false;
}
return true;
}
};
[157] Read N Characters Given Read4 [Easy]
给个read4的api,每次能从文件里面读四个字符,最多读N个字符,考虑到文件里面的字符数目可能比N小,所以不能直接返回N。
每次应该 加上 min(read4(buf + k), n); 这种表达, 代码如下:
// Forward declaration of the read4 API.
int read4(char *buf); class Solution {
public:
/**
* @param buf Destination buffer
* @param n Maximum number of characters to read
* @return The number of characters read
*/
int read(char *buf, int n) {
int res = , k = ;
while (n > ) {
int readN = min(read4(buf + k*), n);
n = n - ;
++k;
res += readN;
}
return res;
}
};
[168] Excel Sheet Column Title [Easy]
给个数字N,返回对应excel上面的列名。 比如1返回A,2返回B,27返回AA
26进制换算,有个N--原因需要注意。
class Solution {
public:
string convertToTitle(int n) {
string ans = "";
while(n > ) {
n--; //在这里N--的原因是A对应1,Z对应26
unsigned int mod = n % ;
char ch = 'A' + mod;
ans = ch + ans;
n = n / ;
}
return ans ;
}
};
[266] Palindrome Permutation [Easy]
给个字符串,判断用它的字母能不能组成个回文串,秒杀
class Solution {
public:
bool canPermutePalindrome(string s) {
map<char, int> mapCnt;
for (auto ele : s) {
mapCnt[ele]++;
}
int oddCnt = ;
for (auto ele : mapCnt) {
if(ele.second % ) {
oddCnt++;
if (oddCnt >= ) {
return false;
}
}
}
return true;
}
};
[500] Keyboard Row [Easy]
给N个单词,判断哪些单词的字母在键盘的同一行,输出这些单词。
class Solution {
public:
set<char> setLine1{'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'},
setLine2{'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'},
setLine3{'z', 'x', 'c', 'v', 'b', 'n', 'm'}; int findGroup(char lowerChar) {
if (setLine1.find(lowerChar) != setLine1.end()) {
return ;
} else if (setLine2.find(lowerChar) != setLine2.end()) {
return ;
} else if (setLine3.find(lowerChar) != setLine3.end()){
return ;
}
return -;
} vector<string> findWords(vector<string>& words) {
vector<string> answer;
for(auto word : words) {
int groupId = ;
bool ansIn = true;
groupId = isupper(word[]) ? findGroup(tolower(word[])) : findGroup(word[]);
for (auto i = ; i < word.size(); ++i) {
int tmpGroupId = ;
tmpGroupId = isupper(word[i]) ? findGroup(tolower(word[i])) : findGroup(word[i]);
if (tmpGroupId != groupId) {
ansIn = false;
break;
}
}
if (ansIn) {
answer.push_back(word);
}
}
return answer;
}
};
【LeetCode】水题(刚开始重新刷题找感觉用的)的更多相关文章
- 在vscode中配置LeetCode插件,从此愉快地刷题
大家好,今早在B站看到up主的vscode里藏了leetcode插件,这才知道原来还有这款神器.但是没想到在用的时候遇到了一些麻烦,花了一点时间才解决.所以写这篇文章除了给大家安利这个好用的插件之外, ...
- NOI题库分治算法刷题记录
今天晚自习机房刷题,有一道题最终WA掉两组,极其不爽,晚上回家补完作业欣然搞定它,特意来写篇博文来记录下 (最想吐槽的是这个叫做分治的分类,里面的题目真的需要分治吗...) 先来说下分治法 分治法的设 ...
- 【套题】qbxt国庆刷题班D2
D2 今天的题感觉还是好妙的 T1 传送门 Description 现在有一张\(n\)个节点\(m\)条边的无向连通图\(G=(V,E)\),满足这张图中不存在长度大于等于3的环且图中没有重边和自环 ...
- 【套题】qbxt国庆刷题班D1
Day1 事实上D1的题目还是比较简单的= =然而D1T2爆炸了就十分尴尬--错失一波键盘 看题 T1 传送门 Description 现在你手里有一个计算器,上面显示了一个数\(S\),这个计算器十 ...
- 菜鸟刷题路(随缘刷题):leetcode88
lc88 class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int i = m - 1, j = ...
- LeetCode 到底怎么刷?GitHub 上多位大厂程序员亲测的高效刷题方式
作者:HelloGitHub-小鱼干 在众多的诸如阿里.腾讯等大厂之中,最看中面试者刷题技能的大概要数有"链表厂"之称的字节跳动了.作为一个新晋大厂,字节跳动以高薪.技术大佬云集吸 ...
- 从小白到 6 个 offer,我究竟是怎么刷题的?
最近自习室里又兴起了一阵刷题潮,大家相约刷题~ 今天和大家系统分享下我去年转行时的一个刷题过程和方法,希望对你有所帮助. 首先介绍下我的编程基础,我学的是金融工程专业,硕士时学过 C++ 的课,这也是 ...
- USACO刷题之路,开始了
几天前,重新开始刷题了. 重新刷题有几个原因: 1.曾经的OI经历,如今除了悟性高些.知识多些,大多已经遗忘.不希望真的让之前的OI水平就这么丢了. 2.越来越觉得,刷题真的是一件很开心的事情.大学中 ...
- 为了考PMP,我做了一个刷题小程序
一.背景 1.我是一名软件工程师,技术出身,担任开发组长,对项目管理不是很熟,所以决定系统学习下项目管理. 2.全球最适合的项目管理学习课程就是PMP,每年有4次PMP考试,证书还是很有含金量的. 3 ...
随机推荐
- Java compiler level does not match the version of the installed Java project facet错误
出现问题情景:从其他地方导入一个项目的时候报错:Java compiler level does not match the version of the installed Java project ...
- java中数据库和VO的一一对应关系
如图所示,数据库中数据如果有下划线,则JavaVO中删除,除第一个单词外,其他单词首字母大写
- div标准布局示例
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 【week8 in ricoh】 Learning CNN
week8:5.27 1.做CNN practical[1]里的example1,了解CNN模块中的每一个部分 (1)卷积层的卷积过程,输入输出维度变化(2)ReLU(3)Pooling层(4)Nor ...
- python中map的排序以及取出map中取最大最小值
map排序: 1.按key排序: items=dict.items() items.sort() sorted(dict.items(),key=lambda x:x[0],reverse=False ...
- nmap使用笔记
扫描全端口判断服务 nmap ip -T4 -Pn -sV -p 1-65535 扫描端口并且标记可以爆破的服务 nmap ip --script=ftp-brute,imap-brute,smtp- ...
- localstorage sessionstorage和cookie的区别
基本概念 cookie:是网景公司的前雇员在1993年发明.它的主要用于保存登陆信息,比如登陆某个网站市场可以看到'记住密码’,这就是通过在cookie中存入一段辨别用户身份的数据来实现的. sess ...
- 【leetcode】994. Rotting Oranges
题目如下: In a given grid, each cell can have one of three values: the value 0 representing an empty cel ...
- 【leetcode】927. Three Equal Parts
题目如下: Given an array A of 0s and 1s, divide the array into 3 non-empty parts such that all of these ...
- django 如何重用app
若有一个已经运行稳定的程序,那么可以将其打包,供其他项目安装使用. 假设django项目的目录结构如下: mysite/ manage.py mysite/ __init__.py settings. ...