代码改变世界

leetcode - Merge Sorted Array

2013-10-23 19:37 by 张汉生, 122 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 void merge(int A[], int m, int B[], int n) { 4 // Note: The Solution object is instantiated only once and is reused by each test case. 5 int i, j; 6 if (m=0; i--){26 while(i+tags[i]<k)27 A[k--] = B[j--];28 ... 阅读全文

leetcode - Roman to Integer

2013-10-20 19:01 by 张汉生, 186 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 int romanToInt(string s) { 4 // Note: The Solution object is instantiated only once and is reused by each test case. 5 string romans[30] = {"I","II","III","IV","V","VI","VII","VIII","I 阅读全文

leetcode - Merge Two Sorted Lists

2013-10-20 10:43 by 张汉生, 122 阅读, 0 推荐, 收藏, 编辑
摘要:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { // Note: The Solution object is instantiated only once and is... 阅读全文

leetcode - Remove Duplicates from Sorted Array

2013-10-20 10:16 by 张汉生, 123 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 int removeDuplicates(int A[], int n) { 4 // Note: The Solution object is instantiated only once and is reused by each test case. 5 if (n<=1) 6 return n; 7 int last = 1; 8 for (int i=1; i<n; i++){ 9 if (A[i]!=... 阅读全文

leetcode - Remove Element

2013-10-20 10:06 by 张汉生, 108 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 int removeElement(int A[], int n, int elem) { 4 // Note: The Solution object is instantiated only once and is reused by each test case. 5 for (int i=0; i0&&A[n-1]==elem){ 7 n--; 8 } 9 if (i>=n){10 ... 阅读全文

leetcode - Symmetric Tree

2013-10-20 09:58 by 张汉生, 162 阅读, 0 推荐, 收藏, 编辑
摘要:/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: bool isSymmetric(TreeNode * left, TreeNode * right){ if (left==NULL && right==NULL) ... 阅读全文

leetcode - Populating Next Right Pointers in Each Node

2013-10-20 09:49 by 张汉生, 189 阅读, 0 推荐, 收藏, 编辑
摘要:1 /** 2 * Definition for binary tree with next pointer. 3 * struct TreeLinkNode { 4 * int val; 5 * TreeLinkNode *left, *right, *next; 6 * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {} 7 * }; 8 */ 9 class Solution {10 public:11 void connect(TreeLinkNode *root) {1... 阅读全文

leetcode - Remove Duplicates from Sorted List

2013-10-19 21:57 by 张汉生, 112 阅读, 0 推荐, 收藏, 编辑
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution {10 public:11 ListNode *deleteDuplicates(ListNode *head) {12 // Note: The Solution object is in... 阅读全文

leetcode - Unique Binary Search Trees

2013-10-19 21:52 by 张汉生, 161 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 int numTrees(int n) { 4 // Note: The Solution object is instantiated only once and is reused by each test case. 5 int ans = 0; 6 if (n<=0) 7 return 0; 8 int * f = new int[n+1]; 9 f[0] = 1;10 for (int i=1;... 阅读全文

leetcode - Search Insert Position

2013-10-19 21:39 by 张汉生, 150 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 int searchInsert(int A[], int n, int target) { 4 // Note: The Solution object is instantiated only once and is reused by each test case. 5 if (n=target) 8 return 0; 9 if (A[n-1]==target)10 return n-1;11 if (A... 阅读全文
上一页 1 ··· 5 6 7 8 9 10 11 12 13 ··· 16 下一页