0%

Array copy

1
2
3
4
5
6
7
8
9
int[] array1, array2;
array1 = new int[] {2,3,5,7,8,9,12,11,10};

//copy array1 to array2
array2 = new int[array1.length];
for(int i = 0; i < array2.length; i++)
{
array2[i] = array1[i];
}

我们通过new的方式,给array2在heap空间中开辟了新的数组空间,将array1数组的元素值一个个的copy进array2里

Swap array

1
2
3
4
5
6
for(int i = 0; i < arr.length / 2; i++)
{
String temp = arr[i];
arr[i] = arr[arr.length - i - 1];
arr[arr.length - i - 1] = temp;
}
Read more »

Blueprint

A graphic description to create a specification

Models
give us the opportunity to fail

Complexity

  1. Size: The number of components
    tree: 3 lines of code, but more complexity
    bubble sort: 10 lines of code
  2. Abstraction
    what is behind code? Eg: tree has less code but harder
Read more »

Requirements is not necessities

Requirement interact with tasks -> stps
i.e. Pacman video game involved requirement

Use case diagram is used to represent requirements

Activity Diagram: sequence of something, condition, loop
State diagram

Read more »

算法基于一个事实,数组从任意位置劈开后,至少有一半是有序的

比如 [ 4 5 6 7 1 2 3] ,从 7 劈开,左边是 [ 4 5 6 7] 右边是 [ 7 1 2 3],左边是有序的。

我们可以先找到哪一段是有序的 (只要判断端点即可),然后看 target 在不在这一段里,如果在,那么就把另一半丢弃。如果不在,那么就把这一段丢弃。

Read more »

Overview

In this project we will write a program that will “fire” a cannonball at a mid-air target and determine if the cannonball hits the target or not. Five variables needed for this calculation will be provided by the user. Our program will do minor input validation on this input and will output the outcome of your calculations. See the diagram below for an example of what we are calculating.

Read more »

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

Example 1:

Input: n = 3
Output: [“((()))”,”(()())”,”(())()”,”()(())”,”()()()”]
Example 2:

Input: n = 1
Output: [“()”]

Read more »

Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists.

Input: l1 = [1,2,4], l2 = [1,3,4]
Output: [1,1,2,3,4,4]
Example 2:

Input: l1 = [], l2 = []
Output: []
Example 3:

Input: l1 = [], l2 = [0]
Output: [0]

Read more »