leetcode-1-basic
leetcode-algorithm
1. Two Sum
解法:循环,试呗。。简单粗暴。。
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
//vector<int> temp = nums;
int i;
int j;
vector<int> result();
bool flag = false;
for (i = ; i != nums.size()-; i++) {
for (j = i+; j != nums.size(); j++) {
if (nums[i] + nums[j] == target) {
result[] = i;
result[] = j;
flag = true;
break;
}
}
if (flag == true)
break;
}
return result;
}
};
7. Reverse Integer
class Solution {
public:
int reverse(int x) {
long newNum;
newNum = ;
while(x != ) {
// overflow has to be handled
newNum = newNum * + x % ;
if(newNum > INT_MAX || newNum < INT_MIN)
return ;
x = x / ;
}
return newNum;
}
};
9. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.
sol: transform into string and compare within string
class Solution {
public:
bool isPalindrome(int x) {
string s = std::to_string(x);
int i = ;
int j = s.length() - ;
while (i < j) {
if (s[i] != s[j])
return false;
i++;
j--;
}
return true;
}
};
13. Roman to Integer
class Solution {
public:
int myFunc(char c) {
int data = ;
switch (c) {
case 'I':
data = ;
break;
case 'V':
data = ;
break;
case 'X':
data = ;
break;
case 'L':
data = ;
break;
case 'C':
data = ;
break;
case 'D':
data = ;
break;
case 'M':
data = ;
break;
}
return data;
}
int romanToInt(string s) {
int result = ;
int i;
int pre;
int cur;
result = myFunc(s[]);
if (s.length() == )
return result;
for (i = ; i < s.length(); i++) {
pre = myFunc(s[i-]);
cur = myFunc(s[i]);
if (pre >= cur)
result += cur;
else
result = result - * pre + cur;
}
return result;
}
};
leetcode-1-basic的更多相关文章
- [LeetCode] 224. Basic Calculator 基本计算器
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- [LeetCode] 227. Basic Calculator II 基本计算器 II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- [LeetCode] 772. Basic Calculator III 基本计算器之三
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- [LeetCode] 227. Basic Calculator II 基本计算器之二
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- LeetCode#227.Basic Calculator II
题目 Implement a basic calculator to evaluate a simple expression string. The expression string contai ...
- Java for LeetCode 227 Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- Java for LeetCode 224 Basic Calculator
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- (medium)LeetCode 224.Basic Calculator
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- (medium)LeetCode 227.Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- leetcode 224. Basic Calculator 、227. Basic Calculator II
这种题都要设置一个符号位的变量 224. Basic Calculator 设置数值和符号两个变量,遇到左括号将数值和符号加进栈中 class Solution { public: int calcu ...
随机推荐
- Jmeter-提取Json数据进行关联
1:Json后置处理器提取结果作为下一个sampler的传入参数 1.1:[线程组]->[简单控制器]->[HTTP sampler]->[Beanshell后置取样器]-> ...
- (洛谷P2512||bzoj1045) [HAOI2008]糖果传递 || 洛谷P4016 负载平衡问题 || UVA11300 Spreading the Wealth || (洛谷P3156||bzoj3293) [CQOI2011]分金币
bzoj1045 洛谷P4016 洛谷P2512 bzoj3293 洛谷P3156 题解:https://www.luogu.org/blog/LittleRewriter/solution-p251 ...
- Hive_Hive的管理_远程服务
远程服务启动方式 - 端口号10000 - 启动方式: #hive --service hiveserver & 以JDBC或ODBC的程序登陆到hive中操作数据时,必须选用远程服务启动方式 ...
- centos 6.x下pxe+tftp+http+kickstart无人值守安装操作系统
1.1 什么是PXE PXE(Pre-boot Execution Environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作站通过 ...
- shell 中的if语句
if [ t != "." -a t != ".." ] 之前一直不知道 -a 是什么意思,后来才知道 -a = and ; -o = or
- 牛客网Java刷题知识点之什么是HTTP协议、什么是HTTP隧道、HTTP响应的结构是怎么样的、HTTP报头包含哪些、HTTP中GET与POST方法有什么区别
不多说,直接上干货! https://www.nowcoder.com/ta/review-java/review?tpId=31&tqId=21169&query=&asc= ...
- 07.Javascript——入门高阶函数
高阶函数英文叫Higher-order function..JavaScript的函数其实都指向某个变量.既然变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数 ...
- iis日志存放位置 及 查看方法
IIS:控制面板--管理工具--internet信息服务 网站的IIS日志是在空间里面看的.要登陆到空间里面的一个IIS日志里面看.IIS日志一般都很大的.看会有点.. 一.应用程序日志.安全日志.系 ...
- JavaScript单元测试框架:Jasmine
摘抄:http://blog.csdn.net/GuoJiangweigege/article/details/52130589 互联网的快速发展,给web开发人员带来了前所未有的挑战.对于前端开发, ...
- JS实现的图片预览功能
之前的博文有实现过图片上传预览,但那种方法是预览时就将图片上传,会产生很大的浪费空间.找到了之前有人写的用JS实现的图片预览,就说用js将上传的图片显示,上传代码在之前的博文中有写到. 以下是实现的代 ...