[LeetCode C实现]287. Find the Duplicate Number

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.

Note:

You must not modify the array (assume the array is read only).

You......

[LeetCode C实现]142. Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

Note: Do not modify the linked list.

Follow up:

Can you solve it without using extra space?

/**

* Definition for singly-linked list.

* struct ListNode {

* int val;

* struct ListNode *ne......

[LeetCode C实现]141. Linked List Cycle

Given a linked list, determine if it has a cycle in it.

Follow up:

Can you solve it without using extra space?

/**

* Definition for singly-linked list.

* struct ListNode {

* int val;

* struct ListNode *next;

* };

*/

bool hasCycle(struct ListNode *head)

{

if(NULL == head)

return false......

C语言宏max

在leetcode刷题LeetCode C 实现110. Balanced Binary Tree时有如下两个宏:

#define max(a,b) (((a) > (b))?(a):(b))

#define max(a,b) ((a) > (b))?(a):(b)

在编写代码时我写成了后者,在检查了代码之后提交始终失败,gdb调试时发现max这个宏存在问题。

1 + max(a, b)

相当于:

1 + ((a) > (b))?(a):(b)

考虑运算符优先级+大于?:,从右到左,因此相当于

(1 + ((a) > (b))) ? (a) : (......

[LeetCode C 实现]412. Fizz Buzz

Write a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

Example:

n = 15,

......

[LeetCode C实现]771. Jewels and Stones

You're given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels.

The letters in J are guaranteed distinct, and all characters i......

[LeetCode C 实现]110. Balanced Binary Tree

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as:

a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

/**

* Definition for a binary tree node.

* struct TreeNode {

* int val;

* ......

[LeetCode C 实现]657. Judge Route Circle

Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.

The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are R (Right), L (Lef......

[LeetCode C 实现]669. Trim a Binary Search Tree

Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in L, R. You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree.

递归的思想在二叉树的应用:

当root的值位于L和R之间,则递归修剪其左右子树,返回roo......

[LeetCode C 实现]541. Reverse String II

Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k char......