随笔分类 - C++
LeetCode, STL, 算法与数据结构
摘要:1 //最普通的变量交换 2 //优点:思路简单 3 //缺点:需要多余的存储空间 4 void swap1(int &a, int &b) 5 { 6 int c; 7 c=a; 8 a=b; 9 b=c; 10 } 11 12 //利用加减法的变量交换 13 //优点:不需要多余的存储空间 14 //缺点:技巧性操作,不容易想到 1...
阅读全文
摘要:题意是把一个二叉树转换成一个链表,链表的元素顺序是二叉树的先序遍历结果。 最简单的做法就是先序遍历该二叉树,将所有经过的节点的指针都依次记录在一个vector里,完成后,在将vector里的节点指针的左孩子置为NULL,右孩子置为vector中的下一个指针。具体的代码和运行效果如下 这里用了多余的空
阅读全文
摘要:Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maxim
阅读全文
摘要:Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeat
阅读全文
摘要:Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possib
阅读全文
摘要:题目描述开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。 输入: 合法坐标为A(或者D或者W或者S) + 数字(两位以内) 坐标之间以;分隔。 非法坐标点需要进行丢弃。如AA10; A1A; $%$; YAD; 等。 下面是一个简单的例子 如: A10;S20;W...
阅读全文
摘要:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then l
阅读全文
摘要:程序的输入都建有一个缓冲区,即输入缓冲区。一次输入过程是这样的,当一次键盘输入结束时会将输入的数据存入输入缓冲区,而cin函数直接从输入缓冲区中取数据。正因为cin函数是直接从缓冲区取数据的,所以有时候当缓冲区中有残留数据时,cin函数会直接取得这些残留数据而不会请求键盘输入一. cin>>该操作符是根据后面变量的类型读取数据。输入结束条件 :遇到Enter、Space、Tab键。对结束符的处理 ...
阅读全文
摘要:Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 两个字符串A和B, 在A中查找是否出现过B, 如
阅读全文
摘要:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 该题是要求合并两
阅读全文
摘要:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这道题如果有第21题 合并两个链表的基础就会比较容易,具体合并链表的时候有两种思路 (1)如果k个li
阅读全文
摘要:Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array whic
阅读全文
摘要:Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers.
阅读全文
摘要:Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: All root-to-leaf paths are:
阅读全文
摘要:112题目如下: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the giv
阅读全文
摘要:Implement atoi to convert a string to an integer. 题目分析: 题目本身很简单就是将一个字符串转化成一个整数,但是由于字符串的千差万别,导致在实现的时候细节非常多。需要注意的有以下一些: 1.字符串可能由一些空格开始,然后遇到一个正号或者负号,然后是正
阅读全文
摘要:leetcode网站原题如下:给一个非负数组,开始位置是下标为0的元素,数组的元素代表当前位置最多向前行进多少个位置,判断是否能到达数组的最后一个位置,能就回复true,不能就回复false. Given an array of non-negative integers, you are init
阅读全文
摘要:题目:算出二叉树的最大深度 解决方案:(1)BFS (2)DFS (1)BFS 一层一层往下搜索,一直找到最深的点,这里由于节点的val是没有用的,所以可以用来存储当前节点的深度,然后注意指针一定要初始化,不然在leetcode里可能会出现runtime error /** * Definition
阅读全文

浙公网安备 33010602011771号