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. AFHTTPSessionManager下载文件 及下载中 进度条处理,进度条处理需要特别注意,要加载NSRunLoop 中

    1.下载文件 和进度条处理代码 - (void)timer:(NSTimer *)timer{ // 另一个View中 进度条progress属性赋值 _downloadView.progress = ...

  2. [题解](最短路)luogu_P5122 Fine Dining

    首先理解这里的美味值相当于给你更多时间让你经过这个草垛的, 也就是在经过草垛时可以给你的时间减少w[i],这样能否比最短路不慢 然而我们并不容易知道怎么走才是最好的,所以要想办法避免找这个方案 我们新 ...

  3. 110 Balanced Binary Tree 平衡二叉树

    给定一个二叉树,确定它是高度平衡的.对于这个问题,一棵高度平衡二叉树的定义是:一棵二叉树中每个节点的两个子树的深度相差不会超过 1.案例 1:给出二叉树 [3,9,20,null,null,15,7] ...

  4. Django 使用allauth报错

    一:报错 RuntimeError: Model class django.contrib.sites.models.Site doesn't declare an explicit app_labe ...

  5. linux安装redis官方教程

    官方链接:http://redis.io/download Download, extract and compile Redis with: $ wget http://download.redis ...

  6. 解决win64无法添加curl扩展的问题

    网上试了很多方法都无效,最后直接用phpstudy集成安装包中对应版本的php_curl.dll替换即可

  7. Alpha-beta pruning

    function alphabeta(node, depth, α, β, maximizingPlayer) or node is a terminal node return the heuris ...

  8. X11/extensions/XShm.h: No such file or directory

    CentOS 编译一些开源项目提示:X11/extensions/XShm.h: No such file or directory. 运行命令:yum install libXext-devel就可 ...

  9. Kafka-broker配置说明

    配置文件在config/server.properties 下面的一些配置可能是你需要进行修改的. broker.id 整数,建议根据ip区分 log.dirs kafka存放消息文件的路径, 默认/ ...

  10. Asp.Net Core 进阶(一) —— 读取appsettings.json

    我们以前在Asp.Net MVC中使用 System.Configuration.ConfigurationManager 来读取web.config文件.但是Asp.Net Core MVC已经没有 ...