原创 LeetCode 141 Container With Most Water

Given n non-negative integers a1, a2, ..., an, where each represents a poi

原创 Q7.1.3 找出字符串中出現最多的字母及次數

題目: 一個字符串中可能包含a~z中的多個字符,如有重複,求出現次數最多的那個字母及次數,如有多個重複的則都求出。 分析: 題目限定只有a~z,所以開闢一個長度26的數組bucket即可,數組元素記錄相應出現的次數, 一次遍歷,bucke

原创 Q7.1.1 把一個數組裏的數的組合全部列出

題目: 把一個數組裏的數的組合全部列出,比如1和2列來爲1,2,12,21. 分析: 這道題有多種擴展, 1,沒有重複元素的數的組合(包括子集的全排列); 2,有重複元素的數的組合; 3,沒有重複元素的數的全排列; 4,有重複元素的數的全

原创 LeetCode 148 Add Two Numbers

You are given two linked lists representing two non-negative numbers. The

原创 CareerCup 17中等難題 Q17.2 井字遊戲

題目: 設計一個算法,判斷玩家是否贏了井字遊戲。 分析: 就是判斷一個N*N矩陣,是否存在一行,一列,兩對角線上元素全部相等。 package cci.section17; public class CCI_17_2 { pub

原创 Q11.3.4 哈弗曼編碼

問題: 用二進制來編碼字符串“abcdabaa”,需要能夠根據編碼,解碼回原來的字符串,最少需要多長的二進制字符串? 分析: 哈弗曼編碼根據字符出現的概率來構造平均長度最短的碼字,是一種文本壓縮算法。 1,先統計各字符出現的頻率; 2,構

原创 LeetCode 146 ZigZag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to displa

原创 LeetCode 150 Median of Two Sorted Arrays

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall

原创 LeetCode 149 Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substri

原创 LeetCode 140 Integer to Roman

Given an integer, convert it to a roman numeral.Input is guaranteed to be

原创 LeetCode 143 Palindrome Number

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

原创 CareerCup 17中等難題 Q17.1 不用臨時變量,直接交換兩個數

題目: 編寫一個函數,不用臨時變量,直接交換兩個數。 分析: 這是位運算異或的典型應用,已經上了百度百科。 對兩個數進行三次異或就可以。 package cci.section17; public class CCI_17_1 {

原创 CareerCup 17中等難題 Q17.3 階乘尾隨零個數

題目: 設計一個算法,算出n階乘有多少個尾隨零。 分析: 就是算,階乘中總共有幾個 2*5,又因爲2總是比5多,所以算出有幾個5相乘就可以。 注意:25算兩個,因爲5*5, 125算三個,因爲5*5*5. 具體算法是這樣, 第一遍,算階乘

原创 LeetCode 151 Two Sum

Given an array of integers, find two numbers such that they add up to a sp

原创 LeetCode 142 Regular Expression Matching

Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero