[LeetCode C实现] 796.Rotate String

We are given two strings, A and B.

A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea' after one shift on A. Return True if and only if A can become B after some number of shifts o......

[LeetCode C实现] 169. Majority Element

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

思路分析

这道题目有一个限制条件是majority element元素总是存在的,这样的话在编写代码的时候就相对简单了,思路也比较巧妙,代码如下:

......

[LeetCode C实现] 448. Find All Numbers Disappeared in an Array

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

Find all the elements of [1, n] inclusive that do not appear in this array.

Could you do it without extra space and in O(n) runtime? You may assume the returned list does not co......

[LeetCode C 实现]206. Reverse Linked List

Reverse a singly linked list.

常规方法,遍历一遍,迭代计算。

/**

* Definition for singly-linked list.

* struct ListNode {

* int val;

* struct ListNode *next;

* };

*/

struct ListNode* reverseList(struct ListNode* head)

{

if(NULL == head)

return NULL;

struct ListNode * p = head->next;

/*第一个结点的next置为N......

[LeetCode C 实现]226. Invert Binary Tree

Invert a binary tree.

/**

* Definition for a binary tree node.

* struct TreeNode {

* int val;

* struct TreeNode *left;

* struct TreeNode *right;

* };

*/

struct TreeNode* invertTree(struct TreeNode* root)

{

if(NULL == root)

return NULL;

struct TreeNode* tmp = root->left;

root->......

[LeetCode C 实现]283. Move Zeroes

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 your function, nums should be [1, 3, 12, 0, 0].

Note:

You must do this in-place without making ......

[LeetCode C 实现]136. Single Number

Given an array of integers, every element appears twice except for one. Find that single one.

Note:

Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

int singleNumber(int* nums, int numsSize)

{

int result = 0;

for(int i = 0;i < nums......

[LeetCode C实现]617. Merge Two Binary Trees

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.

You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the me......

[LeetCode C 实现]461. Hamming Distance

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, calculate the Hamming distance.

int hammingDistance(int x, int y)

{

int cnt = 0;

int result = x^y;

while(result)

{

if(result &1)

cnt++;

result &......

[LeetCode C实现]258. Add Digits

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 only one digit, return it.

Follow up:

Could you do it without any loop/recursion in O(1) runtime?

i......