Skip to content

Instantly share code, notes, and snippets.

@zhoutuo
zhoutuo / Valid Palindrome.cpp
Last active December 12, 2015 09:29
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
class Solution {
public:
bool isPalindrome(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
string tmp = "";
for(int i = 0; i < s.length(); ++i) {
if(!islower(s[i])) {
s[i] = tolower(s[i]);
@zhoutuo
zhoutuo / Longest Substring Without Repeating Characters.cpp
Created February 11, 2013 01:47
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
class Solution {
public:
int lengthOfLongestSubstring(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
unsigned int result = 0;
set<char> table;
queue<char> substr;
for(int i = 0; i < s.length(); ++i) {
if(table.find(s[i]) != table.end()) {
@zhoutuo
zhoutuo / Add Two Numbers.cpp
Created February 11, 2013 02:53
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
@zhoutuo
zhoutuo / Reverse Integer.cpp
Created February 11, 2013 05:48
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321
class Solution {
public:
int lastDigit(int x) {
return x % 10;
}
int reverse(int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() functio
@zhoutuo
zhoutuo / Reverse Linked List II.cpp
Last active December 12, 2015 10:19
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
@zhoutuo
zhoutuo / Valid Parentheses.cpp
Created February 12, 2013 18:54
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
class Solution {
public:
bool isValid(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
stack<char> checker;
for(int i = 0; i < s.length() ; ++i) {
char cur = s[i];
if(isLeft(cur)) {
@zhoutuo
zhoutuo / Longest Common Prefix.cpp
Created February 13, 2013 01:13
Write a function to find the longest common prefix string amongst an array of strings.
class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
string prefix = "";
if(strs.size() == 0) {
return prefix;
}
@zhoutuo
zhoutuo / Remove Nth Node From End of List.cpp
Created February 13, 2013 02:51
Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given n will always be valid. Try to do this in one pass.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
@zhoutuo
zhoutuo / Generate Parentheses.cpp
Created February 13, 2013 03:25
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()"
class Solution {
public:
vector<string> generateParenthesis(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<string> result;
calc(result, "", n, 0);
return result;
@zhoutuo
zhoutuo / Swap Nodes in Pairs.cpp
Created February 14, 2013 18:43
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public: