SAN JOSE — Samsung has bought from Apple a big office building in San Jose, a deal that would enable the tech and consumer electronics titan to widen its Silicon Valley footprint.