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 ...
随机推荐
- javascript 中not defined 和undefined有什么区别
概念上的解释:undefined是javascript语言中定义的五个原始类中的一个,换句话说,undefined并不是程序报错,而是程序允许的一个值.not defined是javascript在运 ...
- DISTINCT 去重---SQL
SQL SELECT DISTINCT 语句 在表中,一个列可能会包含多个重复值,有时您也许希望仅仅列出不同(distinct)的值. DISTINCT 关键词用于返回唯一不同的值. SQL SELE ...
- Redis的分布式锁
一.锁的作用 当多线程执行某一业务时(特别是对数据的更新.新增)等操作,可能就会出现多个线程对同一条数据进行修改.其最终的结果一定与你期望的结果“不太一样”,这就与需要一把锁来控制线程排排队了 - j ...
- laravel之null替换空字符串中间件
在laravel写接口的时候免不了数据库中保存null,可用诸如设置ORM的访问器或以下方法处理 $goods->name?$goods->name:''; 其实可以利用路由中间件,在需要 ...
- Incorrect configuration: namenode address dfs.namenode.servicerpc-address or dfs.namenode.rpc-address is not configured.
在搭建Hadoop集群的时候,遇到了这样的一个报错. 就在启动HDFS调用命令: start-dfs.sh 的时候,报错: 然后输密码就一直很奇怪,反正一直运行不成功. 百度了半天,确定是core-s ...
- Codeforces Beta Round #12 (Div 2 Only) D. Ball 树状数组查询后缀、最值
http://codeforces.com/problemset/problem/12/D 这里的BIT查询,指的是查询[1, R]或者[R, maxn]之间的最值,这样就够用了. 设三个权值分别是b ...
- Exception in thread "main" java.lang.SecurityException: class "javax.servlet.FilterRegistration"'s signer information does not match signer information of other classes in the same package解决办法(图文详解)
不多说,直接上干货! 问题详情 SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF ...
- dos命令安装windows服务
以下两种方法都是通过dos命令创建windows服务 1.创建服务 sc create UploadRealVolumeService start= auto binpath= C:\Users\Ad ...
- uvm_factory——我们的工厂(二)
上节我们说到uvm_object_registry #(T),uvm_object_reistry 又继承自uvm_object_wrapper,所以首先,让我们先看看它爹是啥样子的: //----- ...
- Android学习总结(九)———— 内容提供器(ContentProvider)
一.内容提供器基本概念 内容提供器主要用于在不同的应用程序之间实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能保证被访数据的安全性.详细资料请看下图: 二.示例 ...