class Solution {
public:
int findComplement(int num) {
int countDigit = 0;
int numBak = num;
while(0 < num) {
num = num >> 1;
countDigit++;
}
return (int)(pow(2, countDigit) - 1 - numBak);<br />
}
}……
class Foo {
private:
bool isFirst;
bool isSecond;
bool isThird;
public:
Foo() {
isFirst = false;
isSecond = false;
isThird = true;
}</p>
<pre><code>void first(function&lt;void()&gt; printFirst) {
while (!isThird);
……
此题就是二分的变种,但是千万要注意大整数求和越界,正确的计算方式应该是 left + (right – left) / 2; 我使用的是long long.
// Forward declaration of isBadVersion API.
bool isBadVersion(int version);</p>
<p>class Solution {
public:
int bSearch(long long left, long long right) ……
此提示单链表判断回文,我本来的思路是 逆转单链表前半截,在跑一次,应该能满足时间复杂度o(n)和空间复杂度o(1),但是不知道为啥老判我超时。
看了discuss和hint,其实简单递归一下就可以了。
class Solution {
public:
ListNode * temp;
bool isPalindrome(ListNode* head) {
temp = head;
re……
此题给的是一个数组,求这个数组中的子数组乘积最大值,考虑正负数和0的情况。hint给的解法应该是每个值求一个max和min,然后用max和min来生成算上当前点的最大值和最小值。我自己用了一个模拟。所有数求乘积,如果为正则为最大,如果为负则比较抛弃最左边或最右边的负数。考虑到0的情况,先用0分界。
class Solution {
……
这个题用一个hash存一下 两个字符串的对应关系就好了。我开始以为只有小写字母,其实是所有ASCII字符。开始用一个hash存的,后来因为一一对应需要用两个hash存。
class Solution {
public:
bool isIsomorphic(string s, string t) {
if (s.length() != t.length()) return false;
char hash_s[256];……
此题我用的是 二分查找,毕竟有序嘛。其实更好的解法,应该是 一个数组,left right下标往里移动查找,这个题我曾经应该在编程之美或者剑指offer上看到过。
class Solution {
public:
vector<int> result;<br />
void bSearch(vector<int>&numbers, int start, int e……
此题就是一个联表查询,不过我当时认为join就是left join 没有用on 用的where结果做成内联了,错了几次。
select p.FirstName, p.LastName, a.City, a.State from
Person as p left join Address as a on p.PersonId=a.PersonId
快两年没做题了,压力好大,最近练习一下,保持一种感觉。
这个题据说是《剑指offer》上的一个题,其实我都解法明显有问题,但是还是AC了,我发现PHP,JS,C#写久了,我连Verctor都快不会写了。
此题正确的解法应该是用一个栈来维护最小值,是当前最小值即进入一个栈。另一个栈放数据,即可保证复杂度在O(n),查询复杂度O……
<?php
class Solution
{
public function containsDuplicate($nums)
{
$hashTable = array();
foreach ($nums as $num)
{
if (!array_key_exists($num,$hashTable))
{
$hashTable[$num] = 1;
}
else
……