摘要:
1 class Solution { 2 public: 3 void sortColors(int A[], int n) { 4 auto swap = [](int &a, int &b){int t = a; a = b; b = t;}; 5 in... 阅读全文
posted @ 2015-03-23 15:11
keepshuatishuati
阅读(127)
评论(0)
推荐(0)
摘要:
1 class Solution { 2 public: 3 int singleNumber(int A[], int n) { 4 int ones = 0, twos = 0, threes = 0; 5 for (int i = 0; i < n; ... 阅读全文
posted @ 2015-03-23 15:08
keepshuatishuati
阅读(141)
评论(0)
推荐(0)
摘要:
You can use a hash map to record the frequencys. Or you can use bit operation.x ^ x = 0. So the only left is the one. 1 class Solution { 2 public: 3 ... 阅读全文
posted @ 2015-03-23 15:05
keepshuatishuati
阅读(81)
评论(0)
推荐(0)
摘要:
I split it in the reverse way. So all the following code should do reverse way. 1 class Solution { 2 public: 3 vector splits(string s) { 4 ... 阅读全文
posted @ 2015-03-23 15:00
keepshuatishuati
阅读(123)
评论(0)
推荐(0)
摘要:
You can use two array to record the zeros. Also you can do it in memory: 1 class Solution { 2 public: 3 void setZeroes(vector > &matrix) { 4 ... 阅读全文
posted @ 2015-03-23 13:50
keepshuatishuati
阅读(107)
评论(0)
推荐(0)
摘要:
Regular binary search 1 class Solution { 2 public: 3 int searchInsert(int A[], int n, int target) { 4 int start = 0, end = n-1, mid = 0; 5... 阅读全文
posted @ 2015-03-23 13:23
keepshuatishuati
阅读(94)
评论(0)
推荐(0)
摘要:
Just skip the duplicated ones. 1 class Solution { 2 public: 3 bool search(int A[], int n, int target) { 4 int start = 0, end = n-1, mid = ... 阅读全文
posted @ 2015-03-23 13:13
keepshuatishuati
阅读(120)
评论(0)
推荐(0)
摘要:
Two condition need to equal:1. A[mid] >= A[start]. Because mid = (start + end)/2; it shifts left2. A[end] >= target. Since did not check A[end] now. 1... 阅读全文
posted @ 2015-03-23 13:05
keepshuatishuati
阅读(119)
评论(0)
推荐(0)
摘要:
Actually, we are searching the right end of the target so:1. start could be same as end2. A[mid] > target, shift to left3. A[mid] searchRange(int A[]... 阅读全文
posted @ 2015-03-23 12:56
keepshuatishuati
阅读(122)
评论(0)
推荐(0)
摘要:
Binary search. Just need to convert index. 1 class Solution { 2 public: 3 bool searchMatrix(vector > &matrix, int target) { 4 if (matrix.s... 阅读全文
posted @ 2015-03-23 12:32
keepshuatishuati
阅读(125)
评论(0)
推荐(0)
摘要:
1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(i... 阅读全文
posted @ 2015-03-23 11:45
keepshuatishuati
阅读(98)
评论(0)
推荐(0)
摘要:
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), ne... 阅读全文
posted @ 2015-03-23 11:44
keepshuatishuati
阅读(124)
评论(0)
推荐(0)
摘要:
This is a math trick. 1 class Solution { 2 public: 3 void rotate(vector > &matrix) { 4 auto swap = [](int &a, int &b){int t = a; a = b; b... 阅读全文
posted @ 2015-03-23 10:48
keepshuatishuati
阅读(131)
评论(0)
推荐(0)
摘要:
Note:1. K might be very very large. So remember to module it with n.2. two range reverse (0, k-1), (k, n-1). 1 class Solution { 2 public: 3 void r... 阅读全文
posted @ 2015-03-23 10:37
keepshuatishuati
阅读(120)
评论(0)
推荐(0)
摘要:
1 class Solution { 2 public: 3 int romanToInt(string s) { 4 int len = s.size(), result = 0; 5 for (int i = 0; i < len; i++) { 6 ... 阅读全文
posted @ 2015-03-23 10:28
keepshuatishuati
阅读(112)
评论(0)
推荐(0)
摘要:
Reverse in place:1. reverse the whole sentence.2. reverse every word.We cant do it for I, because there are extra spaces in sentence. 1 class Solution... 阅读全文
posted @ 2015-03-23 10:20
keepshuatishuati
阅读(128)
评论(0)
推荐(0)
摘要:
1 class Solution { 2 public: 3 void reverseWords(string &s) { 4 string result; 5 int len = s.size(); 6 for (int i = len-1... 阅读全文
posted @ 2015-03-23 10:13
keepshuatishuati
阅读(95)
评论(0)
推荐(0)
摘要:
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), ne... 阅读全文
posted @ 2015-03-23 10:08
keepshuatishuati
阅读(110)
评论(0)
推荐(0)
摘要:
Same thing need to as :Whether m > n, m > list size, or n > list size 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * in... 阅读全文
posted @ 2015-03-23 09:50
keepshuatishuati
阅读(98)
评论(0)
推荐(0)
摘要:
Remember to check overflows. 1 class Solution { 2 public: 3 int reverse(int x) { 4 int sign = x 0) { 7 if (result > (INT_MAX ... 阅读全文
posted @ 2015-03-23 05:55
keepshuatishuati
阅读(99)
评论(0)
推荐(0)
摘要:
Two method of making that:1. As we know this is 32 bit integer. We just get the bit and shift to 31 - i. 1 class Solution { 2 public: 3 uint32_t r... 阅读全文
posted @ 2015-03-23 05:50
keepshuatishuati
阅读(114)
评论(0)
推荐(0)
摘要:
1 class Solution { 2 public: 3 bool isIp(string s) { 4 int len = s.size(), ip = 0; 5 if (len == 0 || len > 4 || len > 1 && s[0] =... 阅读全文
posted @ 2015-03-23 05:41
keepshuatishuati
阅读(124)
评论(0)
推荐(0)
摘要:
1. Find mid point and break it into two lists2. reverse the second list (mid to end)3. reinsert into first one. 1 /** 2 * Definition for singly-linke... 阅读全文
posted @ 2015-03-23 05:31
keepshuatishuati
阅读(143)
评论(0)
推荐(0)
摘要:
If interview did not mention n's situation, please ask.It will affect the code. EX: n = 10, List : 1 2 3. 1 /** 2 * Definition for singly-linked list... 阅读全文
posted @ 2015-03-23 05:24
keepshuatishuati
阅读(144)
评论(0)
推荐(0)
浙公网安备 33010602011771号