去除链表中的重复数值。这个题 怎么说 现在发现,链表的题,最好存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) {
……
这个题就是给一个数(1-3999)转换为罗马计数,说简单不简单,说难不难。
附上百度百科:http://baike.baidu.com/link?url=TR8d0KXBBMWZffIDqBd_IFPSi0RZkB06DpUlsaZs-9_gCLNQaIVd2VmMwyf-3zG8
代码如下:
class Solution {
public:
string intToRoman(int num) {
// Note: The Solution object is instantiat……
好吧这个题我直接想没想出来,断断续续,后来忍不住去看了别人的思路。
先说说我的想法吧。
第一种思想就是暴力,每两个线段我都选出来,求个容积比一下,选最大。
思路逻辑都没错,但是超时了。我看了看test case数据量挺大的。
接下来怎么办呢,肯定要找到低于n^2复杂度的方法才有可能过。
想了好久,排序?自下而上的……
这个题简单,判断一个int是否是回文。
需要注意的是 负数不是回文。
最后就将int转为字符串直接判断就好。
一次AC。
class Solution {
public:
bool isPalindrome(int x) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(x < 0)
ret……
这个题真心想我理解了读题的重要性。
我就疯狂的WA啊。。。
题意很简单:
读入一个串,转化为int,如果越界,输出边界值。
但是需要注意+ – 符号
还需要值得注意的是:
先清空前面的所有空格,然后遇到第一个不是空格的字符开始识别,一直识别到不能识别为止。
但是特殊情况是:如果是‘ a123123123’这个串需要返回0……