摘要: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.Example:Given a = 1 and b = 2, return 3.Credits:Special thanks to @fujiaozhu for adding this problem and ... 阅读全文
posted @ 2017-01-10 23:21 xiejunzhao 阅读(120) 评论(0) 推荐(0)
摘要: Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), des... 阅读全文
posted @ 2017-01-10 23:19 xiejunzhao 阅读(135) 评论(0) 推荐(0)
摘要: 题目要求求出长度即可,并不需要求出最长回文串。思路:用字典统计每一个字符的出现次数,出现次数大于1的字符必定出现在回文串中,另外还再加上一个中心点。public static int LongestPalindrome(string s) { int length = 0; Dictionary dictionary = new Dictionary(); int value = 0; forea... 阅读全文
posted @ 2017-01-10 23:18 xiejunzhao 阅读(121) 评论(0) 推荐(0)
摘要: Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of ... 阅读全文
posted @ 2017-01-10 23:18 xiejunzhao 阅读(287) 评论(0) 推荐(0)
摘要: 假设str长度为len,重复的子串长度为k,则如果真的由连续多个长度为k的子串重复构成str,那么在对str求next时,由于连续对称性(如图,前后两个虚线框内字符串相等),会从next[k+1]开始,1,2,3...地递增,直到next[len]=len-k,且(len-k)%k==0,表示有整数个k要一直求到next[len]而不是next[len-1],是因为next[len-1]只是表示前... 阅读全文
posted @ 2017-01-10 23:14 xiejunzhao 阅读(215) 评论(0) 推荐(0)
摘要: //循环版本public static bool IsPowerOfThree(int n) { if (n == 1) { return true; } double num = n; bool isPower = false; while (num >= 1) { num = num / 3; if (num == 1 && num == (int)num) { return tru... 阅读全文
posted @ 2017-01-10 23:14 xiejunzhao 阅读(572) 评论(0) 推荐(0)
摘要: Power of Two Total Accepted: 3596 Total Submissions: 11779Given an integer, write a function to determine if it is a power of two.Credits:Special thanks to @jianchao.li.fighter for adding this problem... 阅读全文
posted @ 2017-01-10 23:14 xiejunzhao 阅读(229) 评论(0) 推荐(0)
摘要: public class Solution { public int HammingDistance(int x, int y) { int distance = 0; string sX = Convert.ToString(x, 2); string sY = Convert.ToString(y, 2); int maxLength = Math.Max(sX.Length, ... 阅读全文
posted @ 2017-01-10 23:13 xiejunzhao 阅读(204) 评论(0) 推荐(0)
摘要: Reverse a singly linked list. 递归实现 阅读全文
posted @ 2017-01-10 23:12 xiejunzhao 阅读(118) 评论(0) 推荐(0)
摘要: 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 yo... 阅读全文
posted @ 2017-01-10 23:11 xiejunzhao 阅读(124) 评论(0) 推荐(0)