leetCode笔记--(1)
陪朋友刷题,记录下。
1.
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +, -, *, /. Each operand may be an integer or another expression.
Note:
Division between two integers should truncate toward zero.
The given RPN expression is always valid. That means the expression would always evaluate to a result and there won't be any divide by zero operation.
出错的地方:
Q1. memcmp的误用引发的内存堆栈溢出,可能由于编译器的差别,本地OK,valgrind ok,但是远端ERROR。
修复用strcmp,按字节比,直到1个到尾。
Q2. 二级指针的运用
char* acSrc[5] = {"2", "1", "+", "3", "*"};
char** ppTcSrc = NULL;
ppTcSrc = acSrc;
取“2”, “1” 这些成员,原先取 *ppTcSrc + 0, *ppTcSrc + 1
129 printf("%s ", *ppTcSrc);
(gdb) n
140 return 0;
(gdb) p *ppTcSrc
$1 = 0x400bcb "2"
(gdb) p *ppTcSrc + 1
$2 = 0x400bcc ""
(gdb) p *ppTcSrc + 2
$3 = 0x400bcd "1"
(gdb) p *ppTcSrc + 3
$4 = 0x400bce ""
(gdb) p *ppTcSrc + 4
$5 = 0x400bcf "3"
(gdb) p *ppTcSrc + 5
$6 = 0x400bd0 ""
(gdb) p *ppTcSrc + 6
$7 = 0x400bd1 "%s "
本意是ppTcSrc[0], ppTcSc[1]. ... 也可以写成*(ppTcSrc), *(ppTcSrc + 1)
(gdb) p *(ppTcSrc + 2)
$10 = 0x400ba0 "+"
(gdb) p ppTcSc[2]
No symbol "ppTcSc" in current context.
(gdb) p *(ppTcSrc + 2)
$11 = 0x400ba0 "+"
(gdb) p ppTcSrc[2]
$12 = 0x400ba0 "+"
## 还是要注意下, ppTcSrc 的类型是char*, 想下他的指向,他的自加是什么意思。
2.
leetcode 7:
Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.
Q1:
AddressSanitizer: heap-buffer-overflow on address 0x602000000298 at pc 0x7f6c0babb79b bp 0x7ffdfb3aca30 sp 0x7ffdfb3ac1e0
原因分析:
memcmp 的S使用,可能是编译器差距,本地OK。
解决方法:memcmp -> strncmp
3.405. Convert a Number to Hexadecimal
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.
Note:
All letters in hexadecimal (a-f) must be in lowercase.
The hexadecimal string must not contain extra leading 0s. If the number is zero, it is represented by a single zero character '0'; otherwise, the first character in the hexadecimal string will not be the zero character.
The given number is guaranteed to fit within the range of a 32-bit signed integer.
You must not use any method provided by the library which converts/formats the number to hex directly.
Example 1:
Input:
26
Output:
"1a"
-- 这个做的很开心,虽然折腾了下,但是还是搞定了。^_^
Q1:
# 负数问题
负数是有符号的,当想要转成字符串,需要先转成无符号的
# 补码问题
负数,计算机用补码表示。
$15 = -1
(gdb) p /x iRh
$16 = 0xffffffff
(gdb) p /x iRh>>4
$17 = 0xffffffff
# 值范围
unsigned int 0~4294967295
int 2147483648~2147483647
unsigned long 0~4294967295
long 2147483648~2147483647
long long的最大值:9223372036854775807
long long的最小值:-9223372036854775808
unsigned long long的最大值:1844674407370955161
__int64的最大值:9223372036854775807
__int64的最小值:-9223372036854775808
unsigned __int64的最大值:18446744073709551615
# -2147483648的补码
if (num == -2147483648) return 0xFFFFFFFF80000000;
git 地址:
https://github.com/HellsingAshen/Myleetcode-cn
leetCode笔记--(1)的更多相关文章
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- Leetcode 笔记 35 - Valid Soduko
题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...
- Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II
题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...
随机推荐
- HDU 4512 最长公共上升子序列
各种序列复习: (1)最长上升子序列. 1.这个问题用动态规划就很好解决了,设dp[i]是以第i个数字结尾的上升子序列的最长长度.那么方程可以是dp[i]=max(dp[j]+1).(j<i). ...
- POJ 1175
//本来写了个和1021相同的HASH,但没过,于是,抱着侥幸的心理,把它变成距离的四次方, //我就呵呵了... //这个题,完全靠概率.当然了,如果是把图翻转来比较,也是可以的.但好像很麻烦.. ...
- RubyMine中自动完成只输入部分字母
RubyMine中自动完成只输入部分字母 1,有下划线情况(其实看第二点跟下划线就关系不大了) 对于attr_reader之类的输入,输入attr之后,下划线可以不输入,然后输入r或者e都可以出来, ...
- Oracle-11-主键约束
一.Oracle系统一共提供了下面5种约束 1.非空(NOT NULL)约束:所定义的列绝不能为空: 2.唯一(UNIQUE)约束:在表中每一行中所定义的列,其列值不能同样: 3.主键(PARIMAR ...
- 自己定义隐式转换和显式转换c#简单样例
自己定义隐式转换和显式转换c#简单样例 (出自朱朱家园http://blog.csdn.net/zhgl7688) 样例:对用户user中,usernamefirst name和last name进行 ...
- ijkplayer视频播放
http://android-doc.com/androiddocs/2017/1018/5416.html https://www.2cto.com/kf/201801/714366.html ...
- bzoj4034: [HAOI2015]树上操作(树剖)
4034: [HAOI2015]树上操作 题目:传送门 题解: 树剖裸题: 麻烦一点的就只有子树修改(其实一点也不),因为子树编号连续啊,直接改段(记录编号最小和最大) 开个long long 水模版 ...
- [POJ 3621] Sightseeing Cows
[题目链接] http://poj.org/problem?id=3621 [算法] 01分数规划(最优比率环) [代码] #include <algorithm> #include &l ...
- typeof、instanceof、hasOwnProperty()、isPrototypeOf()
typeof 操作符 instanceof 操作符 hasOwnProperty()方法 isPrototypeOf()方法 1.typeof 用于获取变量的类型,一般只返回以下几个值:string, ...
- linux 免密登陆(超简单)
一.客户端生产公钥 在windwos上 生成公钥私钥前,先下载git哦 ssh-keygen -t rsa# 记住下方方框内公钥保存地址, 二.查看自己用户的登录地址 cat /etc/passwd ...