leetcode
pkufergus
I am Fergus from PKU.
展开
-
Jump Game II
class Solution {public: int jump(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function int *dist = new int[n+1];dist[n - 1] = 0;int *next = new int[n+1];next[原创 2013-09-11 13:16:51 · 600 阅读 · 0 评论 -
4Sum
struct TwoSum{public: TwoSum(int idx1, int idx2, int v) : index1(idx1), index2(idx2), value(v) {}public: int index1; int index2; int value;};struct increaing { bool op原创 2014-01-16 14:29:15 · 659 阅读 · 0 评论 -
ZigZag Conversion
class Solution{public: string convert(string s, int nRows) { if (nRows < 2) return s; int N = s.size(); int L = 2 * (nRows - 1); // provide offset string res;原创 2014-01-16 14:19:23 · 585 阅读 · 0 评论 -
String to Integer (atoi)
class Solution {public: int atoi(const char *str) { // Start typing your C/C++ solution below // DO NOT write int main() function if (str == NU原创 2014-01-16 14:20:10 · 588 阅读 · 0 评论 -
RomantoInteger
//============================================================================// Roman to Integer// Given a roman numeral, convert it to an integer.//// Input is guaranteed to be within the range原创 2014-01-16 14:21:06 · 556 阅读 · 0 评论 -
Reverse Integer
class Solution {public: int reverse(int x) { // Start typing your C/C++ solution below // DO NOT write int main() function int ret; int sign = (x < 0); if原创 2014-01-16 14:21:54 · 464 阅读 · 0 评论 -
Palindrome Number
class Solution {public: bool isPalindrome(int x) { if (x < 0) return false; int d = 1; while (x / d >= 10) d *= 10; while (x != 0) { int l = x / d;原创 2014-01-16 14:23:31 · 430 阅读 · 0 评论 -
Median of Two Sorted Arrays
class Solution {public: double findKth(int A[], int m, int B[], int n, int k) { //m is equal or smaller than n if (m > n) return findKth(B, n, A, m, k); if原创 2014-01-16 14:24:13 · 524 阅读 · 0 评论 -
Longest Substring Without Repeating Characters
class Solution {public: int lengthOfLongestSubstring(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function int locs[256];//保存字符上一次出现的位置原创 2014-01-16 14:24:50 · 463 阅读 · 0 评论 -
Longest Common Prefix
class Solution {public: string longestCommonPrefix(vector &strs) { int N = strs.size(); if (N == 0) return ""; int l = 0; while (l < strs[0].size()) {原创 2014-01-16 14:25:57 · 430 阅读 · 0 评论 -
Container With Most Water
class Solution {public: int min(int a, int b){ if (a < b) { return a; } else { return b; } } int max(int a, int b){ if (a > b) {原创 2014-01-16 14:27:01 · 496 阅读 · 0 评论 -
Add Two Numbers
class Solution { public: ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { // Start typing your C/C++ solution below // DO NOT write int main() function ListNode *head; if (l1 == NU原创 2014-01-16 14:27:35 · 463 阅读 · 0 评论 -
3Sum Closest
class Solution {public: int threeSumClosest(vector &num, int target) { int N = num.size(); sort(num.begin(), num.end()); int res = num[0] + num[1] + num[2]; for (i原创 2014-01-16 14:28:47 · 468 阅读 · 0 评论 -
IntegertoRoman
//============================================================================// Given an integer, convert it to a roman numeral.//// Input is guaranteed to be within the range from 1 to 3999.//==原创 2014-01-16 14:26:24 · 446 阅读 · 0 评论 -
N-Queens
The n-queens puzzle is the problem of placing n queens on an n�n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens pu原创 2013-09-12 15:19:50 · 616 阅读 · 0 评论 -
N-Queens II
Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.class Solution {public: void backtrace(int &ans, vector &board, int原创 2013-09-12 15:21:48 · 595 阅读 · 0 评论 -
Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3):"123""132""213"原创 2013-09-13 10:18:08 · 482 阅读 · 0 评论 -
Anagrams
题目:Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.class Node{public: vector bit; int len; int index; int oper原创 2013-09-12 14:37:59 · 713 阅读 · 0 评论 -
Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each原创 2013-09-19 09:34:18 · 586 阅读 · 0 评论 -
Word Search
Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically原创 2013-09-19 09:39:14 · 507 阅读 · 0 评论 -
Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S = "ADOBECODEBANC"T = "ABC"Minimum window is "BAN原创 2013-09-19 09:31:27 · 625 阅读 · 0 评论 -
Text Justification
Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.You should pack your words in a greedy approach; that i原创 2013-09-19 09:38:03 · 526 阅读 · 0 评论 -
Simplify Path
Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"click to show corner cases.Corner Cases:Did原创 2013-09-19 09:35:43 · 640 阅读 · 0 评论 -
Regular Expression Matching
class Solution {public: bool isMatch(const char *s, const char *p) { if (*p == '\0') return *s == '\0'; if (*(p+1) != '*') { return ((*p == *s) || (*p == '.' && *s !=原创 2014-01-16 14:22:52 · 468 阅读 · 0 评论 -
Longest Palindromic Substring
class Solution { public: string longestPalindrome(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function int i, j; int left, right; int max; int原创 2014-01-16 14:25:22 · 405 阅读 · 0 评论 -
3Sum
class Solution { public: vector > threeSum(vector &num) { // Start typing your C/C++ solution below // DO NOT write int main() function //map > hashmap; map hashneg; map hashpos;原创 2014-01-16 14:28:19 · 469 阅读 · 0 评论