
LeetCode 刷题
guilanl
这个作者很懒,什么都没留下…
展开
-
C++ string 的基本操作(不断更新中)
C++ string 的操作,这篇博客里面讲的比较清楚:c++中string类的详解下面是一些我自己使用过的一些string 的用法:1. 头文件#include // 注意是,不是,带.h的是C语言中的头文件using std::string;using std::wstring;或using namespace std;原创 2015-10-22 16:45:56 · 405 阅读 · 0 评论 -
LeetCode 刷题:move 一个数组中的所有0 到数组最后
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling you原创 2015-10-27 22:37:56 · 973 阅读 · 0 评论 -
LeetCode 刷题:int 型数如果转变为二进制数,二进制数里面有几个1
Number of 1 BitsWrite a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has bi原创 2015-10-29 22:13:02 · 455 阅读 · 0 评论 -
LeetCode 刷题: 删除已排序链表中的重复节点
Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.一种解法:/** * Definitio原创 2015-12-11 23:12:41 · 352 阅读 · 0 评论 -
LeetCode 刷题: Happy Number 的判断
Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares原创 2015-12-28 15:38:12 · 1644 阅读 · 1 评论 -
LeetCode 刷题: 丑数 ugly number 判断方法
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly sinc原创 2015-12-16 11:37:28 · 544 阅读 · 0 评论 -
LeetCode刷题: Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.You should try to do it in原创 2016-02-05 16:40:46 · 350 阅读 · 0 评论 -
LeetCode 刷题: 合并两个有序链表 (merge two sorted list)
题目: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.合并两个有序链表,新链表的元素就是之前的两个链表里面的元素。程序:/** * De原创 2016-01-13 14:05:57 · 393 阅读 · 0 评论 -
LeetCode刷题: power of two (判断一个数是不是2的幂次方)
Given an integer, write a function to determine if it is a power of two.下面是我在网上看到的一种方面,利用了2幂次方的特点如果是power of two, 则2进制表达中,有且仅有一个1. 可以通过移位来数1的个数, 这里用了一个巧妙的办法, 即判断 N & (N-1) 是否为0. class原创 2016-01-14 14:17:15 · 825 阅读 · 0 评论 -
LeetCode 刷题 -- power of three
题目:Given an integer, write a function to determine if it is a power of three.Follow up:Could you do it without using any loop / recursion?解法1:在网上看到的解决办法:class Solution {public原创 2016-04-18 16:27:18 · 423 阅读 · 0 评论 -
LeetCode刷题 -- Best Time to Buy and Sell Stock
题目:Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the原创 2016-05-17 11:13:55 · 291 阅读 · 0 评论 -
LeetCode 刷题4 -- 删除单链表的节点
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value原创 2015-10-16 13:50:53 · 314 阅读 · 0 评论 -
typedef 一个struct
下面的struct:struct node{int data;struct node * next;};要用的时候,必须这样调用:struct node n1;struct node *n2;简单化方法:typedef struct node{int data;struct node *next;} Node, *原创 2015-10-27 22:43:55 · 339 阅读 · 0 评论 -
LeetCode 刷题: Climbing Stairs --- 类似于Fibonacci 数列
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?这道题目类似于Fibonacci 数列。解法有两原创 2015-11-20 23:45:15 · 421 阅读 · 0 评论 -
如何将int型转换成字符型
比如: 24 ---> '24'.1. 如果要转换的数字是0 -- 9,那么可以这样: int i = 1;char c = 1+'0'2. 如果是个多位数,如 24, 那么最好这样char buffer[50]; //check if 50 is enoughint i = 45;sprintf(buffer, '%d', i);原创 2015-10-22 16:34:16 · 3177 阅读 · 0 评论 -
散列表的基本概念
网上转来的,拼拼凑凑。我们知道数组能够提供对元素的快速访问但难于扩展;链表易于扩展但不能对其元素进行快速访问。对于大量元素的数据来说,我们当然希望两全其美。散列表提供了达到此目标的一种方法。 散列表(也叫哈希表)是一种查找算法,与链表、树等算法不同的是,散列表算法在查找时不需要进行一系列和关键字(关键字是数据元素中某个数据项的值,用以标识一个数据元素)的比转载 2015-10-20 10:58:23 · 479 阅读 · 0 评论 -
二叉树的遍历 -- 递归和非递归方法
发现有个博主讲的非常好,代码清晰,转如下:http://blog.youkuaiyun.com/sjf0115/article/details/8645991树形结构是一类重要的非线性数据结构,其中以树和二叉树最为常用。二叉树是每个结点最多有两个子树的有序树。通常子树的根被称作“左子树”(left subtree)和“右子树”(right subtree)。二叉树常转载 2015-10-16 16:54:38 · 359 阅读 · 0 评论 -
LeetCode 刷题1
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the原创 2015-10-15 13:57:51 · 353 阅读 · 0 评论 -
LeetCode 刷题2 (digit sum)
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has on原创 2015-10-15 14:59:56 · 389 阅读 · 0 评论 -
LeetCode 刷题3 -- 二叉树的最大深度
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.我借鉴的代码(非递归实现)/** * Definition原创 2015-10-16 11:30:48 · 443 阅读 · 0 评论 -
LeetCode 刷题: 左右反转一个二叉树
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas原创 2015-10-28 22:26:08 · 618 阅读 · 0 评论 -
LeetCode 刷题: 两个二叉树节点的最近公共节点Least Common Ancestor
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined betw原创 2015-10-29 23:00:54 · 422 阅读 · 0 评论 -
LeetCode 刷题: Roman to Integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.一些背景知识:【罗马数字】1~9: {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}原创 2015-11-05 12:44:37 · 307 阅读 · 0 评论 -
LeetCode 刷题 -- 反转一个单链表
Reverse a singly linked list.非递归法实现:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */cla原创 2015-11-13 16:07:56 · 814 阅读 · 0 评论 -
leetcode 刷题之: reverse string
Example:Given s = "hello", return "olleh".Subscribe to see which companies asked this questionclass Solution {public: string reverseString(string s) { int len = s.lengt原创 2016-10-06 00:17:30 · 281 阅读 · 0 评论