in GUI, a Form SearchForm for searching, TextField input for input key word.
but after search once, when go back search again, it throws
java.lang.IllegalStateException, it can't append form. why? The 1st round is
ok, but never can run the 2nd time.
searchForm.append(input);
I know it is easy to append to end of file, just cat file1 file2>file3.
Actually, I want to append one file into another file in given position (in
bytes). How can I do it?
Thanks.
I know it is easy to append to end of file, just cat file1 file2>file3.
Actually, I want to append one file into another file in given position (in
bytes). How can I do it?
Thanks.
I hope I have understood the problem right -- read the Javadoc for
FileOutputStream's constructor:
public FileOutputStream(String name,
boolean append)
throws FileNotFoundException
Creates an output file stream to write to the file with the specified name. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file
connection.
Hi all,
I want to append new column data to a data file. The data has been formated
in a tabbed way (very clean data without any missing/empty cells). This is
usefull when I run my c/c++ code and save data each time as one or more
columns. Ideally, it will be done within c/c++ code with fstream, but it is
ok to use stript split single (or 2 column) column data into multiple
columns.
e.g.
file1:
1 2 3
2 3 4
5 6 7
There are 5 data sets in total. Each set has one observation and 3 variables
(different variable names and attributes).
How to append or row bind all the data sets together?
Tried several ways, PROC DATASETS with FORCE causes missing data; PROC IML
cannot read in multiple tables, have to add one at a time.
I wrote some java code, might be ugly, but it works...
package Testing;
public class TestReplaceWithQuotes {
public static String replaceInsideQuotes(String source, char quote,
String target, String replacement){
StringBuilder regex = new StringBuilder();
StringBuilder replace = new StringBuilder();
Leetcode Integer to Roma
看完题,读了一下wiki,好像不那么复杂,行云流水写下如下code,竟然很少有的bug
free,沾沾自喜。查查别人的解法,有人说写成我这样会被面试官直接送回家。
帮我看看,至于吗?有啥问题?
public String intToRoman(int num) {
// Start typing your Java solution below
// DO NOT write main() function
StringBuilder sb = new StringBuilder();
while (num > 0) {
if (num >= 1000) {
sb.append('M');
num -= 1000;
} else if (num >= 900) {
sb.append("CM");
num -= 900;
} else if (num >= 500) {
sb.a... 阅读全帖
import java.io.*;
import java.util.*;
public class Roman
{
public static void main(String[] args)
{
new Roman().run();
}
PrintWriter out = null;
void run()
{
Scanner in = new Scanner(System.in);
out = new PrintWriter(System.out);
String s = in.next();
out.println(romanToInt(s));
out.close();
}
public String intToRoman(int num)
{
StringBuffer sb = new StringBuffer();
sb.append(Convert(num, 1000, ne... 阅读全帖
如果不用recursion的话代码太长是不是面试的时候不能用?
public ArrayList numString2Char(String num) {
//map to store all the previous possibilities
HashMap> map = new HashMap
ArrayList>();
int[] numbers = new int[num.length()];
for (int i = 0; i < num.length(); i++) {
numbers[i] = Integer.parseInt(num.charAt(i) + "");
}
if(numbers[0]==0) return null;
ArrayList tempList;
... 阅读全帖
public static String getDecimal(int a, int b){
if(a == 0)
return "0.0";
if(b == 0)
return "";
StringBuilder res = new StringBuilder();
res.append(a/b);
res.append(".");
int c = a;
HashMap mod = new HashMap();
ArrayList decimals = new ArrayList();
int index=0;
while(c%b !=0 && !mod.containsKey(c%b)){
... 阅读全帖
Here is my implementation. google了很久,本以为是个很基本的use case,但最后没
有找到好办法。写出来也不算太麻烦。没有考虑general的solution,也没有考虑CDData
和comment.对我们这个app应该足够了。
public static String removeNameSPace(String input1) {
String ret = null;
int strStart = 0;
boolean finished = false;
if ( input1 != null ){
StringBuffer sb = new StringBuffer(input1.length());
while (!finished){
int start = input1.indexOf('<', strStart);
int end = input1.indexOf('>', strStart);
if ( start != -1 && end != -1)... 阅读全帖
from Advanced Bash-Scripting Guide: http://www.tldp.org/LDP/abs/html/io-redirection.html
Chapter 20. I/O Redirection
Table of Contents
20.1. Using exec
20.2. Redirecting Code Blocks
20.3. Applications
There are always three default files [1] open, stdin (the keyboard), stdout
(the screen), and stderr (error messages output to the screen). These, and a
ny other open files, can be redirected. Redirection simply means capturing o
utput from a file, command, program, script, or even code block withi... 阅读全帖
先计算可以放的最大单词数,然后在单词中插入空格输出行
public class AdjustText {
static List justifyText(String[] words, int L) {
List result = new ArrayList();
int i = 0;
while (i < words.length) {
List lineWords = new ArrayList();
int curLen = 0;
do {
lineWords.add(words[i]);
curLen += words[i].length() + 1;
i++;
} while (i阅读全帖
看看你漏了什么
public class Solution {
public ArrayList fullJustify(String[] words, int L) {
ArrayList result=new ArrayList();
ArrayList currentLine=new ArrayList();
int i=0;
while(i
currentLine=new ArrayList();
int len=0;
int wordLength=0;
int wordCount=0;
do {
len+=words[i].length()+1;
wordLength+=words[i].lengt... 阅读全帖
仅此而已,no trick ,
public String lc301(String input) {
char[]inputt = input.toCharArray();
StringBuilder s = new StringBuilder();
StringBuilder res = new StringBuilder();
int open = 0;
for (char i : inputt) {
if (i =='(') {
open++;
s.append(i);
}else if (i ==')') {
if(open >0) {
s.append(i);
open--;
}
}el... 阅读全帖
I used a DP method and get the same result with your sample outputs.
My approach is to assume the last Ctrl-C is at position j,
then everything after the last Ctrl-C should be Ctrl-V.
I also think the operation right before Ctrl-C should be Ctrl-A.
So j can be varied to pick the maximum.
python code here, it prints out the solution key sequence
#!/usr/bin/env python
这个是我CrimeTypeKey的class,生成composite key object:
public class CrimeTypeKey implements Writable, WritableComparable<
CrimeTypeKey>
{
private Text key = new Text();
private IntWritable year = new IntWritable();
public CrimeTypeKey() { }
public CrimeTypeKey(String k, int y)
{
key.set(k);
year.set(y);
}
public static CrimeTypeKey read(DataInput in) throws IOException
{
CrimeTypeKey keytp = new CrimeTypeKey();
keytp.readFields(... 阅读全帖
ok, I only had a glimpse on a.append(0), and quickly skip it... :-), have
not written python for quite some time.
then I think it is correct. but personally, I feel it is tricky to append "0
" at the end of input, and also the input has to change. Another
controversial case is the empty input, you will output "out".
Well, maybe the reviewer did not see that "append", or did not think too
much. or there is still something wrong we have not checked out.
Your code is not far from not modifying inpu... 阅读全帖
是这样的solution吗?
其中returnPosition的function应该不难写,但面试会要求写吗?
应该像上面的牛人说的,建个char-》position的表格最好了,不然每次还要算。
结果应该不唯一,估计面试follow up需要decode。
public class remoteControl {
String[] dic = {"abcde", "fghij", "klmno","pqrst", "uvwxy", "z"};
public String remoteControl(String name) {
int x = 0;
int y = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < name.length(); i++) {
Point curr = returnPosition(name.charAt(i), dic);
for (int j = y... 阅读全帖
class TreeNode:
def __init__(self,x):
self.child=[]
self.val=x
class Solution:
def strToTree(self,s):
if len(s)==0: return None
stack=[]
for i in range(len(s)):
if s[i]=='(': stack.append(s[i])
elif s[i].isalpha():
stack.append(TreeNode(s[i])) #push to stack except ')'
else:
t,temp=stack.pop(),[]
while t!='(':
temp.append(t)
t... 阅读全帖
We have two schedules S1&S2:
S1 = { , , ... }
S2 = { , , ... }
Assumptions:
1. S1&S2 are sorted by the starting time.
2. There are no conflicts in S1 and S2 itself.
Problem:
We try to find all the conflicts between these two schedules.
the basic idea is to merge sort to find the conflicts. the running time is
O(n).
My code in python: (code is kind of messy)
def merge(a,b):
i, j = 0, 0
unconflict = []
conflict = []
while( i < len(a... 阅读全帖
我的递归解法,大家看看对吗?
Node flat_list_head[N]; // N is the largest level.
void flattenList(Node *head, int level)
{
if (head->next){
insert_node_front_flat_list_head(head, level);
// Go next
head = head->next;
flattenList(head, level);
}
if (head->child){
insert_node_front_flat_list_head(head, level+1);
// Go child
head = head->child;
flattenList(head, level+1);
}
}
void ... 阅读全帖
Python写了一个,练练手
L = ['fooo', 'barr', 'wing', 'ding', 'wing']
S = 'lingmindraboofooowingdingbarrwingmonkeypoundcake'
# L1 stores distinct stings in L
# multiples counts the corresponding multiples
L1 = []
multiples = []
for str in L:
if str not in L1:
L1.append(str)
multiples.append(L.count(str))
# k is the lenght of each substring
k = len(L[0])
match = [0]*len(S)
checklist = []
for i in range(0, len(L1)):
mul = multiples[i]
list_pos = []
# find all possible positio... 阅读全帖
Python写了一个,练练手
L = ['fooo', 'barr', 'wing', 'ding', 'wing']
S = 'lingmindraboofooowingdingbarrwingmonkeypoundcake'
# L1 stores distinct stings in L
# multiples counts the corresponding multiples
L1 = []
multiples = []
for str in L:
if str not in L1:
L1.append(str)
multiples.append(L.count(str))
# k is the lenght of each substring
k = len(L[0])
match = [0]*len(S)
checklist = []
for i in range(0, len(L1)):
mul = multiples[i]
list_pos = []
# find all possible positio... 阅读全帖
这个是概率题。。
先生成 4个bit的 二进制 true代表1,false代表0
这样我们能生成0-15的 等概率的 distribution
然后 [0,15] 当中 选[0,10] -->[0,2](true), [3,9](false)
public class Solution {
/**
* @param args
*/
public void doit(){
int iterate = 10000000;
boolean valve = true;
StringBuilder s = new StringBuilder();
while (valve) {
StringBuilder str = new StringBuilder();
for (int i = 0; i < 4; i++) {
if (Math.random() < 0.5) str.append('1');
... 阅读全帖