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的更多相关文章

  1. [LeetCode] 224. Basic Calculator 基本计算器

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  2. [LeetCode] 227. Basic Calculator II 基本计算器 II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  3. [LeetCode] 772. Basic Calculator III 基本计算器之三

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  4. [LeetCode] 227. Basic Calculator II 基本计算器之二

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  5. LeetCode#227.Basic Calculator II

    题目 Implement a basic calculator to evaluate a simple expression string. The expression string contai ...

  6. Java for LeetCode 227 Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  7. Java for LeetCode 224 Basic Calculator

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  8. (medium)LeetCode 224.Basic Calculator

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  9. (medium)LeetCode 227.Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  10. leetcode 224. Basic Calculator 、227. Basic Calculator II

    这种题都要设置一个符号位的变量 224. Basic Calculator 设置数值和符号两个变量,遇到左括号将数值和符号加进栈中 class Solution { public: int calcu ...

随机推荐

  1. 使用top观察一进程的cpu历史占用情况

    #!/bin/shtop -b -n 1 -p 1975| tail -3 >>process1975.log 搞了时间节点,做个定时任务什么的就ok了

  2. Mysql的跨服务器操作

    1.查询FEDERATED功能是否开启: show ENGINES; 2.如果状态为NO则需修改my.ini文件,增加一行federated配置: my.ini配置文件的默认路径 C:\Program ...

  3. JetSpeed2因dom4j包冲突导致PSML页面文件数据丢失

    使用JetSpeed2进行二次开发时突然出现在保存Portlet配置信息时出现PSML页面文件数据丢失的情况,几经测试,最终发现是因为Portlet中的dom4j.jar与jetspeed应用中的do ...

  4. 关于 ie8不兼容的一些方法

    ie8 不兼容的方法 $(function(){ //添加数组IndexOf方法 if (!Array.prototype.indexOf){ Array.prototype.indexOf = fu ...

  5. python GIL锁问题

    一.GIL是什么 官方解释: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple na ...

  6. InitialContext与lookup

    Context initial = new InitialContext(); Object objref = initial.lookup("java:comp/env/ejb/Simpl ...

  7. 什么是闭包(Closure)?

    http://kb.cnblogs.com/page/111780/ 这个问题是在最近一次英格兰Brighton ALT.NET Beers活动中提出来的.我发现,如果不用代码来演示,你很难单用话语把 ...

  8. 如何在Ubuntu 16.04上安装Apache Web服务器

    转载自:https://www.howtoing.com/how-to-install-the-apache-web-server-on-ubuntu-16-04 介绍 Apache HTTP服务器是 ...

  9. CAD控件界面显示与隐藏(网页版)

    控件界面工具栏的显示或隐藏,js代码实现如下: 1 2 3 4 5 6 7 8 9 //隐藏/显示工具栏       mxOcx.ShowToolBar("常用工具",isShow ...

  10. 如何用node命令和webpack命令传递参数

    1. 比如在项目中我们的publicPath需要根据服务器环境的变化而变化,这时我们会写一个配置文件,在webpack.config.js中读取,可以 如何才能 取到变量呢? 这里介绍一种方法: 如果 ...