[LeetCode C实现]191. Number of 1 Bits

Write 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 binary representation 00000000000000000000000000001011, so the function should return 3.

常规思路,每次最低位与1,如果为1则右移一位,cnt++,如果......

[LeetCode C实现] 70. Climbing Stairs

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?

Note: Given n will be a positive integer.

int climbStairs(int n)

{

/*递归实现更好理解,但性能存在问题*/

if(n <= 0)

return 0;

if(1 == n)......

[LeetCode C 实现]674. Longest Continuous Increasing Subsequence

Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).

Example 1:

Input:

Output: 3

Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3.

Even though [1,3,5,7] is also an increasing subsequence, it'......

[LeetCode C 实现]82. Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

For example,

Given 1->2->3->3->4->4->5, return 1->2->5.

Given 1->1->1->2->3, return 2->3.

/**

* Definition for singly-linked ......

[LeetCode C 实现]83. Remove Duplicates from Sorted List

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.

/**

* Definition for singly-linked list.

* struct ListNode

{

* int val;

* struct ListNo......

[LeetCode C实现]50. Pow(x,n)

Implement pow(x, n).

Example 1:

Input: 2.00000, 10

Output: 1024.00000

Example 2:

Input: 2.10000, 3

Output: 9.26100

double myPow(double x, int n)

{

if(n == 0)

return 1.0;

if(n == 1)

return x;

long long int m = n > 0 ? n : -n;

double ans = x;

long long int cur = 1.0;

while(2 * cur &l......

[LeetCode C 实现]100. Same Tree

Given two binary trees, write a function to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical and the nodes have the same value.

/**

* Definition for a binary tree node.

* struct TreeNode {

* int val;

* struct TreeNode *left;

* ......

[LeetCode C实现] 67.Add Binary

Given two binary strings, return their sum (also a binary string).

For example,

a = "11"

b = "1"

Return "100".

char* addBinary(char* a, char* b)

{

int la = strlen(a);

int lb = strlen(b);

int lr = la > lb ? la : lb;

int carry = 0;

/*可能存在进位,计算前无法预判,多申请进位和\0*/......

[LeetCode C实现]23. Merge k sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

实现思路:由于在LeetCode C实现 21. Merge two sorted Lists中我们已经完成了两个有序链表的合并,所以k个有序链表的合并可以依次合并,经过k-1次合并成一个链表。

实现代码如下:

方法一:

/**

* Definition for singly-linked list.

* struct ListNode {

* int val;

* str......

[LeetCode C实现]9. Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.

click to show spoilers.

Some hints:

Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

You could also try reve......