大数据加减法
一次AC附上代码,可能不是最优。
class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
// Note: The Solution object is instantiated only once and is reused by each test case.
vector<int> ans(digits.size()+1);
for(int i = 0……
删除已排好序的重复的元素。
不许开辟额外的空间。
插排,交换顺序到队尾。
和上一个题有点点像。
一次AC,附上代码:
class Solution {
public:
int removeDuplicates(int A[], int n) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int count = 0;……
这个题就是个坑!英文一定要学好!题意一定要理解对。
我一开始以为自己求删除之后的长度就可以了!其实不止如此!你还要让A数组前length符合要求。
开始WA
后来一次AC
附上代码:
class Solution {
public:
int removeElement(int A[], int n, int elem) {
// Note: The Solution object is instantiated o……
爬楼梯,那么一次爬一步,要么一次爬两步。问你有几种爬法?
搜索?
会超时
后来改成迭代:
a[n] = a[n-1] + a[n-2]
一次AC
class Solution {
public:
int climbStairs(int n) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int a1 = 1;
int……
去除链表中的重复数值。这个题 怎么说 现在发现,链表的题,最好存before, now , next这样的话 实现逻辑就很简单了~
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
// Note: The Solution object is instantiated only once and is reused by each test case.
ListNode *……
简单题,二分。
找到了返回下标,找不到返回应该放的位置的下标。
class Solution {
public:
int ans;
int searchInsert(int A[], int n, int target) {
// Note: The Solution object is instantiated only once and is reused by each test case.
ans = 0;
bsearch(0,n-1,A,target);
……
比较两棵树是否完全一样。
一次AC,枚举即可。
class Solution {
public:
bool ans;
bool isSameTree(TreeNode *p, TreeNode *q)
{
// Note: The Solution object is instantiated only once and is reused by each test case.
ans = true;
check(p,q);
return ans;
……
题意很简单,给一颗二叉树,请问该树的深度。
RE一次,CE一次[提交代码重定义]
最后AC
RE需要注意的是:root如果是NULL需要直接返回0;
其他DFS即可。
附上代码:
</p>
<p>class Solution {
public:
int depth;
int maxDepth(TreeNode *root) {
// Note: The Solution object is instantiat……
输入一个数组,里面每个元素都有两个,但是有一个元素只有一个。
请在不消耗额外内存空间的情况下,找出那个单一的元素并输出。
此题用异或即可。
不会消耗额外的内存空间。
一次AC,附上代码:
class Solution {
public:
int singleNumber(int A[], int n) {
// Note: The Solution object is instantiated……
这个和上一个题相反。
嘿嘿嘿,比较猥琐的我本来是想用上一个题,打一个3999的表,但是我觉得这样太不厚道了。
于是我选择了好好做题= =
当然上一个题的代码太多的switch 和 if else
所以。。。
我决定改进一下。
附上代码:依然是一次AC
class Solution {
public:
int num;
int romanToInt(string s) {
……